Skip to content

Commit

Permalink
fix the STDEV func and help with the historgam
Browse files Browse the repository at this point in the history
  • Loading branch information
lza@arthur committed Jun 25, 2017
1 parent bac618b commit 4038700
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 44 deletions.
15 changes: 0 additions & 15 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,6 @@ func WithBinsNumber(i int) Options {
}
}

func WithGrowthFactor(i float64) Options {
return func(o *Config) {
o.GrowthFactor = i
}
}
func WithBaseBucketSize(i float64) Options {
return func(o *Config) {
o.BaseBucketSize = i
}
}
func WithMinValue(i int64) Options {
return func(o *Config) {
o.MinValue = i
}
}
func WithNoPrint() Options {
return func(o *Config) {
o.NoPrint = true
Expand Down
9 changes: 3 additions & 6 deletions demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ import (
)

func main() {

perfm := perfm.New(perfm.WithBinsNumber(15), perfm.WithMinValue(0), perfm.WithGrowthFactor(0.4), perfm.WithBaseBucketSize(20), perfm.WithParallel(5))
//perfm := perfm.New(perfm.WithBinsNumber(15), perfm.WithParallel(5), perfm.WithNumber(100))

perfm.Registe(func() error {
_, err := http.Get("http://www.baidu.com")
perfm := perfm.New(perfm.WithBinsNumber(15), perfm.WithParallel(1), perfm.WithDuration(3))
perfm.Registe(func() (err error) {
_, err = http.Get("https://www.baudu.com")
return err
})
perfm.Start()
Expand Down
39 changes: 16 additions & 23 deletions perfm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

hist "github.com/VividCortex/gohistogram"
//hist "github.com/shafreeck/fperf/stats"
)

//PerfMonitor define the atcion about perfmonitor
Expand Down Expand Up @@ -45,13 +44,6 @@ type perfmonitor struct {
func New(options ...Options) PerfMonitor {
conf := newConfig(options...)

//histopt := hist.HistogramOptions{
// NumBuckets: conf.BinsNumber,
// GrowthFactor: conf.GrowthFactor,
// BaseBucketSize: conf.BaseBucketSize,
// MinValue: conf.MinValue,
//}

var p *perfmonitor = &perfmonitor{
done: make(chan int, 0),
workers: conf.Parallel,
Expand All @@ -61,11 +53,10 @@ func New(options ...Options) PerfMonitor {
timer: time.Tick(time.Second * time.Duration(conf.Frequency)),
collector: make(chan time.Duration, conf.BufferSize),
errCollector: make(chan error, conf.BufferSize),
//histogram: hist.NewHistogram(histopt),
histogram: hist.NewHistogram(conf.BinsNumber),
buffer: make(chan int64, 100000000),
noPrint: conf.NoPrint,
wg: sync.WaitGroup{},
histogram: hist.NewHistogram(conf.BinsNumber),
buffer: make(chan int64, 100000000),
noPrint: conf.NoPrint,
wg: sync.WaitGroup{},
}
return p
}
Expand All @@ -92,7 +83,6 @@ func (p *perfmonitor) Start() {
p.localTimeCount += cost
p.buffer <- int64(cost)
case <-p.timer:
fmt.Println("DBG in print")
if p.localCount == 0 {
continue
}
Expand All @@ -101,7 +91,6 @@ func (p *perfmonitor) Start() {
p.localTimeCount = 0
case <-p.done:
localwg.Wait()
fmt.Println("DBG wati for workers")
for {
cost = <-p.collector
p.buffer <- int64(cost)
Expand Down Expand Up @@ -149,14 +138,13 @@ func (p *perfmonitor) Start() {
return
}
// 500us sleep time will cost 7% cpu time
// 1ms sleep time cost only 4% cpu time that could be a better choice
// 1ms sleep time cost only 4% cpu time that could be a better choice
time.Sleep(time.Millisecond)
}
}()
} else {
// in test duration module
go func() {
fmt.Println("DBG in duration stoper")
p.wg.Done()
time.Sleep(time.Second * time.Duration(p.duration))
close(p.done)
Expand All @@ -167,22 +155,27 @@ func (p *perfmonitor) Start() {

func (p *perfmonitor) Wait() {
p.wg.Wait()
var sum2, i, d int64
fmt.Println("DBG BEFORE DRAW", len(p.buffer), p.Total)
var sum2, i, d, max, min int64
min = 0x7fffffffffffffff
for i = 0; i < p.Total; i++ {
d = <-p.buffer
//fmt.Println("DBG add", d)
p.histogram.Add(float64(d))
p.Sum += d
sum2 += d * d
if d > max {
max = d
}
if d < min {
min = d
}
}
fmt.Println("DBG after caculate")

p.Avg = p.Sum / p.Total
p.Stdev = math.Sqrt(float64(sum2) - 2*float64(p.Avg*p.Sum) + float64(p.Total*p.Avg*p.Avg)/float64(p.Total))
p.Stdev = math.Sqrt((float64(sum2) - 2*float64(p.Avg*p.Sum) + float64(p.Total*p.Avg*p.Avg)) / float64(p.Total))

// here show the histogram
if !p.noPrint {
fmt.Printf("\nSTDEV: %f CV: %f % \n%s\n", p.Stdev, p.Stdev/float64(p.Avg)*100, p.histogram.String())
fmt.Printf("\nMAX: %.3vms MIN: %.3vms STDEV: %.3fms CV: %.3f %% ", max/1000000, min/1000000, p.Stdev/1000000, p.Stdev/float64(p.Avg)*100)
fmt.Println(p.histogram)
}
}

0 comments on commit 4038700

Please sign in to comment.