Skip to content

Commit dd96892

Browse files
committed
Refactor duplicate code in stat_holder
Remove little duplicate logic and unused variable. Change-Id: Ib1e633578cb13393a6351a7e60cd6f47325c5d09 Signed-off-by: grapebaba <[email protected]>
1 parent d48a1c2 commit dd96892

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

core/ledger/perfstat/stat_holder.go

+37-36
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const commonPrefix = "github.com/hyperledger/fabric/core/ledger"
3535
const commonPrefixLen = len(commonPrefix)
3636

3737
var holder *statsHolder
38-
var once sync.Once
3938
var logger = logging.MustGetLogger("ledger.perfstat")
4039

4140
type statsHolder struct {
@@ -55,24 +54,12 @@ func init() {
5554

5655
// UpdateTimeStat updates the stats for time spent at a particular point in the code
5756
func UpdateTimeStat(id string, startTime time.Time) {
58-
if !enableStats {
59-
return
60-
}
61-
path := getCallerInfo()
62-
statName := fmt.Sprintf("%s:%s", path, id)
63-
stat := getOrCreateStat(statName, "", 0)
64-
stat.updateDataStat(time.Since(startTime).Nanoseconds())
57+
updateStat(id, time.Since(startTime).Nanoseconds())
6558
}
6659

6760
// UpdateDataStat updates the stats for data at a particular point in the code
6861
func UpdateDataStat(id string, value int64) {
69-
if !enableStats {
70-
return
71-
}
72-
path := getCallerInfo()
73-
statName := fmt.Sprintf("%s:%s", path, id)
74-
stat := getOrCreateStat(statName, "", 0)
75-
stat.updateDataStat(value)
62+
updateStat(id, value)
7663
}
7764

7865
// ResetStats resets all the stats data
@@ -87,6 +74,37 @@ func ResetStats() {
8774
}
8875
}
8976

77+
// PrintStats prints the stats in the log file.
78+
func PrintStats() {
79+
if !enableStats {
80+
return
81+
}
82+
holder.rwLock.RLock()
83+
defer holder.rwLock.RUnlock()
84+
logger.Info("Stats.......Start")
85+
var paths []string
86+
for k := range holder.m {
87+
paths = append(paths, k)
88+
}
89+
sort.Strings(paths)
90+
for _, k := range paths {
91+
v := holder.m[k]
92+
logger.Info(v.String())
93+
}
94+
logger.Info("Stats.......Finish")
95+
}
96+
97+
func updateStat(id string, value int64) {
98+
if !enableStats {
99+
return
100+
}
101+
path := getCallerInfo()
102+
statName := fmt.Sprintf("%s:%s", path, id)
103+
fmt.Println(statName)
104+
stat := getOrCreateStat(statName, "", 0)
105+
stat.updateDataStat(value)
106+
}
107+
90108
func getOrCreateStat(name string, file string, line int) *stat {
91109
holder.rwLock.RLock()
92110
stat, ok := holder.m[name]
@@ -113,29 +131,12 @@ func printStatsPeriodically() {
113131
}
114132
}
115133

116-
// PrintStats prints the stats in the log file.
117-
func PrintStats() {
118-
if !enableStats {
119-
return
120-
}
121-
holder.rwLock.RLock()
122-
defer holder.rwLock.RUnlock()
123-
logger.Info("Stats.......Start")
124-
var paths []string
125-
for k := range holder.m {
126-
paths = append(paths, k)
127-
}
128-
sort.Strings(paths)
129-
for _, k := range paths {
130-
v := holder.m[k]
131-
logger.Info(v.String())
132-
}
133-
logger.Info("Stats.......Finish")
134-
}
135-
136134
func getCallerInfo() string {
137135
pc := make([]uintptr, 10)
138-
runtime.Callers(3, pc)
136+
// Note: the default value 4 will ensure stat name exclude the path
137+
// "/perfstat.UpdateTimeStat -> /perfstat.updateStat -> /perfstat.getCallerInfo"
138+
// "/perfstat.UpdateDataStat -> /perfstat.updateStat -> /perfstat.getCallerInfo"
139+
runtime.Callers(4, pc)
139140
var path bytes.Buffer
140141
j := 0
141142
for i := range pc {

0 commit comments

Comments
 (0)