-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from sasile/master
fix ingest worker + latency hist
- Loading branch information
Showing
5 changed files
with
116 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package histogram | ||
|
||
import ( | ||
"time" | ||
"sync" | ||
log "github.com/sirupsen/logrus" | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
type LatencyHist struct { | ||
ch_values chan time.Duration | ||
hist map[int]int | ||
count int64 | ||
wg sync.WaitGroup | ||
} | ||
|
||
|
||
func (self *LatencyHist) Add(v time.Duration) { | ||
log.Debugln("values added") | ||
self.ch_values <- v | ||
} | ||
|
||
func (self *LatencyHist) place(v float64) { | ||
self.hist[int(v/100)]++ | ||
} | ||
|
||
func (self *LatencyHist)New()chan time.Duration { | ||
log.Debugln("new latency hist") | ||
self.hist = make(map[int]int) | ||
self.wg.Add(1) | ||
|
||
self.ch_values = make(chan time.Duration, 10000) | ||
go func() { | ||
defer self.wg.Done() | ||
for v := range self.ch_values { | ||
self.count++ | ||
self.place(float64(v.Nanoseconds() / 1000)) | ||
} | ||
}() | ||
return self.ch_values | ||
} | ||
|
||
func (self *LatencyHist) GetResults() ([]string, []float64) { | ||
log.Debugln("get latency hist") | ||
self.wg.Wait() | ||
var keys []int | ||
for k := range self.hist { | ||
keys = append(keys, k) | ||
} | ||
sort.Ints(keys) | ||
log.Debugln("latency hist wait released") | ||
res_strings := [] string{} | ||
res_values := [] float64{} | ||
for _,k := range keys{ | ||
v := self.hist[k] | ||
res_strings = append(res_strings, fmt.Sprintf("%5d - %5d", | ||
k*100, (k+1)*100) ) | ||
value := float64(v * 100) / float64(self.count) | ||
res_values = append(res_values,value) | ||
} | ||
return res_strings, res_values | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package histogram | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
"math/rand" | ||
) | ||
|
||
func TestLatencyHist_Get(t *testing.T) { | ||
l := LatencyHist{} | ||
c:=l.New() | ||
req:= 1000000 | ||
|
||
go func() { | ||
for i := 0; i < req; i++ { | ||
l.Add(time.Microsecond * time.Duration(rand.Intn(2000))) | ||
|
||
} | ||
close(c) | ||
}() | ||
|
||
s,v:= l.GetResults() | ||
total:= float64(0) | ||
for i,_ := range s{ | ||
total+=v[i] | ||
t.Logf("%6v(us)\t\t%3.2f%%", s[i], v[i]) | ||
} | ||
t.Logf("Total: %3.3f", total) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters