mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-13 18:00:00 +08:00
chore: optimize profile center and remove tablewriter dependency
This commit is contained in:
@@ -1,13 +1,12 @@
|
|||||||
package prof
|
package prof
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"fmt"
|
||||||
"strconv"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/olekukonko/tablewriter"
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
"github.com/zeromicro/go-zero/core/threading"
|
"github.com/zeromicro/go-zero/core/threading"
|
||||||
)
|
)
|
||||||
@@ -28,46 +27,15 @@ type (
|
|||||||
|
|
||||||
const flushInterval = 5 * time.Minute
|
const flushInterval = 5 * time.Minute
|
||||||
|
|
||||||
var (
|
var pc = &profileCenter{
|
||||||
pc = &profileCenter{
|
|
||||||
slots: make(map[string]*profileSlot),
|
slots: make(map[string]*profileSlot),
|
||||||
}
|
}
|
||||||
once sync.Once
|
|
||||||
)
|
|
||||||
|
|
||||||
func report(name string, duration time.Duration) {
|
func init() {
|
||||||
updated := func() bool {
|
flushRepeatedly()
|
||||||
pc.lock.RLock()
|
|
||||||
defer pc.lock.RUnlock()
|
|
||||||
|
|
||||||
slot, ok := pc.slots[name]
|
|
||||||
if ok {
|
|
||||||
atomic.AddInt64(&slot.lifecount, 1)
|
|
||||||
atomic.AddInt64(&slot.lastcount, 1)
|
|
||||||
atomic.AddInt64(&slot.lifecycle, int64(duration))
|
|
||||||
atomic.AddInt64(&slot.lastcycle, int64(duration))
|
|
||||||
}
|
|
||||||
return ok
|
|
||||||
}()
|
|
||||||
|
|
||||||
if !updated {
|
|
||||||
func() {
|
|
||||||
pc.lock.Lock()
|
|
||||||
defer pc.lock.Unlock()
|
|
||||||
|
|
||||||
pc.slots[name] = &profileSlot{
|
|
||||||
lifecount: 1,
|
|
||||||
lastcount: 1,
|
|
||||||
lifecycle: int64(duration),
|
|
||||||
lastcycle: int64(duration),
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
once.Do(flushRepeatly)
|
func flushRepeatedly() {
|
||||||
}
|
|
||||||
|
|
||||||
func flushRepeatly() {
|
|
||||||
threading.GoSafe(func() {
|
threading.GoSafe(func() {
|
||||||
for {
|
for {
|
||||||
time.Sleep(flushInterval)
|
time.Sleep(flushInterval)
|
||||||
@@ -76,42 +44,64 @@ func flushRepeatly() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func report(name string, duration time.Duration) {
|
||||||
|
slot := loadOrStoreSlot(name, duration)
|
||||||
|
|
||||||
|
atomic.AddInt64(&slot.lifecount, 1)
|
||||||
|
atomic.AddInt64(&slot.lastcount, 1)
|
||||||
|
atomic.AddInt64(&slot.lifecycle, int64(duration))
|
||||||
|
atomic.AddInt64(&slot.lastcycle, int64(duration))
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadOrStoreSlot(name string, duration time.Duration) *profileSlot {
|
||||||
|
pc.lock.RLock()
|
||||||
|
slot, ok := pc.slots[name]
|
||||||
|
pc.lock.RUnlock()
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
return slot
|
||||||
|
}
|
||||||
|
|
||||||
|
pc.lock.Lock()
|
||||||
|
defer pc.lock.Unlock()
|
||||||
|
|
||||||
|
// double-check
|
||||||
|
if slot, ok = pc.slots[name]; ok {
|
||||||
|
return slot
|
||||||
|
}
|
||||||
|
|
||||||
|
slot = &profileSlot{}
|
||||||
|
pc.slots[name] = slot
|
||||||
|
return slot
|
||||||
|
}
|
||||||
|
|
||||||
func generateReport() string {
|
func generateReport() string {
|
||||||
var buffer bytes.Buffer
|
var builder strings.Builder
|
||||||
buffer.WriteString("Profiling report\n")
|
builder.WriteString("Profiling report\n")
|
||||||
var data [][]string
|
builder.WriteString("QUEUE,LIFECOUNT,LIFECYCLE,LASTCOUNT,LASTCYCLE\n")
|
||||||
|
|
||||||
calcFn := func(total, count int64) string {
|
calcFn := func(total, count int64) string {
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
return "-"
|
return "-"
|
||||||
}
|
}
|
||||||
|
|
||||||
return (time.Duration(total) / time.Duration(count)).String()
|
return (time.Duration(total) / time.Duration(count)).String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func() {
|
|
||||||
pc.lock.Lock()
|
pc.lock.Lock()
|
||||||
defer pc.lock.Unlock()
|
|
||||||
|
|
||||||
for key, slot := range pc.slots {
|
for key, slot := range pc.slots {
|
||||||
data = append(data, []string{
|
builder.WriteString(fmt.Sprintf("%s,%d,%s,%d,%s\n",
|
||||||
key,
|
key,
|
||||||
strconv.FormatInt(slot.lifecount, 10),
|
slot.lifecount,
|
||||||
calcFn(slot.lifecycle, slot.lifecount),
|
calcFn(slot.lifecycle, slot.lifecount),
|
||||||
strconv.FormatInt(slot.lastcount, 10),
|
slot.lastcount,
|
||||||
calcFn(slot.lastcycle, slot.lastcount),
|
calcFn(slot.lastcycle, slot.lastcount),
|
||||||
})
|
))
|
||||||
|
|
||||||
// reset the data for last cycle
|
// reset last cycle stats
|
||||||
slot.lastcount = 0
|
atomic.StoreInt64(&slot.lastcount, 0)
|
||||||
slot.lastcycle = 0
|
atomic.StoreInt64(&slot.lastcycle, 0)
|
||||||
}
|
}
|
||||||
}()
|
pc.lock.Unlock()
|
||||||
|
|
||||||
table := tablewriter.NewWriter(&buffer)
|
return builder.String()
|
||||||
table.SetHeader([]string{"QUEUE", "LIFECOUNT", "LIFECYCLE", "LASTCOUNT", "LASTCYCLE"})
|
|
||||||
table.SetBorder(false)
|
|
||||||
table.AppendBulk(data)
|
|
||||||
table.Render()
|
|
||||||
|
|
||||||
return buffer.String()
|
|
||||||
}
|
}
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -14,7 +14,6 @@ require (
|
|||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/jackc/pgx/v5 v5.7.4
|
github.com/jackc/pgx/v5 v5.7.4
|
||||||
github.com/jhump/protoreflect v1.17.0
|
github.com/jhump/protoreflect v1.17.0
|
||||||
github.com/olekukonko/tablewriter v0.0.5
|
|
||||||
github.com/pelletier/go-toml/v2 v2.2.2
|
github.com/pelletier/go-toml/v2 v2.2.2
|
||||||
github.com/prometheus/client_golang v1.21.1
|
github.com/prometheus/client_golang v1.21.1
|
||||||
github.com/redis/go-redis/v9 v9.8.0
|
github.com/redis/go-redis/v9 v9.8.0
|
||||||
|
|||||||
3
go.sum
3
go.sum
@@ -121,7 +121,6 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
|
|||||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
|
||||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
@@ -135,8 +134,6 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
|
|||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||||
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
|
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
|
||||||
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
|
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
|
||||||
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
|
||||||
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
|
|
||||||
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
|
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
|
||||||
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
|
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
|
||||||
github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg=
|
github.com/onsi/gomega v1.29.0 h1:KIA/t2t5UBzoirT4H9tsML45GEbo3ouUnBHsCfD2tVg=
|
||||||
|
|||||||
Reference in New Issue
Block a user