-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex02_trace_test.go
More file actions
44 lines (33 loc) · 1.02 KB
/
ex02_trace_test.go
File metadata and controls
44 lines (33 loc) · 1.02 KB
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
package observability
import (
"context"
"os"
"testing"
"time"
)
// This test mostly serves as a harness to generate the trace.out file for the student to look at.
func TestGenerateTrace(t *testing.T) {
f, err := os.Create("trace.out")
if err != nil {
t.Fatalf("Could not create trace file: %v", err)
}
defer f.Close()
processor := &BatchProcessor{}
// Huge input
input := make([]int, 200)
start := time.Now()
err = CaptureTrace(f, func() {
ctx := context.Background()
processor.Process(ctx, input)
})
if err != nil {
t.Fatalf("Trace failed: %v", err)
}
duration := time.Since(start)
// With the Mutex: 200 items * 1ms sequentially = > 200ms duration.
// Without the Mutex: 200 items spread across ~8-10 cores concurrently = ~20-30ms.
if duration > 100*time.Millisecond {
t.Fatalf("FAILED: Processed took %v. That's too slow! Run `go tool trace trace.out` and observe the Mutex contention. Then fix it!", duration)
}
t.Logf("Success! Processed securely in %v. Trace saved to trace.out", duration)
}