update profiler to be singleton; add metricsReportURL argument.

pull/69/head
Richard Liu 6 years ago
parent 8fe12c2574
commit 3815f3ca14
  1. 10
      benchmark.go
  2. 18
      consensus/consensus_leader.go
  3. 2
      go_executable_build.sh
  4. 48
      profiler/profiler.go

@ -50,6 +50,7 @@ func main() {
attackedMode := flag.Int("attacked_mode", 0, "0 means not attacked, 1 means attacked, 2 means being open to be selected as attacked") attackedMode := flag.Int("attacked_mode", 0, "0 means not attacked, 1 means attacked, 2 means being open to be selected as attacked")
dbSupported := flag.Bool("db_supported", false, "false means not db_supported, true means db_supported") dbSupported := flag.Bool("db_supported", false, "false means not db_supported, true means db_supported")
profile := flag.Bool("profile", false, "Turn on profiling (CPU, Memory).") profile := flag.Bool("profile", false, "Turn on profiling (CPU, Memory).")
metricsReportURL := flag.String("metrics_profile_url", "", "If set, reports metrics to this URL.")
flag.Parse() flag.Parse()
// Set up randomization seed. // Set up randomization seed.
@ -90,9 +91,12 @@ func main() {
consensus := consensus.NewConsensus(*ip, *port, shardID, peers, leader) consensus := consensus.NewConsensus(*ip, *port, shardID, peers, leader)
// Start Profiler for leader if profile argument is on // Start Profiler for leader if profile argument is on
if *profile && role == "leader" { if role == "leader" && (*profile || *metricsReportURL != "") {
profiler := profiler.NewProfiler(consensus.Log, os.Getpid(), shardID) prof := profiler.GetProfiler()
profiler.Start() prof.Config(consensus.Log, shardID, *metricsReportURL)
if *profile {
prof.Start()
}
} }
// Set logger to attack model. // Set logger to attack model.

@ -6,11 +6,12 @@ import (
"encoding/gob" "encoding/gob"
"encoding/hex" "encoding/hex"
"errors" "errors"
"net/http"
"net/url" "net/url"
"strconv" "strconv"
"time" "time"
"github.com/simple-rules/harmony-benchmark/profiler"
"github.com/dedis/kyber" "github.com/dedis/kyber"
"github.com/dedis/kyber/sign/schnorr" "github.com/dedis/kyber/sign/schnorr"
"github.com/simple-rules/harmony-benchmark/blockchain" "github.com/simple-rules/harmony-benchmark/blockchain"
@ -417,14 +418,18 @@ func (consensus *Consensus) reportMetrics(block blockchain.Block) {
"consensus", consensus) "consensus", consensus)
// Post metrics // Post metrics
URL := "http://localhost:3000/report" profiler := profiler.GetProfiler()
if profiler.MetricsReportURL == "" {
return
}
txHashes := []string{} txHashes := []string{}
for i := 1; i <= 3; i++ { for i := 1; i <= 3; i++ {
if len(block.TransactionIds)-i >= 0 { if len(block.TransactionIds)-i >= 0 {
txHashes = append(txHashes, hex.EncodeToString(block.TransactionIds[len(block.TransactionIds)-i][:])) txHashes = append(txHashes, hex.EncodeToString(block.TransactionIds[len(block.TransactionIds)-i][:]))
} }
} }
form := url.Values{ metrics := url.Values{
"key": {consensus.pubKey.String()}, "key": {consensus.pubKey.String()},
"tps": {strconv.FormatFloat(tps, 'f', 2, 64)}, "tps": {strconv.FormatFloat(tps, 'f', 2, 64)},
"txCount": {strconv.Itoa(int(numOfTxs))}, "txCount": {strconv.Itoa(int(numOfTxs))},
@ -433,10 +438,5 @@ func (consensus *Consensus) reportMetrics(block blockchain.Block) {
"latestTxHashes": txHashes, "latestTxHashes": txHashes,
"blockLatency": {strconv.Itoa(int(timeElapsed / time.Millisecond))}, "blockLatency": {strconv.Itoa(int(timeElapsed / time.Millisecond))},
} }
profiler.LogMetrics(metrics)
body := bytes.NewBufferString(form.Encode())
rsp, err := http.Post(URL, "application/x-www-form-urlencoded", body)
if err == nil {
defer rsp.Body.Close()
}
} }

@ -4,7 +4,6 @@ GOOS=linux
GOARCH=amd64 GOARCH=amd64
env GOOS=$GOOS GOARCH=$GOARCH go build -o bin/benchmark benchmark.go env GOOS=$GOOS GOARCH=$GOARCH go build -o bin/benchmark benchmark.go
env GOOS=$GOOS GOARCH=$GOARCH go build -o bin/txgen client/txgen/main.go env GOOS=$GOOS GOARCH=$GOARCH go build -o bin/txgen client/txgen/main.go
env GOOS=$GOOS GOARCH=$GOARCH go build -o bin/profiler profiler/main.go
AWSCLI=aws AWSCLI=aws
if [ "$1" != "" ]; then if [ "$1" != "" ]; then
@ -13,4 +12,3 @@ fi
$AWSCLI s3 cp bin/benchmark s3://unique-bucket-bin/benchmark --acl public-read-write $AWSCLI s3 cp bin/benchmark s3://unique-bucket-bin/benchmark --acl public-read-write
$AWSCLI s3 cp bin/txgen s3://unique-bucket-bin/txgen --acl public-read-write $AWSCLI s3 cp bin/txgen s3://unique-bucket-bin/txgen --acl public-read-write
$AWSCLI s3 cp bin/profiler s3://unique-bucket-bin/profiler --acl public-read-write

@ -1,6 +1,11 @@
package profiler package profiler
import ( import (
"bytes"
"net/http"
"net/url"
"os"
"sync"
"time" "time"
"github.com/shirou/gopsutil/process" "github.com/shirou/gopsutil/process"
@ -8,15 +13,30 @@ import (
) )
type Profiler struct { type Profiler struct {
logger log.Logger // parameters
PID int32 logger log.Logger
ShardID string pid int32
proc *process.Process shardID string
MetricsReportURL string
// Internal
proc *process.Process
} }
func NewProfiler(logger log.Logger, pid int, shardID string) *Profiler { var singleton *Profiler
profiler := Profiler{logger, int32(pid), shardID, nil} var once sync.Once
return &profiler
func GetProfiler() *Profiler {
once.Do(func() {
singleton = &Profiler{}
})
return singleton
}
func (profiler *Profiler) Config(logger log.Logger, shardID string, metricsReportURL string) {
profiler.logger = logger
profiler.pid = int32(os.Getpid())
profiler.shardID = shardID
profiler.MetricsReportURL = metricsReportURL
} }
func (profiler *Profiler) LogMemory() { func (profiler *Profiler) LogMemory() {
@ -24,7 +44,7 @@ func (profiler *Profiler) LogMemory() {
// log mem usage // log mem usage
info, _ := profiler.proc.MemoryInfo() info, _ := profiler.proc.MemoryInfo()
memMap, _ := profiler.proc.MemoryMaps(false) memMap, _ := profiler.proc.MemoryMaps(false)
profiler.logger.Info("Mem Report", "info", info, "map", memMap, "shardID", profiler.ShardID) profiler.logger.Info("Mem Report", "info", info, "map", memMap, "shardID", profiler.shardID)
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
} }
@ -35,14 +55,22 @@ func (profiler *Profiler) LogCPU() {
// log cpu usage // log cpu usage
percent, _ := profiler.proc.CPUPercent() percent, _ := profiler.proc.CPUPercent()
times, _ := profiler.proc.Times() times, _ := profiler.proc.Times()
profiler.logger.Info("CPU Report", "percent", percent, "times", times, "shardID", profiler.ShardID) profiler.logger.Info("CPU Report", "percent", percent, "times", times, "shardID", profiler.shardID)
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
} }
} }
func (profiler *Profiler) LogMetrics(metrics url.Values) {
body := bytes.NewBufferString(metrics.Encode())
rsp, err := http.Post(profiler.MetricsReportURL, "application/x-www-form-urlencoded", body)
if err == nil {
defer rsp.Body.Close()
}
}
func (profiler *Profiler) Start() { func (profiler *Profiler) Start() {
profiler.proc, _ = process.NewProcess(profiler.PID) profiler.proc, _ = process.NewProcess(profiler.pid)
go profiler.LogCPU() go profiler.LogCPU()
go profiler.LogMemory() go profiler.LogMemory()
} }

Loading…
Cancel
Save