-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_test.go
52 lines (42 loc) · 1.05 KB
/
benchmark_test.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
package main
import (
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
)
func TestBenchmark(t *testing.T) {
requests := 100
var received int64
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello"))
atomic.AddInt64(&received, 1)
}))
defer ts.Close()
config := &Config{
concurrency: 10,
requests: requests,
method: "GET",
executionTimeout: MaxExecutionTimeout,
url: ts.URL,
}
context := NewContext(config)
context.SetInt(FieldContentSize, 5)
benchmark := NewBenchmark(context)
go benchmark.Run()
go func() {
counter := 0
for record := range benchmark.collector {
counter++
if counter == requests || record.Error != nil {
break
}
}
close(context.stop)
}()
context.start.Wait()
<-context.stop
if actualReceived := atomic.LoadInt64(&received); int64(requests) != actualReceived {
t.Fatalf("expected to send %d requests and receive %d responses, but got %d responses", requests, requests, actualReceived)
}
}