Home | History | Annotate | Download | only in testcase
      1 // Copyright 2015 Google Inc. All rights reserved
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //      http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 /*
     16 Program parse_benchcmp runs testcase_parse_benchmark and displays
     17 performance changes.
     18 
     19 */
     20 package main
     21 
     22 import (
     23 	"fmt"
     24 	"os"
     25 	"os/exec"
     26 	"strings"
     27 )
     28 
     29 func run(prog string, args ...string) {
     30 	cmd := exec.Command(prog, args...)
     31 	cmd.Stdout = os.Stdout
     32 	cmd.Stderr = os.Stderr
     33 	err := cmd.Run()
     34 	if err != nil {
     35 		panic(err)
     36 	}
     37 }
     38 
     39 func output(prog string, args ...string) string {
     40 	cmd := exec.Command(prog, args...)
     41 	out, err := cmd.CombinedOutput()
     42 	if err != nil {
     43 		panic(err)
     44 	}
     45 	return strings.TrimSpace(string(out))
     46 }
     47 
     48 func runBenchtest(fname string) {
     49 	run("go", "generate")
     50 	f, err := os.Create(fname)
     51 	if err != nil {
     52 		panic(err)
     53 	}
     54 	defer func() {
     55 		err = f.Close()
     56 		if err != nil {
     57 			panic(err)
     58 		}
     59 	}()
     60 	cmd := exec.Command("go", "test", "-run", "NONE", "-bench", ".")
     61 	cmd.Stdout = f
     62 	err = cmd.Run()
     63 	if err != nil {
     64 		panic(err)
     65 	}
     66 }
     67 
     68 func main() {
     69 	_, err := exec.LookPath("benchcmp")
     70 	if err != nil {
     71 		fmt.Fprintln(os.Stderr, "benchcmp not found:", err)
     72 		fmt.Fprintln(os.Stderr, "install it by:")
     73 		fmt.Fprintln(os.Stderr, " export GOPATH=$HOME  # if not set")
     74 		fmt.Fprintln(os.Stderr, " PATH=$PATH:$GOPATH/bin")
     75 		fmt.Fprintln(os.Stderr, " go get -u golang.org/x/tools/cmd/benchcmp")
     76 		os.Exit(1)
     77 	}
     78 	status := output("git", "status", "-s")
     79 	if status != "" {
     80 		fmt.Fprintln(os.Stderr, "workspace is dirty. please commit.")
     81 		fmt.Fprintln(os.Stderr, status)
     82 		os.Exit(1)
     83 	}
     84 	curBranch := output("git", "symbolic-ref", "--short", "HEAD")
     85 	if curBranch == "master" {
     86 		fmt.Fprintln(os.Stderr, "current branch is master.")
     87 		fmt.Fprintln(os.Stderr, "run in branch to compare with master.")
     88 		os.Exit(1)
     89 	}
     90 	fmt.Println("Run benchmark on master and ", curBranch)
     91 	fmt.Println("git checkout master")
     92 	run("git", "checkout", "master")
     93 	run("git", "clean", "-f")
     94 	commit := output("git", "log", "--oneline", "-1")
     95 	fmt.Println(commit)
     96 	fmt.Println("running benchmark tests...")
     97 	runBenchtest("bench-old.out")
     98 
     99 	fmt.Println("git checkout", curBranch)
    100 	run("git", "checkout", curBranch)
    101 	run("git", "clean", "-f")
    102 	commit = output("git", "log", "--oneline", "-1")
    103 	fmt.Println(commit)
    104 	fmt.Println("running benchmark tests...")
    105 	runBenchtest("bench-new.out")
    106 
    107 	run("benchcmp", "bench-old.out", "bench-new.out")
    108 }
    109