From 41e8b0930c050b938fe8235cd6eefa4411a4ce97 Mon Sep 17 00:00:00 2001 From: NoneBack Date: Sat, 12 Oct 2024 16:20:20 +0800 Subject: [PATCH] ut: add profiler ut --- profiler_test.go | 106 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 profiler_test.go diff --git a/profiler_test.go b/profiler_test.go new file mode 100644 index 0000000..8faf752 --- /dev/null +++ b/profiler_test.go @@ -0,0 +1,106 @@ +package gotaskflow + +import ( + "bytes" + "testing" + "time" +) + +func TestProfilerStartStop(t *testing.T) { + profiler := NewTracer() + profiler.Start() + time.Sleep(10 * time.Millisecond) + profiler.Stop() + + if profiler.start.After(profiler.end) { + t.Errorf("expected start time before end time, got start: %v, end: %v", profiler.start, profiler.end) + } + + if profiler.start.IsZero() || profiler.end.IsZero() { + t.Errorf("expected start and end times to be set, got start: %v, end: %v", profiler.start, profiler.end) + } +} + +func TestProfilerAddSpan(t *testing.T) { + profiler := NewTracer() + span := &span{ + extra: attr{ + typ: NodeStatic, + success: true, + name: "test-span", + }, + begin: time.Now(), + end: time.Now().Add(5 * time.Millisecond), + } + profiler.AddSpan(span) + + if len(profiler.spans) != 1 { + t.Errorf("expected 1 span, got %d", len(profiler.spans)) + } + + if profiler.spans[0] != span { + t.Errorf("expected span to be added correctly, got %v", profiler.spans[0]) + } +} + +func TestSpanString(t *testing.T) { + span := &span{ + extra: attr{ + typ: NodeStatic, + success: true, + name: "test-span", + }, + begin: time.Now(), + end: time.Now().Add(10 * time.Millisecond), + } + + expected := "NodeStatic,test-span,cost 10000µs" + actual := span.String() + + if actual != expected { + t.Errorf("expected %s, got %s", expected, actual) + } +} + +func TestProfilerDraw(t *testing.T) { + profiler := NewTracer() + parentSpan := &span{ + extra: attr{ + typ: NodeStatic, + success: true, + name: "parent", + }, + begin: time.Now(), + end: time.Now().Add(10 * time.Millisecond), + } + + childSpan := &span{ + extra: attr{ + typ: NodeStatic, + success: true, + name: "child", + }, + begin: time.Now(), + end: time.Now().Add(5 * time.Millisecond), + parent: parentSpan, + } + + profiler.AddSpan(parentSpan) + profiler.AddSpan(childSpan) + + var buf bytes.Buffer + err := profiler.draw(&buf) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + + output := buf.String() + if len(output) == 0 { + t.Errorf("expected output, got empty string") + } + + expectedOutput := "NodeStatic,parent,cost 10000µs;NodeStatic,child,cost 5000µs 5000\n" + if output != expectedOutput { + t.Errorf("expected output: %s, got: %s", expectedOutput, output) + } +}