File: scope.go

package info (click to toggle)
golang-github-anacrolix-missinggo 2.1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 872 kB
  • sloc: makefile: 4
file content (39 lines) | stat: -rw-r--r-- 675 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package perf

import (
	"runtime"
)

func ScopeTimer(opts ...timerOpt) func() {
	t := NewTimer(CallerName(1))
	return func() { t.Mark("returned") }
}

func ScopeTimerOk(ok *bool) func() {
	t := NewTimer(CallerName(1))
	return func() { t.MarkOk(*ok) }
}

func ScopeTimerErr(err *error) func() {
	t := NewTimer(CallerName(1))
	return func() {
		r := recover()
		if r != nil {
			t.Mark("panic")
			panic(r)
		}
		t.MarkErr(*err)
	}
}

func CallerName(skip int) timerOpt {
	return Name(getCallerName(skip))
}

func getCallerName(skip int) string {
	var pc [1]uintptr
	runtime.Callers(3+skip, pc[:])
	fs := runtime.CallersFrames(pc[:])
	f, _ := fs.Next()
	return f.Func.Name()
}