[2c7eee0] | 1 | package main
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
| 4 | "bufio"
|
---|
| 5 | "flag"
|
---|
| 6 | "fmt"
|
---|
[f4f79dd] | 7 | "log"
|
---|
[2c7eee0] | 8 | "os"
|
---|
| 9 | "runtime"
|
---|
[f4f79dd] | 10 | "runtime/pprof"
|
---|
[2c7eee0] | 11 | "sync/atomic"
|
---|
| 12 | "time"
|
---|
| 13 | )
|
---|
| 14 |
|
---|
| 15 | var clock_mode bool
|
---|
| 16 | var threads_left int64
|
---|
| 17 | var stop int32
|
---|
| 18 | var duration float64
|
---|
| 19 | var stop_count uint64
|
---|
| 20 | var nprocs int
|
---|
| 21 | var nthreads int
|
---|
| 22 |
|
---|
| 23 | func fflush(f *bufio.Writer) {
|
---|
| 24 | defer f.Flush()
|
---|
| 25 | f.Write([]byte("\r"))
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | func wait(start time.Time, is_tty bool) {
|
---|
| 29 | f := bufio.NewWriter(os.Stdout)
|
---|
| 30 | tdur := time.Duration(duration)
|
---|
| 31 | for true {
|
---|
| 32 | time.Sleep(100 * time.Millisecond)
|
---|
| 33 | end := time.Now()
|
---|
| 34 | delta := end.Sub(start)
|
---|
| 35 | if is_tty {
|
---|
| 36 | fmt.Printf(" %.1f",delta.Seconds())
|
---|
| 37 | fflush(f)
|
---|
| 38 | }
|
---|
| 39 | if clock_mode && delta >= (tdur * time.Second) {
|
---|
| 40 | break
|
---|
| 41 | } else if !clock_mode && atomic.LoadInt64(&threads_left) == 0 {
|
---|
| 42 | break
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[f4f79dd] | 47 | func bench_init() func() {
|
---|
[2c7eee0] | 48 | nprocsOpt := flag.Int("p", 1, "The number of processors")
|
---|
| 49 | nthreadsOpt := flag.Int("t", 1, "The number of threads")
|
---|
| 50 | durationOpt := flag.Float64("d", 0, "Duration of the experiment in seconds")
|
---|
| 51 | stopOpt := flag.Uint64("i", 0, "Duration of the experiment in iterations")
|
---|
[f4f79dd] | 52 | cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
|
---|
[2c7eee0] | 53 |
|
---|
| 54 | flag.Parse()
|
---|
| 55 |
|
---|
| 56 | nprocs = *nprocsOpt
|
---|
| 57 | nthreads = *nthreadsOpt
|
---|
| 58 | duration = *durationOpt
|
---|
| 59 | stop_count = *stopOpt
|
---|
| 60 |
|
---|
| 61 | if duration > 0 && stop_count > 0 {
|
---|
| 62 | panic(fmt.Sprintf("--duration and --iterations cannot be used together\n"))
|
---|
| 63 | } else if duration > 0 {
|
---|
| 64 | clock_mode = true
|
---|
| 65 | stop_count = 0xFFFFFFFFFFFFFFFF
|
---|
| 66 | fmt.Printf("Running for %f seconds\n", duration)
|
---|
| 67 | } else if stop_count > 0 {
|
---|
| 68 | clock_mode = false
|
---|
| 69 | fmt.Printf("Running for %d iterations\n", stop_count)
|
---|
| 70 | } else {
|
---|
| 71 | duration = 5
|
---|
| 72 | clock_mode = true
|
---|
[8faa6612] | 73 | fmt.Printf("Running for %f seconds (default)\n", duration)
|
---|
[2c7eee0] | 74 | }
|
---|
| 75 |
|
---|
| 76 | runtime.GOMAXPROCS(nprocs)
|
---|
[f4f79dd] | 77 |
|
---|
| 78 | if (*cpuprofile) != "" {
|
---|
| 79 | f, err := os.Create(*cpuprofile)
|
---|
| 80 | if err != nil {
|
---|
| 81 | log.Fatal(err)
|
---|
| 82 | }
|
---|
| 83 | pprof.StartCPUProfile(f)
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | return func() {
|
---|
| 87 | if (*cpuprofile) != "" {
|
---|
| 88 | pprof.StopCPUProfile()
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
[2c7eee0] | 91 | }
|
---|