-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
71 lines (59 loc) · 1.32 KB
/
main_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"encoding/json"
"io/ioutil"
"net/http/httptest"
"testing"
)
func Benchmark_ServeHttp(b *testing.B) {
req := httptest.NewRequest("GET", "http://example.com/random?start=10&end=20000", nil)
w := httptest.NewRecorder()
for n := 0; n < b.N; n++ {
s := &Server{}
s.ServeHTTP(w, req)
if w.Result().StatusCode != 200 {
b.Error("Invalid status code")
}
}
}
func reset(rw *httptest.ResponseRecorder) {
m := rw.HeaderMap
for k := range m {
delete(m, k)
}
body := rw.Body
body.Reset()
*rw = httptest.ResponseRecorder{
Body: body,
HeaderMap: m,
}
}
func BenchmarkHealthParallel(b *testing.B) {
r := httptest.NewRequest("GET", "http://example.com/health", nil)
b.RunParallel(func(pb *testing.PB) {
rw := httptest.NewRecorder()
for pb.Next() {
HandleHealth(rw, r)
reset(rw)
}
})
}
func Benchmark_Random(b *testing.B) {
for n := 0; n < b.N; n++ {
req := httptest.NewRequest("GET", "http://example.com/random?start=10&end=20", nil)
w := httptest.NewRecorder()
HandleRandom(w, req)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
random := &RandomNumber{}
if json.Unmarshal(body, random) != nil {
b.Error("Failed to unmarshal")
}
if random.Number < 10 {
b.Error("Random to low")
}
if random.Number > 20 {
b.Error("Random to high")
}
}
}