-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
155 lines (134 loc) · 3.37 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package command
import (
"bufio"
"encoding/csv"
"fmt"
"log"
"math"
"os"
"strings"
"sync"
"time"
)
var elements = make(chan []string)
var start = time.Now()
var filesCount = 0
var successUsersCount = 0
var successFilesLastCheckBySeconds = 0
var failedUsersCount = 0
// ICommand is used for definning the action to execute
type ICommand interface {
ExecuteAction(element []string) (string, error)
}
func waitToFinish(outputPointer *csv.Writer, outputFile *os.File, seconds int) {
var wg sync.WaitGroup
wg.Add(1)
ticker := time.NewTicker(time.Duration(seconds) * time.Second)
quit := make(chan struct{})
go func() {
defer wg.Done()
for {
select {
case <-ticker.C:
if filesCount == successFilesLastCheckBySeconds {
printProgress()
close(quit)
} else {
successFilesLastCheckBySeconds = filesCount
}
case <-quit:
ticker.Stop()
closeOutputWriter(outputPointer, outputFile)
return
}
}
}()
fmt.Println("Waiting To Finish")
wg.Wait()
fmt.Println("\nTerminating Program")
}
func closeOutputWriter(pointer *csv.Writer, file *os.File) {
pointer.Flush()
file.Close()
}
func printProgress() {
seconds := time.Since(start).Seconds()
fmt.Printf("Mean velocity: %v f/s -- Index: %v files -- Success: %v files -- Failed: %v files \n",
math.Round(float64(filesCount)/seconds),
filesCount,
successUsersCount,
failedUsersCount,
)
}
func definedOrEmpty(arr []string, pos int) string {
if len(arr) >= pos+1 {
return arr[pos]
}
return ""
}
func readFile(filepath string, separator string, elements chan []string) {
file, err := os.Open(filepath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
fmt.Println("Reading file")
scanner := bufio.NewScanner(file)
for scanner.Scan() {
split := strings.Split(scanner.Text(), separator)
if len(split) < 1 {
fmt.Printf("Ignored small Element: %v \n", scanner.Text())
continue
}
elements <- split
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
fmt.Println("Ending reading file")
}
// createRoutines is used to create de amount of needed routines
func createRoutines(command ICommand, count int, outputPointer *csv.Writer) {
for i := 0; i < count; i++ {
time.Sleep(1 * time.Second)
go func() {
for {
select {
case element := <-elements:
executeAction(command, element, outputPointer)
filesCount++
printProgress()
}
}
}()
}
}
func executeAction(command ICommand, element []string, outputPointer *csv.Writer) {
res, err := command.ExecuteAction(element)
if err != nil {
fmt.Printf("Server error: %v \n", err)
failedUsersCount++
element = append(element, err.Error())
outputPointer.Write(element)
outputPointer.Flush()
return
}
fmt.Printf("Response: %v \n", res)
successUsersCount++
}
func getOutputWriter(outputPath string) (*csv.Writer, *os.File) {
csvfile, err := os.OpenFile(outputPath, os.O_CREATE|os.O_WRONLY, 0777)
if err != nil {
fmt.Printf("Failed creating file: %s \n", err)
os.Exit(0)
}
pointer := csv.NewWriter(csvfile)
return pointer, csvfile
}
// RunProcess is the function to trigger the full process
func RunProcess(command ICommand, routines int, inFile string, outFile string, secondsToFinish int) {
outputPointer, outputFile := getOutputWriter(outFile)
createRoutines(command, routines, outputPointer)
readFile(inFile, ",", elements)
waitToFinish(outputPointer, outputFile, secondsToFinish)
}