MCPcopy
hub / github.com/grpc/grpc-go / TestProfiling

Method TestProfiling

internal/profiling/profiling_test.go:40–92  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

38}
39
40func (s) TestProfiling(t *testing.T) {
41 cb, err := buffer.NewCircularBuffer(128)
42 if err != nil {
43 t.Fatalf("error creating circular buffer: %v", err)
44 }
45
46 stat := NewStat("foo")
47 cb.Push(stat)
48 bar := func(n int) {
49 if n%2 == 0 {
50 defer stat.NewTimer(strconv.Itoa(n)).Egress()
51 } else {
52 timer := NewTimer(strconv.Itoa(n))
53 stat.AppendTimer(timer)
54 defer timer.Egress()
55 }
56 time.Sleep(1 * time.Microsecond)
57 }
58
59 numTimers := int(8 * defaultStatAllocatedTimers)
60 for i := 0; i < numTimers; i++ {
61 bar(i)
62 }
63
64 results := cb.Drain()
65 if len(results) != 1 {
66 t.Fatalf("len(results) = %d; want 1", len(results))
67 }
68
69 statReturned := results[0].(*Stat)
70 if stat.Tags != "foo" {
71 t.Fatalf("stat.Tags = %s; want foo", stat.Tags)
72 }
73
74 if len(stat.Timers) != numTimers {
75 t.Fatalf("len(stat.Timers) = %d; want %d", len(stat.Timers), numTimers)
76 }
77
78 lastIdx := 0
79 for i, timer := range statReturned.Timers {
80 // Check that they're in the order of append.
81 if n, err := strconv.Atoi(timer.Tags); err != nil && n != lastIdx {
82 t.Fatalf("stat.Timers[%d].Tags = %s; wanted %d", i, timer.Tags, lastIdx)
83 }
84
85 // Check that the timestamps are consistent.
86 if diff := timer.End.Sub(timer.Begin); diff.Nanoseconds() < 1000 {
87 t.Fatalf("stat.Timers[%d].End - stat.Timers[%d].Begin = %v; want >= 1000ns", i, i, diff)
88 }
89
90 lastIdx++
91 }
92}
93
94func (s) TestProfilingRace(t *testing.T) {
95 stat := NewStat("foo")

Callers

nothing calls this directly

Calls 9

PushMethod · 0.95
EgressMethod · 0.95
NewTimerMethod · 0.95
AppendTimerMethod · 0.95
DrainMethod · 0.95
NewCircularBufferFunction · 0.92
NewStatFunction · 0.85
NewTimerFunction · 0.85
FatalfMethod · 0.65

Tested by

no test coverage detected