Skip to content

Commit

Permalink
doc: add example about profiler and visualizer
Browse files Browse the repository at this point in the history
  • Loading branch information
noneback committed Oct 1, 2024
1 parent f30bb23 commit 782e6cd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
4 changes: 2 additions & 2 deletions profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 3 additions & 14 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,51 @@ 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 {
t.Errorf("Expected %q but got %q", original, string(b))
}
}

// 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)
Expand All @@ -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 {
Expand Down

0 comments on commit 782e6cd

Please sign in to comment.