You've already forked go-profiler-notes
mirror of
https://github.com/DataDog/go-profiler-notes.git
synced 2025-07-15 23:54:16 +02:00
Add cpu bench test
This commit is contained in:
BIN
examples/cpu/benchmark.cpu.pb.gz
Normal file
BIN
examples/cpu/benchmark.cpu.pb.gz
Normal file
Binary file not shown.
@ -28,25 +28,29 @@ func run() error {
|
|||||||
return http.ListenAndServe(addr, nil)
|
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()
|
return g.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func computeSum(to int, sleep time.Duration) error {
|
func computeSleepLoop(n int, sleep time.Duration) error {
|
||||||
for {
|
for {
|
||||||
var sum int64
|
compute(n)
|
||||||
for i := 0; i < to; i++ {
|
|
||||||
sum += int64(i) / 2
|
|
||||||
sum += int64(i) / 3
|
|
||||||
sum += int64(i) / 4
|
|
||||||
sum += int64(i) / 5
|
|
||||||
sum += int64(i) / 6
|
|
||||||
sum += int64(i) / 7
|
|
||||||
sum += int64(i) / 8
|
|
||||||
}
|
|
||||||
time.Sleep(sleep)
|
time.Sleep(sleep)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func compute(n int) int64 {
|
||||||
|
var sum int64
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
sum += int64(i) / 2
|
||||||
|
sum += int64(i) / 3
|
||||||
|
sum += int64(i) / 4
|
||||||
|
sum += int64(i) / 5
|
||||||
|
sum += int64(i) / 6
|
||||||
|
sum += int64(i) / 7
|
||||||
|
sum += int64(i) / 8
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
Reference in New Issue
Block a user