1package vegeta
2
3import (
4	"reflect"
5	"testing"
6	"time"
7)
8
9func TestHistogram_Add(t *testing.T) {
10	t.Parallel()
11	hist := Histogram{
12		Buckets: []time.Duration{
13			0,
14			10 * time.Millisecond,
15			25 * time.Millisecond,
16			50 * time.Millisecond,
17			100 * time.Millisecond,
18			1000 * time.Millisecond,
19		},
20	}
21
22	for _, d := range []time.Duration{
23		5 * time.Millisecond,
24		15 * time.Millisecond,
25		30 * time.Millisecond,
26		75 * time.Millisecond,
27		200 * time.Millisecond,
28		2000 * time.Millisecond,
29	} {
30		hist.Add(&Result{Latency: d})
31	}
32
33	if got, want := hist.Counts, []uint64{1, 1, 1, 1, 1, 1}; !reflect.DeepEqual(got, want) {
34		t.Errorf("Counts: got: %v, want: %v", got, want)
35	}
36
37	if got, want := hist.Total, uint64(6); got != want {
38		t.Errorf("Total: got %v, want: %v", got, want)
39	}
40}
41
42func TestBuckets_UnmarshalText(t *testing.T) {
43	t.Parallel()
44	for value, want := range map[string]string{
45		"":       "bad buckets: ",
46		" ":      "bad buckets:  ",
47		"{0, 2}": "bad buckets: {0, 2}",
48		"[]":     "time: invalid duration ",
49		"[0, 2]": "time: missing unit in duration 2",
50	} {
51		if got := (&Buckets{}).UnmarshalText([]byte(value)).Error(); got != want {
52			t.Errorf("got: %v, want: %v", got, want)
53		}
54	}
55
56	for value, want := range map[string]Buckets{
57		"[0,5ms]":             {0, 5 * time.Millisecond},
58		"[0, 5ms]":            {0, 5 * time.Millisecond},
59		"[   0,5ms, 10m    ]": {0, 5 * time.Millisecond, 10 * time.Minute},
60		"[3ms,10ms]":          {0, 3 * time.Millisecond, 10 * time.Millisecond},
61	} {
62		var got Buckets
63		if err := got.UnmarshalText([]byte(value)); err != nil {
64			t.Fatal(err)
65		} else if !reflect.DeepEqual(got, want) {
66			t.Errorf("got: %v, want: %v", got, want)
67		}
68	}
69}
70