From ed0c72c36f316b81dcdda6a1d3616e8e486777f4 Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Fri, 13 Jul 2018 18:09:32 -0700 Subject: [PATCH] use gopsutil --- benchmark.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/benchmark.go b/benchmark.go index ed7df08dc..bf1744a3c 100644 --- a/benchmark.go +++ b/benchmark.go @@ -15,6 +15,8 @@ import ( "runtime" "strings" "time" + + "github.com/shirou/gopsutil/process" ) const ( @@ -102,6 +104,21 @@ func logMemUsage(consensus *consensus.Consensus) { } } +// TODO: @ricl, start another process for reporting. +func logCPUUsage(consensus *consensus.Consensus) { + log.Info("PPID", "ppid", os.Getpid()) + p, _ := process.NewProcess(int32(os.Getpid())) + for { + percent, _ := p.CPUPercent() + times, err := p.Times() + if err != nil { + log.Info("ERR", "Err", err) + } + log.Info("CPU Report", "Percent", percent, "Times", times, "consensus", consensus) + time.Sleep(10 * time.Second) + } +} + func main() { ip := flag.String("ip", "127.0.0.1", "IP of the node") port := flag.String("port", "9000", "port of the node.") @@ -140,7 +157,10 @@ func main() { // Consensus object. consensus := consensus.NewConsensus(*ip, *port, shardID, peers, leader) // Logging for consensus. + log.Info("PID", "pid", os.Getpid()) go logMemUsage(consensus) + go logCPUUsage(consensus) + // Set logger to attack model. attack.GetInstance().SetLogger(consensus.Log) // Current node.