diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2024-12-20 12:17:14 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-20 12:17:14 +0800 |
commit | 52b319bc00712da095ee4121b616be232b1e455b (patch) | |
tree | e509235780bab858e43023247f82d1c956c71bf1 /modules/util | |
parent | c66de245c4603c8426273f98af1a0c4d2855a677 (diff) | |
download | gitea-52b319bc00712da095ee4121b616be232b1e455b.tar.gz gitea-52b319bc00712da095ee4121b616be232b1e455b.zip |
Refactor pprof labels and process desc (#32909)
* Deprecate "gopid" in log, it is not useful and requires very hacky
approach
* Remove "git.Command.SetDescription" because it is not useful and only
makes the logs too flexible
Diffstat (limited to 'modules/util')
-rw-r--r-- | modules/util/runtime.go | 13 | ||||
-rw-r--r-- | modules/util/runtime_test.go | 32 |
2 files changed, 45 insertions, 0 deletions
diff --git a/modules/util/runtime.go b/modules/util/runtime.go new file mode 100644 index 0000000000..91ec3c869c --- /dev/null +++ b/modules/util/runtime.go @@ -0,0 +1,13 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package util + +import "runtime" + +func CallerFuncName(skip int) string { + pc := make([]uintptr, 1) + runtime.Callers(skip+1, pc) + funcName := runtime.FuncForPC(pc[0]).Name() + return funcName +} diff --git a/modules/util/runtime_test.go b/modules/util/runtime_test.go new file mode 100644 index 0000000000..20f9063b0b --- /dev/null +++ b/modules/util/runtime_test.go @@ -0,0 +1,32 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package util + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCallerFuncName(t *testing.T) { + s := CallerFuncName(1) + assert.Equal(t, "code.gitea.io/gitea/modules/util.TestCallerFuncName", s) +} + +func BenchmarkCallerFuncName(b *testing.B) { + // BenchmarkCaller/sprintf-12 12744829 95.49 ns/op + b.Run("sprintf", func(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = fmt.Sprintf("aaaaaaaaaaaaaaaa %s %s %s", "bbbbbbbbbbbbbbbbbbb", b.Name(), "ccccccccccccccccccccc") + } + }) + // BenchmarkCaller/caller-12 10625133 113.6 ns/op + // It is almost as fast as fmt.Sprintf + b.Run("caller", func(b *testing.B) { + for i := 0; i < b.N; i++ { + CallerFuncName(1) + } + }) +} |