scope.go 675 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package perf
  2. import (
  3. "runtime"
  4. )
  5. func ScopeTimer(opts ...timerOpt) func() {
  6. t := NewTimer(CallerName(1))
  7. return func() { t.Mark("returned") }
  8. }
  9. func ScopeTimerOk(ok *bool) func() {
  10. t := NewTimer(CallerName(1))
  11. return func() { t.MarkOk(*ok) }
  12. }
  13. func ScopeTimerErr(err *error) func() {
  14. t := NewTimer(CallerName(1))
  15. return func() {
  16. r := recover()
  17. if r != nil {
  18. t.Mark("panic")
  19. panic(r)
  20. }
  21. t.MarkErr(*err)
  22. }
  23. }
  24. func CallerName(skip int) timerOpt {
  25. return Name(getCallerName(skip))
  26. }
  27. func getCallerName(skip int) string {
  28. var pc [1]uintptr
  29. runtime.Callers(3+skip, pc[:])
  30. fs := runtime.CallersFrames(pc[:])
  31. f, _ := fs.Next()
  32. return f.Func.Name()
  33. }