From 1d0b139a3a2bf91178423a7d25ee0b29e4d27408 Mon Sep 17 00:00:00 2001 From: Alexey Kostin Date: Sun, 3 Mar 2019 16:17:03 +0300 Subject: [PATCH] Move profiler to main --- ceph-gobench.go | 26 ++++++++++++++++++++++++++ flags.go | 27 --------------------------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/ceph-gobench.go b/ceph-gobench.go index c8c6d5a..24af561 100644 --- a/ceph-gobench.go +++ b/ceph-gobench.go @@ -6,6 +6,9 @@ import ( "github.com/fatih/color" "log" "math/rand" + "os" + "runtime" + "runtime/pprof" "sort" "strconv" "strings" @@ -203,6 +206,29 @@ func BenchThread(cephconn *Cephconnection, osddevice Device, buffs [][]byte, par func main() { params := Route() + if params.cpuprofile != "" { + f, err := os.Create(params.cpuprofile) + if err != nil { + log.Fatal("could not create CPU profile: ", err) + } + defer f.Close() + if err := pprof.StartCPUProfile(f); err != nil { + log.Fatal("could not start CPU profile: ", err) + } + defer pprof.StopCPUProfile() + } + + if params.memprofile != "" { + f, err := os.Create(params.memprofile) + if err != nil { + log.Fatal("could not create memory profile: ", err) + } + defer f.Close() + runtime.GC() // get up-to-date statistics + if err := pprof.WriteHeapProfile(f); err != nil { + log.Fatal("could not write memory profile: ", err) + } + } cephconn := connectioninit(params) defer cephconn.conn.Shutdown() diff --git a/flags.go b/flags.go index 521bad3..119d99f 100644 --- a/flags.go +++ b/flags.go @@ -4,9 +4,6 @@ import ( "code.cloudfoundry.org/bytefmt" "github.com/juju/gnuflag" "log" - "os" - "runtime" - "runtime/pprof" "time" ) @@ -58,30 +55,6 @@ func Route() Params { "Name of memprofile") gnuflag.Parse(true) - if params.cpuprofile != "" { - f, err := os.Create(params.cpuprofile) - if err != nil { - log.Fatal("could not create CPU profile: ", err) - } - defer f.Close() - if err := pprof.StartCPUProfile(f); err != nil { - log.Fatal("could not start CPU profile: ", err) - } - defer pprof.StopCPUProfile() - } - - if params.memprofile != "" { - f, err := os.Create(params.memprofile) - if err != nil { - log.Fatal("could not create memory profile: ", err) - } - defer f.Close() - runtime.GC() // get up-to-date statistics - if err := pprof.WriteHeapProfile(f); err != nil { - log.Fatal("could not write memory profile: ", err) - } - } - blocksize, err := bytefmt.ToBytes(params.bs) params.blocksize = blocksize if err != nil {