mirror of
https://github.com/DataDog/go-profiler-notes.git
synced 2026-06-21 00:46:51 +08:00
Add cpu bench test
This commit is contained in:
parent
681333a641
commit
928b0be51a
3 changed files with 26 additions and 13 deletions
BIN
examples/cpu/benchmark.cpu.pb.gz
Normal file
BIN
examples/cpu/benchmark.cpu.pb.gz
Normal file
Binary file not shown.
|
|
@ -28,15 +28,22 @@ func run() error {
|
|||
return http.ListenAndServe(addr, nil)
|
||||
})
|
||||
|
||||
g.Go(func() error { return computeSum(1000000, 10*time.Millisecond) })
|
||||
g.Go(func() error { return computeSleepLoop(1000000, 10*time.Millisecond) })
|
||||
|
||||
return g.Wait()
|
||||
}
|
||||
|
||||
func computeSum(to int, sleep time.Duration) error {
|
||||
func computeSleepLoop(n int, sleep time.Duration) error {
|
||||
for {
|
||||
compute(n)
|
||||
time.Sleep(sleep)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func compute(n int) int64 {
|
||||
var sum int64
|
||||
for i := 0; i < to; i++ {
|
||||
for i := 0; i < n; i++ {
|
||||
sum += int64(i) / 2
|
||||
sum += int64(i) / 3
|
||||
sum += int64(i) / 4
|
||||
|
|
@ -45,8 +52,5 @@ func computeSum(to int, sleep time.Duration) error {
|
|||
sum += int64(i) / 7
|
||||
sum += int64(i) / 8
|
||||
}
|
||||
time.Sleep(sleep)
|
||||
}
|
||||
|
||||
return nil
|
||||
return sum
|
||||
}
|
||||
|
|
|
|||
9
examples/cpu/main_test.go
Normal file
9
examples/cpu/main_test.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Benchmark_compute(b *testing.B) {
|
||||
compute(b.N)
|
||||
}
|
||||
Loading…
Reference in a new issue