Skip to content

Commit

Permalink
Merge pull request #888 from ydb-platform/stack-record
Browse files Browse the repository at this point in the history
refactoring of stack.Record
  • Loading branch information
asmyasnikov authored Nov 8, 2023
2 parents 36f3718 + 204a981 commit 3d4ec7a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
21 changes: 19 additions & 2 deletions internal/stack/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,16 @@ func PackagePath(b bool) recordOption {
}
}

func Record(depth int, opts ...recordOption) string {
type call func() (function uintptr, file string, line int)

func Call(depth int) call {
return func() (function uintptr, file string, line int) {
function, file, line, _ = runtime.Caller(depth + 2)
return function, file, line
}
}

func (call call) Record(opts ...recordOption) string {
optionsHolder := recordOptions{
packagePath: true,
packageName: true,
Expand All @@ -75,7 +84,7 @@ func Record(depth int, opts ...recordOption) string {
for _, opt := range opts {
opt(&optionsHolder)
}
function, file, line, _ := runtime.Caller(depth + 1)
function, file, line := call()
name := runtime.FuncForPC(function).Name()
var (
pkgPath string
Expand Down Expand Up @@ -155,3 +164,11 @@ func Record(depth int, opts ...recordOption) string {
}
return buffer.String()
}

func (call call) FunctionID() string {
return call.Record(Lambda(false), FileName(false))
}

func Record(depth int, opts ...recordOption) string {
return Call(depth + 1).Record(opts...)
}
28 changes: 28 additions & 0 deletions internal/stack/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,31 @@ func TestRecord(t *testing.T) {
})
}
}

func BenchmarkCall(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = Call(0)
}
}

func BenchmarkRecord(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = Record(0)
}
}

func BenchmarkCallRecord(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = Call(0).Record()
}
}

func BenchmarkCallFuncionID(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = Call(0).FunctionID()
}
}

0 comments on commit 3d4ec7a

Please sign in to comment.