-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (149 loc) · 3.97 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Package main contains a small tool to interact with php-fpm over FastCGI.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"sort"
"sync"
"time"
)
func worker(
network, address string,
req map[string]string,
doneWG *sync.WaitGroup,
tick <-chan time.Time,
doneChan <-chan struct{},
latencyChan chan<- time.Duration,
) {
defer doneWG.Done()
fcgi, err := Dial(network, address)
if err != nil {
log.Printf("ERR: dial: %v", err)
return
}
fcgi.SetKeepAlive(true)
defer fcgi.Close()
for {
select {
case <-doneChan:
return
case <-tick:
}
now := time.Now()
resp, err := fcgi.Get(req)
if err != nil {
log.Printf("ERR: get: %v", err)
return
}
if resp.StatusCode != http.StatusOK {
log.Printf("ERR: http: %v", resp.StatusCode)
return
}
_, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Printf("ERR: read: %v", err)
return
}
latencyChan <- time.Since(now)
}
}
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] <benchmark|get>\n", os.Args[0])
flag.PrintDefaults()
}
script := flag.String("script", "index.php", "script to execute")
network := flag.String("network", "tcp", "network to dial (net or unix)")
address := flag.String("address", "localhost:9000", "address to dial")
workerCount := flag.Int("workers", 4, "number of worker goroutines")
rate := flag.Float64("rate", 10, "number of requests per second")
duration := flag.Duration("duration", 30*time.Second, "duration of the measurement")
verbose := flag.Bool("verbose", false, "verbose output")
flag.Parse()
args := flag.Args()
if len(args) != 1 {
flag.Usage()
os.Exit(1)
}
req := map[string]string{
"SCRIPT_FILENAME": path.Join("/opt/omegaup/frontend/www", *script),
"GATEWAY_INTERFACE": "CGI/1.1",
"SERVER_SOFTWARE": "go / fcgiclient",
"REMOTE_ADDR": "127.0.0.1",
}
switch args[0] {
case "benchmark":
var doneWG sync.WaitGroup
tick := time.NewTicker(time.Second / time.Duration(*rate))
doneChan := make(chan struct{})
latencyChan := make(chan time.Duration, 1024)
for i := 0; i < *workerCount; i++ {
doneWG.Add(1)
go worker(*network, *address, req, &doneWG, tick.C, doneChan, latencyChan)
}
time.AfterFunc(*duration, func() {
tick.Stop()
close(doneChan)
})
go func() {
doneWG.Wait()
close(latencyChan)
}()
var latencies []time.Duration
var averageLatency time.Duration
for latency := range latencyChan {
averageLatency += latency
latencies = append(latencies, latency)
}
log.Printf("Done sending %d requests", len(latencies))
if len(latencies) == 0 {
return
}
sort.Slice(latencies, func(i, j int) bool { return latencies[i] < latencies[j] })
averageLatency /= time.Duration(len(latencies))
log.Printf("Mean latency: %v", averageLatency)
log.Printf("Min latency: %v", latencies[0])
log.Printf("50th latency: %v", latencies[len(latencies)/2])
log.Printf("90th latency: %v", latencies[int(float64(len(latencies))*0.9)])
log.Printf("95th latency: %v", latencies[int(float64(len(latencies))*0.95)])
log.Printf("99th latency: %v", latencies[int(float64(len(latencies))*0.99)])
log.Printf("Max latency: %v", latencies[len(latencies)-1])
case "get":
fcgi, err := Dial(*network, *address)
if err != nil {
log.Fatalf("ERR: dial: %v", err)
}
defer fcgi.Close()
resp, err := fcgi.Get(req)
if err != nil {
log.Fatalf("ERR: get: %v", err)
}
if *verbose {
fmt.Fprintf(os.Stderr, "HTTP/%d.%d %d\n", resp.ProtoMajor, resp.ProtoMinor, resp.StatusCode)
for k, values := range resp.Header {
for _, v := range values {
fmt.Fprintf(os.Stderr, "%v: %v\n", k, v)
}
}
fmt.Fprintf(os.Stderr, "\n")
}
contents, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatalf("ERR: read: %v", err)
}
os.Stdout.Write(contents)
if resp.StatusCode != 0 && resp.StatusCode != http.StatusOK {
os.Exit(1)
}
default:
flag.Usage()
os.Exit(1)
}
}