Skip to content

Commit

Permalink
新增 Mark 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
eaglexiang committed Dec 13, 2024
1 parent 9af9e43 commit c11b9be
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
10 changes: 10 additions & 0 deletions costwhere.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ func (s *CostWhere) End() (stacks []string) {
return
}

func Mark(ctx *context.Context, topic string) (end func()) {
if ctx == nil {
end = func() {}
return
}
newCtx, end := Begin(*ctx, topic)
*ctx = newCtx
return
}

// StartStack 开始一个栈帧
func Begin(ctx context.Context, topic string) (newCtx context.Context, end func()) {
// 读取父级栈帧
Expand Down
16 changes: 8 additions & 8 deletions tests/01/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,30 @@ func main() {
}

func F0(ctx context.Context) {
ctx, end := costwhere.Begin(ctx, "F0")
defer end()
defer costwhere.Mark(&ctx, "F0")()

time.Sleep(100 * time.Millisecond)
F2(ctx)
}

func F1(ctx context.Context) {
ctx, end := costwhere.Begin(ctx, "F1")
defer end()
defer costwhere.Mark(&ctx, "F1")()

time.Sleep(1 * time.Second)
F2(ctx)
}

func F2(ctx context.Context) {
ctx, end := costwhere.Begin(ctx, "F2")
defer end()
defer costwhere.Mark(&ctx, "F2")()

time.Sleep(100 * time.Millisecond)
F3(ctx)
F3(ctx)
F3(ctx)
}

func F3(ctx context.Context) {
_, end := costwhere.Begin(ctx, "F3")
defer end()
defer costwhere.Mark(&ctx, "F3")()

time.Sleep(300 * time.Millisecond)
}

0 comments on commit c11b9be

Please sign in to comment.