...
1 package gmeasure_test
2
3 import (
4 "time"
5
6 . "github.com/onsi/ginkgo/v2"
7 . "github.com/onsi/gomega"
8 "github.com/onsi/gomega/gmeasure"
9 )
10
11 var _ = Describe("Stopwatch", func() {
12 var e *gmeasure.Experiment
13 var stopwatch *gmeasure.Stopwatch
14
15 BeforeEach(func() {
16 e = gmeasure.NewExperiment("My Test Experiment")
17 stopwatch = e.NewStopwatch()
18 })
19
20 It("records durations", func() {
21 time.Sleep(100 * time.Millisecond)
22 stopwatch.Record("recordings", gmeasure.Annotation("A"))
23 time.Sleep(100 * time.Millisecond)
24 stopwatch.Record("recordings", gmeasure.Annotation("B")).Reset()
25 time.Sleep(100 * time.Millisecond)
26 stopwatch.Record("recordings", gmeasure.Annotation("C")).Reset()
27 time.Sleep(100 * time.Millisecond)
28 stopwatch.Pause()
29 time.Sleep(100 * time.Millisecond)
30 stopwatch.Resume()
31 time.Sleep(100 * time.Millisecond)
32 stopwatch.Pause()
33 time.Sleep(100 * time.Millisecond)
34 stopwatch.Resume()
35 time.Sleep(100 * time.Millisecond)
36 stopwatch.Record("recordings", gmeasure.Annotation("D"))
37 durations := e.Get("recordings").Durations
38 annotations := e.Get("recordings").Annotations
39 Ω(annotations).Should(Equal([]string{"A", "B", "C", "D"}))
40 Ω(durations[0]).Should(BeNumerically("~", 100*time.Millisecond, 50*time.Millisecond))
41 Ω(durations[1]).Should(BeNumerically("~", 200*time.Millisecond, 50*time.Millisecond))
42 Ω(durations[2]).Should(BeNumerically("~", 100*time.Millisecond, 50*time.Millisecond))
43 Ω(durations[3]).Should(BeNumerically("~", 300*time.Millisecond, 50*time.Millisecond))
44
45 })
46
47 It("panics when asked to record but not running", func() {
48 stopwatch.Pause()
49 Ω(func() {
50 stopwatch.Record("A")
51 }).Should(PanicWith("stopwatch is not running - call Resume or Reset before calling Record"))
52 })
53
54 It("panics when paused but not running", func() {
55 stopwatch.Pause()
56 Ω(func() {
57 stopwatch.Pause()
58 }).Should(PanicWith("stopwatch is not running - call Resume or Reset before calling Pause"))
59 })
60
61 It("panics when asked to resume but not paused", func() {
62 Ω(func() {
63 stopwatch.Resume()
64 }).Should(PanicWith("stopwatch is running - call Pause before calling Resume"))
65 })
66 })
67
View as plain text