From 782e6cdb7075b3db1be82c2353d0b7b7af0c5dfe Mon Sep 17 00:00:00 2001 From: noneback Date: Wed, 2 Oct 2024 01:15:30 +0800 Subject: [PATCH] doc: add example about profiler and visualizer --- profiler.go | 4 ++-- readme.md | 10 ++++++---- utils/utils_test.go | 17 +++-------------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/profiler.go b/profiler.go index d629447..e17d0e8 100644 --- a/profiler.go +++ b/profiler.go @@ -47,7 +47,7 @@ type span struct { } func (s *span) String() string { - return fmt.Sprintf("%s,%s,cost %vns", s.extra.typ, s.extra.name, s.end.Sub(s.begin).Microseconds()) + return fmt.Sprintf("%s,%s,%vns", s.extra.typ, s.extra.name, s.end.Sub(s.begin).Nanoseconds()) } func (t *Profiler) draw(w io.Writer) error { @@ -61,7 +61,7 @@ func (t *Profiler) draw(w io.Writer) error { path = cur.parent.String() + ";" + path cur = cur.parent } - msg := fmt.Sprintf("%s %v\n", path, s.end.Sub(s.begin).Microseconds()) + msg := fmt.Sprintf("%s %v\n", path, s.end.Sub(s.begin).Nanoseconds()) if _, err := w.Write([]byte(msg)); err != nil { return fmt.Errorf("write profile -> %w", err) diff --git a/readme.md b/readme.md index 0b8fe6c..ee4a1c6 100644 --- a/readme.md +++ b/readme.md @@ -100,20 +100,22 @@ func main() { } } ``` -### How to use visualize taskflow +### How to visualize a taskflow ```go if err := gotaskflow.Visualizer.Visualize(tf, os.Stdout); err != nil { log.Fatal(err) } ``` -`Visualize` generate raw string in dot format, just use dot to draw a DAG svg. -### How to use profile taskflow +`Visualize` generate raw string in dot format, just use `dot` to draw a DAG svg. +![example](https://raw.githubusercontent.com/noneback/images/00fb2d98005ff9213a2003e915caf9426725dcbc/graphviz.svg) +### How to profile a taskflow ```go if err :=exector.Profile(os.Stdout);err != nil { log.Fatal(err) } ``` -`Profile` alse generate raw string in flamegraph format, just use flamegraph to draw a flamegraph svg. +`Profile` alse generate raw string in flamegraph format, just use `flamegraph` to draw a flamegraph svg. +![example](https://raw.githubusercontent.com/noneback/images/ae31f3ea57f3f1b8d4cf94300a5ff502b2340214/t.svg) ## What's next - [ ] Taskflow Composition - [x] Taskflow Profiler diff --git a/utils/utils_test.go b/utils/utils_test.go index 635ff42..dac1629 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -4,23 +4,19 @@ import ( "testing" ) -// UnsafeToString 测试 + func TestUnsafeToString(t *testing.T) { original := "Hello, World!" b := []byte(original) - - // 将字节切片转换为字符串 s := UnsafeToString(b) - if s != original { t.Errorf("Expected %q but got %q", original, s) } } -// UnsafeToBytes 测试 + func TestUnsafeToBytes(t *testing.T) { original := "Hello, World!" - // 将字符串转换为字节切片 b := UnsafeToBytes(original) if string(b) != original { @@ -28,37 +24,31 @@ func TestUnsafeToBytes(t *testing.T) { } } -// RC 结构体测试 func TestRC(t *testing.T) { rc := &RC{} - // 测试初始值 if rc.Value() != 0 { t.Errorf("Expected count to be 0, got %d", rc.Value()) } - // 测试增加计数 rc.Increase() if rc.Value() != 1 { t.Errorf("Expected count to be 1, got %d", rc.Value()) } - // 测试减少计数 rc.Decrease() if rc.Value() != 0 { t.Errorf("Expected count to be 0, got %d", rc.Value()) } - // 测试不能减少到负值 defer func() { if r := recover(); r == nil { t.Errorf("Expected panic when decreasing below zero, but did not") } }() - rc.Decrease() // 这里应该触发 panic + rc.Decrease() // should panic } -// 测试 Set 和负值 func TestSet(t *testing.T) { rc := &RC{} rc.Set(5) @@ -67,7 +57,6 @@ func TestSet(t *testing.T) { t.Errorf("Expected count to be 5, got %d", rc.Value()) } - // 测试负值情况 rc.Set(-1) if rc.Value() != -1 {