aboutsummaryrefslogtreecommitdiffstats
path: root/testcase/parse_benchcmp.go
blob: 6426ff461f4bdb00e8bf3ba713efbd79976e0e1d (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2015 Google Inc. All rights reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*
Program parse_benchcmp runs testcase_parse_benchmark and displays
performance changes.

*/
package main

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
)

func run(prog string, args ...string) {
	cmd := exec.Command(prog, args...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Run()
	if err != nil {
		panic(err)
	}
}

func output(prog string, args ...string) string {
	cmd := exec.Command(prog, args...)
	out, err := cmd.CombinedOutput()
	if err != nil {
		panic(err)
	}
	return strings.TrimSpace(string(out))
}

func runBenchtest(fname string) {
	run("go", "generate")
	f, err := os.Create(fname)
	if err != nil {
		panic(err)
	}
	defer func() {
		err = f.Close()
		if err != nil {
			panic(err)
		}
	}()
	cmd := exec.Command("go", "test", "-run", "NONE", "-bench", ".")
	cmd.Stdout = f
	err = cmd.Run()
	if err != nil {
		panic(err)
	}
}

func main() {
	_, err := exec.LookPath("benchcmp")
	if err != nil {
		fmt.Fprintln(os.Stderr, "benchcmp not found:", err)
		fmt.Fprintln(os.Stderr, "install it by:")
		fmt.Fprintln(os.Stderr, " export GOPATH=$HOME  # if not set")
		fmt.Fprintln(os.Stderr, " PATH=$PATH:$GOPATH/bin")
		fmt.Fprintln(os.Stderr, " go get -u golang.org/x/tools/cmd/benchcmp")
		os.Exit(1)
	}
	status := output("git", "status", "-s")
	if status != "" {
		fmt.Fprintln(os.Stderr, "workspace is dirty. please commit.")
		fmt.Fprintln(os.Stderr, status)
		os.Exit(1)
	}
	curBranch := output("git", "symbolic-ref", "--short", "HEAD")
	if curBranch == "master" {
		fmt.Fprintln(os.Stderr, "current branch is master.")
		fmt.Fprintln(os.Stderr, "run in branch to compare with master.")
		os.Exit(1)
	}
	fmt.Println("Run benchmark on master and ", curBranch)
	fmt.Println("git checkout master")
	run("git", "checkout", "master")
	run("git", "clean", "-f")
	commit := output("git", "log", "--oneline", "-1")
	fmt.Println(commit)
	fmt.Println("running benchmark tests...")
	runBenchtest("bench-old.out")

	fmt.Println("git checkout", curBranch)
	run("git", "checkout", curBranch)
	run("git", "clean", "-f")
	commit = output("git", "log", "--oneline", "-1")
	fmt.Println(commit)
	fmt.Println("running benchmark tests...")
	runBenchtest("bench-new.out")

	run("benchcmp", "bench-old.out", "bench-new.out")
}