Skip to content

Commit

Permalink
Refactor duplicate code in stat_holder
Browse files Browse the repository at this point in the history
Remove little duplicate logic and unused variable.

Change-Id: Ib1e633578cb13393a6351a7e60cd6f47325c5d09
Signed-off-by: grapebaba <281165273@qq.com>
  • Loading branch information
GrapeBaBa committed Aug 10, 2016
1 parent d48a1c2 commit dd96892
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions core/ledger/perfstat/stat_holder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const commonPrefix = "github.com/hyperledger/fabric/core/ledger"
const commonPrefixLen = len(commonPrefix)

var holder *statsHolder
var once sync.Once
var logger = logging.MustGetLogger("ledger.perfstat")

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

// UpdateTimeStat updates the stats for time spent at a particular point in the code
func UpdateTimeStat(id string, startTime time.Time) {
if !enableStats {
return
}
path := getCallerInfo()
statName := fmt.Sprintf("%s:%s", path, id)
stat := getOrCreateStat(statName, "", 0)
stat.updateDataStat(time.Since(startTime).Nanoseconds())
updateStat(id, time.Since(startTime).Nanoseconds())
}

// UpdateDataStat updates the stats for data at a particular point in the code
func UpdateDataStat(id string, value int64) {
if !enableStats {
return
}
path := getCallerInfo()
statName := fmt.Sprintf("%s:%s", path, id)
stat := getOrCreateStat(statName, "", 0)
stat.updateDataStat(value)
updateStat(id, value)
}

// ResetStats resets all the stats data
Expand All @@ -87,6 +74,37 @@ func ResetStats() {
}
}

// PrintStats prints the stats in the log file.
func PrintStats() {
if !enableStats {
return
}
holder.rwLock.RLock()
defer holder.rwLock.RUnlock()
logger.Info("Stats.......Start")
var paths []string
for k := range holder.m {
paths = append(paths, k)
}
sort.Strings(paths)
for _, k := range paths {
v := holder.m[k]
logger.Info(v.String())
}
logger.Info("Stats.......Finish")
}

func updateStat(id string, value int64) {
if !enableStats {
return
}
path := getCallerInfo()
statName := fmt.Sprintf("%s:%s", path, id)
fmt.Println(statName)
stat := getOrCreateStat(statName, "", 0)
stat.updateDataStat(value)
}

func getOrCreateStat(name string, file string, line int) *stat {
holder.rwLock.RLock()
stat, ok := holder.m[name]
Expand All @@ -113,29 +131,12 @@ func printStatsPeriodically() {
}
}

// PrintStats prints the stats in the log file.
func PrintStats() {
if !enableStats {
return
}
holder.rwLock.RLock()
defer holder.rwLock.RUnlock()
logger.Info("Stats.......Start")
var paths []string
for k := range holder.m {
paths = append(paths, k)
}
sort.Strings(paths)
for _, k := range paths {
v := holder.m[k]
logger.Info(v.String())
}
logger.Info("Stats.......Finish")
}

func getCallerInfo() string {
pc := make([]uintptr, 10)
runtime.Callers(3, pc)
// Note: the default value 4 will ensure stat name exclude the path
// "/perfstat.UpdateTimeStat -> /perfstat.updateStat -> /perfstat.getCallerInfo"
// "/perfstat.UpdateDataStat -> /perfstat.updateStat -> /perfstat.getCallerInfo"
runtime.Callers(4, pc)
var path bytes.Buffer
j := 0
for i := range pc {
Expand Down

0 comments on commit dd96892

Please sign in to comment.