|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "runtime" |
| 8 | + "time" |
| 9 | + |
| 10 | + gotaskflow "github.com/noneback/go-taskflow" |
| 11 | +) |
| 12 | + |
| 13 | +// This example demonstrates how to capture Chrome Trace Event data |
| 14 | +// using WithTracer(). The output can be visualized in chrome://tracing |
| 15 | +// or Perfetto UI (https://ui.perfetto.dev/). |
| 16 | +func main() { |
| 17 | + executor := gotaskflow.NewExecutor(uint(runtime.NumCPU()*4), gotaskflow.WithTracer()) |
| 18 | + |
| 19 | + tf := gotaskflow.NewTaskFlow("tracing-demo") |
| 20 | + |
| 21 | + // Stage 1: parallel fetch and load |
| 22 | + fetch := tf.NewTask("fetch", func() { |
| 23 | + fmt.Println("fetch") |
| 24 | + time.Sleep(10 * time.Millisecond) |
| 25 | + }) |
| 26 | + load := tf.NewTask("load", func() { |
| 27 | + fmt.Println("load") |
| 28 | + time.Sleep(8 * time.Millisecond) |
| 29 | + }) |
| 30 | + |
| 31 | + // Stage 2: process depends on both fetch and load |
| 32 | + process := tf.NewSubflow("process", func(sf *gotaskflow.Subflow) { |
| 33 | + transform := sf.NewTask("transform", func() { |
| 34 | + fmt.Println("transform") |
| 35 | + time.Sleep(5 * time.Millisecond) |
| 36 | + }) |
| 37 | + enrich := sf.NewTask("enrich", func() { |
| 38 | + fmt.Println("enrich") |
| 39 | + time.Sleep(4 * time.Millisecond) |
| 40 | + }) |
| 41 | + transform.Precede(enrich) |
| 42 | + }) |
| 43 | + |
| 44 | + // Stage 3: output waits for process to complete |
| 45 | + output := tf.NewTask("output", func() { |
| 46 | + fmt.Println("output") |
| 47 | + time.Sleep(3 * time.Millisecond) |
| 48 | + }) |
| 49 | + |
| 50 | + fetch.Precede(process) |
| 51 | + load.Precede(process) |
| 52 | + process.Precede(output) |
| 53 | + |
| 54 | + executor.Run(tf).Wait() |
| 55 | + |
| 56 | + fmt.Println("--- Chrome Trace JSON (open in chrome://tracing or https://ui.perfetto.dev) ---") |
| 57 | + if err := executor.Trace(os.Stdout); err != nil { |
| 58 | + log.Fatal(err) |
| 59 | + } |
| 60 | +} |
0 commit comments