github.com/charlievieth/fastwalk@v1.0.3/scripts/bench_comp.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "log" 9 "os" 10 "os/exec" 11 "path/filepath" 12 "strconv" 13 ) 14 15 func init() { 16 log.SetOutput(os.Stderr) 17 log.SetFlags(log.LstdFlags | log.Lshortfile) 18 } 19 20 var Tests = []string{ 21 "filepath", 22 "fastwalk", 23 } 24 25 func main() { 26 count := flag.Int("count", 5, "Run each test and benchmark n times") 27 compCmd := flag.String("comp", "benchstat", "Benchmark comparison command") 28 flag.Parse() 29 30 if _, err := exec.LookPath(*compCmd); err != nil { 31 log.Fatalf("error: %v: %q\n", err, *compCmd) 32 } 33 34 tmpdir, err := ioutil.TempDir("", "fastwalk-bench.*") 35 if err != nil { 36 log.Fatal(err) 37 } 38 39 runTest := func(name string) error { 40 fmt.Println("##", name) 41 42 filename := filepath.Join(tmpdir, name+".txt") 43 f, err := os.Create(filename) 44 if err != nil { 45 log.Fatal(err) 46 } 47 defer f.Close() 48 49 args := []string{ 50 "test", 51 "-run", `^$`, 52 "-bench", `^BenchmarkWalkComparison$`, 53 "-benchmem", 54 "-count", strconv.Itoa(*count), 55 "github.com/charlievieth/fastwalk", 56 "-walkfunc", name, 57 } 58 59 cmd := exec.Command("go", args...) 60 cmd.Stderr = os.Stderr 61 cmd.Stdout = io.MultiWriter(os.Stdout, f) 62 63 if err := cmd.Run(); err != nil { 64 log.Fatalf("error running command: %q: %v\n", cmd.Args, err) 65 } 66 67 if err := f.Close(); err != nil { 68 log.Fatal(err) 69 } 70 71 fmt.Print("\n") 72 return nil 73 } 74 75 for _, name := range Tests { 76 runTest(name) 77 } 78 79 benchStat := func(from, to string) { 80 fmt.Printf("## %s vs. %s\n", from, to) 81 cmd := exec.Command(*compCmd, 82 filepath.Join(tmpdir, from+".txt"), 83 filepath.Join(tmpdir, to+".txt"), 84 ) 85 cmd.Stderr = os.Stderr 86 cmd.Stdout = os.Stdout 87 if err := cmd.Run(); err != nil { 88 log.Fatalf("error running command: %q: %v\n", cmd.Args, err) 89 } 90 fmt.Print("\n") 91 } 92 93 fmt.Println("## Comparisons") 94 fmt.Println("########################################################") 95 fmt.Print("\n") 96 benchStat("filepath", "fastwalk") 97 benchStat("godirwalk", "fastwalk") 98 99 fmt.Printf("Temp: %s\n", tmpdir) 100 }