Skip to content

Instantly share code, notes, and snippets.

@liaoyw
Last active September 17, 2017 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liaoyw/8a30b0625dec470cef2897507db8f6e7 to your computer and use it in GitHub Desktop.
Save liaoyw/8a30b0625dec470cef2897507db8f6e7 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"time"
"sync/atomic"
"hash/adler32"
"fmt"
"os"
"runtime"
)
// test how cfs_quota affect cpu intensive task
// docker run -it --cpu-period=100000 --cpu-quota=400000 -v `pwd`:/tmp --entrypoint bash test_image
// /tmp/cfs_quota -c 8 -d 10s
var c = flag.Int("c", 4, "groutine count")
var t = flag.Duration("t", 1*time.Second, "tick to print result")
var d = flag.Duration("d", 30*time.Second, "test duration")
var s = []byte(`Adler-32 is composed of two sums accumulated per byte: s1 is
the sum of all bytes, s2 is the sum of all s1 values. Both sums
are done modulo 65521. s1 is initialized to 1, s2 to zero. The
Adler-32 checksum is stored as s2*65536 + s1 in most-
significant-byte first (network) order.`)
func burnCpu(n int) {
for i := 0; i < n; i++ {
adler32.Checksum(s)
}
}
func main() {
flag.Parse()
fmt.Println(runtime.NumCPU(), runtime.GOMAXPROCS(-1))
tickC := time.Tick(*t)
stopC := time.After(*d)
var taskCount int32
for i := 0; i < *c; i ++ {
go func() {
for {
//s := time.Now()
burnCpu(5000)
//fmt.Println("cost: ", time.Since(s))
atomic.AddInt32(&taskCount, 1)
}
}()
}
var count int32 = 0
for {
select {
case <- tickC:
cur := atomic.LoadInt32(&taskCount)
fmt.Printf("%v: %d\n", time.Now(), cur - count)
count = cur
case <- stopC:
fmt.Println("avg process speed: ", atomic.LoadInt32(&taskCount)/int32((*d).Seconds()))
os.Exit(0)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment