This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
forked from codahale/buster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buster_test.go
96 lines (80 loc) · 1.93 KB
/
buster_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
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
package buster_test
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/picmonkey/buster"
)
func Example() {
// run a bench for 1 minute, tracking latencies from 1µs to 1 minute
bench := buster.Bench{
Duration: 1 * time.Minute,
MinLatency: 1 * time.Microsecond,
MaxLatency: 1 * time.Minute,
}
r := bench.Run(
10, // concurrent workers
1000, // 1000 ops/sec total
func(id int, gen *buster.Generator) error { // the job to be run
client := &http.Client{}
return gen.Do(func() error {
// perform a GET request
resp, err := client.Get("http://www.google.com/")
if err != nil {
return err
}
// read the body
io.Copy(ioutil.Discard, resp.Body)
return resp.Body.Close()
})
},
)
fmt.Println(r)
}
func TestBenchRun(t *testing.T) {
bench := buster.Bench{
Duration: 1 * time.Second,
MinLatency: 1 * time.Millisecond,
MaxLatency: 1 * time.Second,
}
r := bench.Run(10, 1000, func(id int, gen *buster.Generator) error {
return gen.Do(func() error {
return nil
})
})
if v, want := r.Concurrency, 10; v != want {
t.Errorf("Concurrency was %d, but expected %d", v, want)
}
}
func TestBenchRunFailures(t *testing.T) {
bench := buster.Bench{
Duration: 1 * time.Second,
MinLatency: 1 * time.Millisecond,
MaxLatency: 1 * time.Second,
}
r := bench.Run(10, 1000, func(id int, gen *buster.Generator) error {
return gen.Do(func() error {
return errors.New("woo hoo")
})
})
if r.Failure == 0 {
t.Errorf("Failure count was 0, but expected %d", r.Failure)
}
}
func TestBenchRunErrors(t *testing.T) {
bench := buster.Bench{
Duration: 1 * time.Second,
MinLatency: 1 * time.Millisecond,
MaxLatency: 1 * time.Second,
}
r := bench.Run(10, 100, func(id int, gen *buster.Generator) error {
return errors.New("woo hoo")
})
if v, want := len(r.Errors), 10; v != want {
t.Fatalf("Error count was %d, but expected %d", v, want)
}
}