-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoStoragePing.go
162 lines (141 loc) · 3.7 KB
/
GoStoragePing.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
156
157
158
159
160
161
162
package main
import (
"flag"
"io/ioutil"
"log"
"os"
"os/signal"
"time"
"github.com/fatih/color"
)
func CheckIfFileExists(file string) bool {
_, error := os.Stat(file)
var value bool
// check if error is "file not exists"
if os.IsNotExist(error) {
value = false
} else {
value = true
}
return value
}
func CreateFile(file string) {
if CheckIfFileExists(file) {
color.Set(color.FgRed)
log.Println("File exists!")
os.Exit(1)
} else {
f, e := os.Create(file)
if e != nil {
log.Fatal(e)
}
color.Set(color.FgCyan)
log.Printf("Created file \"%v\"\n", file)
f.Close()
}
}
func RemoveFile(file string) {
e := os.Remove(file)
if e != nil {
log.Fatal(e)
}
color.Set(color.FgCyan)
log.Printf("Removed file \"%v\"\n", file)
}
func WriteStringToFile(text string, file string) {
f, err := os.Create(file)
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err2 := f.WriteString(text)
if err2 != nil {
log.Fatal(err2)
}
}
func ReadStringFromFile(file string) {
_, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
}
func main() {
// FLAGS
pathPtr := flag.String("path", "empty", "Give a path for the file, this file will use to read/write. THIS FILE WILL BE OVERWRITTEN")
// TODO: RW Options
//readPtr := flag.Bool("r", false, "Give a path for the file, this file will use to read/write. THIS FILE WILL BE OVERWRITTEN")
//writePtr := flag.Bool("w", false, "Give a path for the file, this file will use to read/write. THIS FILE WILL BE OVERWRITTEN")
intervalPtr := flag.Int64("interval", 1000, "Interval between checks in milliseconds.")
textPtr := flag.String("text", "ping", "You can change the text that is written to the test file.")
latencyWarnPtr := flag.Int64("latencywarn", 20, "Latency in milliseconds at which the text will change color to show high latency")
flag.Parse()
// Vars from flags
var path string = *pathPtr
// > TODO: Read and write options are disabled, as they are messed by caches, I need to study it more.
//var read bool = *readPtr
//var write bool = *writePtr
var read bool = false
var write bool = false
// TODO <
var interval int64 = *intervalPtr
var text string = *textPtr
var latencyWarn int64 = *latencyWarnPtr
// Time calculation vars
start := time.Now()
elapsed := time.Since(start)
var latency float64 = float64(elapsed.Microseconds()) / 1000
// Initial file creation
CreateFile(path)
//CTRL+C Handle
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
RemoveFile(path)
log.Printf("Captured %v, byebye!", sig)
os.Exit(0)
}
}()
// Measures
//var interval time.Duration = time.Duration(*intervalPtr)
for true {
if (write && !read) || (!write && !read) {
//WRITE
//Measurement
start = time.Now()
WriteStringToFile(text, path)
elapsed = time.Since(start)
//Conversion to milliseconds
latency = float64(elapsed.Microseconds()) / 1000
//Coloring
if latency >= float64(latencyWarn) {
color.Set(color.FgRed)
} else {
color.Set(color.FgGreen)
}
// Print!
log.Printf("W: => P[I]NG time=%v ms", float64(elapsed.Microseconds())/1000)
// Interval
time.Sleep(time.Duration(interval) * time.Millisecond)
}
if (!write && read) || (!write && !read) {
//READ
//Measurement
start = time.Now()
ReadStringFromFile(path)
elapsed = time.Since(start)
//Conversion to milliseconds
latency = float64(elapsed.Microseconds()) / 1000
// Coloring
if latency >= float64(latencyWarn) {
color.Set(color.FgRed)
} else {
color.Set(color.FgGreen)
}
// Print!
log.Printf("R: <= P[O]NG time=%v ms", float64(elapsed.Microseconds())/1000)
// Interval
time.Sleep(time.Duration(interval) * time.Millisecond)
}
}
}