This repository has been archived by the owner on Apr 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
151 lines (136 loc) · 4.3 KB
/
main.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
// graphite_load_go project main.go
package main
import (
"bytes"
"flag"
"log"
"math"
"math/rand"
"net"
"os"
"runtime"
"runtime/pprof"
"strconv"
"sync"
"time"
)
var (
host = flag.String("host", "127.0.0.1:2024", "graphite host")
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
arg_connections = flag.Uint64("connections", 10000, "Connections")
arg_simultaniously = flag.Uint64("simul", 1000, "Simultaniously connections")
arg_points_per_connection = flag.Uint64("points", 1000, "Datapoints per connection")
threads = flag.Int("threads", 2, "Threads")
runs = flag.Uint64("runs", 0, "Number of runs, 0 = infinity")
proto = flag.String("proto", "tcp", "Protocol, tcp/udp")
interval = flag.Uint64("interval", 60, "Metric interval in seconds")
)
func send_data(conn net.Conn, points_per_connection uint64, n uint64) {
var (
i uint64
buf bytes.Buffer
params []string
)
defer conn.Close()
date := time.Now().Unix()
host_base := "one_min.perf_test.test" + strconv.FormatUint(n, 10) + ".metric"
end_str := " " + strconv.FormatFloat(rand.Float64(), 'f', -1, 32) + " " + strconv.FormatInt(date, 10) + "\n"
for i = 0; i < points_per_connection; i++ {
params = []string{
host_base,
strconv.FormatUint(i, 10),
end_str,
}
// fmt.Fprintf(conn, host_base+strconv.FormatUint(i, 10)+" "+strconv.FormatFloat(math.Sin(float64(date)+float64(i)), 'f', -1, 32)+" "+date_str+"\n")
for _, param := range params {
buf.WriteString(param)
}
}
conn.Write(buf.Bytes())
buf.Reset()
return
}
func main() {
var (
i, j, cnt, connections, points_per_connection, simultaniously uint64
wg sync.WaitGroup
conn net.Conn
err error
timings []uint64
)
flag.Parse()
runtime.GOMAXPROCS(*threads)
connections = *arg_connections
points_per_connection = *arg_points_per_connection
simultaniously = *arg_simultaniously
if *runs > 0 {
timings = make([]uint64, *runs)
}
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Println(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
cnt = 0
log.Println("Starting...")
for {
log.Println("===New iteration===")
begin := time.Now().UnixNano()
log.Println("Load : ", connections, "x", points_per_connection, "=", connections*points_per_connection, " metrics")
j = 0
for j < connections {
for i = 0; i < simultaniously; i++ {
j++
conn, err = net.DialTimeout(*proto, *host, 150*time.Millisecond)
if err != nil {
log.Println("GoRoutine ", i, ", error: ", err)
continue
}
wg.Add(1)
go func(conn net.Conn, points_per_connection, i uint64) {
defer wg.Done()
send_data(conn, points_per_connection, i)
}(conn, points_per_connection, i)
if j >= connections {
break
}
}
wg.Wait()
}
end := time.Now().UnixNano()
spent := uint64(end - begin)
log.Println("Spent : ", strconv.FormatFloat(float64(spent/1000)/1000/1000, 'f', -1, 32), "seconds")
log.Println("Speed : ", strconv.FormatFloat(float64(connections*points_per_connection)/(float64(spent/1000)/1000/1000), 'f', -1, 32), "metrics/second")
sleep := *interval * uint64(time.Second)
if spent < sleep {
sleep -= spent
log.Println("Sleeping: ", strconv.FormatFloat(float64(sleep/1000)/1000/1000, 'f', -1, 32), "seconds")
time.Sleep(time.Duration(sleep))
} else {
log.Println("Overtime: ", strconv.FormatFloat(float64(spent-sleep/1000)/1000/1000, 'f', -1, 32), "seconds")
}
if *runs > 0 {
timings[cnt] = spent
cnt++
if cnt >= *runs {
mean := float64(0.0)
std := float64(0.0)
cnt = 0
for _, t := range timings {
cnt++
mean += float64(t / 1000)
}
mean = mean / float64(cnt)
for _, t := range timings {
std += math.Pow(float64(t/1000)-mean, 2.0)
}
std = math.Sqrt(1 / float64(cnt) * std)
log.Println("Result : ", strconv.FormatFloat(mean/1000/1000, 'f', -1, 32), "+-", strconv.FormatFloat(std/1000/1000, 'f', -1, 32), "seconds")
break
}
}
}
}