-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.go
47 lines (43 loc) · 1.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Command benchcmd times a shell command using Go benchmark format.
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"syscall"
"time"
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [-n iters] benchname cmd...\n", os.Args[0])
flag.PrintDefaults()
}
n := flag.Int("n", 5, "iterations")
flag.Parse()
if flag.NArg() < 2 {
flag.Usage()
os.Exit(2)
}
benchname := flag.Arg(0)
args := flag.Args()[1:]
for i := 0; i < *n; i++ {
fmt.Printf("Benchmark%s\t", benchname)
cmd := exec.Command(args[0], args[1:]...)
before := time.Now()
if err := cmd.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
after := time.Now()
fmt.Printf("%d\t%d ns/op", 1, after.Sub(before))
fmt.Printf("\t%d user-ns/op\t%d sys-ns/op", cmd.ProcessState.UserTime(), cmd.ProcessState.SystemTime())
if ru, ok := cmd.ProcessState.SysUsage().(*syscall.Rusage); ok {
fmt.Printf("\t%d peak-RSS-bytes", ru.Maxrss*(1<<10))
}
fmt.Printf("\n")
}
}