|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "slices" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +var workloads = []string{ |
| 15 | + "prime-workload.clj", |
| 16 | + "event-analytics.clj", |
| 17 | + "game-of-life.clj", |
| 18 | + "mandelbrot.clj", |
| 19 | +} |
| 20 | + |
| 21 | +func main() { |
| 22 | + glojure := flag.String("glojure", "./bin/glj", "path to the Glojure executable") |
| 23 | + letGo := flag.String("let-go", "lg", "path to the let-go executable") |
| 24 | + runs := flag.Int("runs", 11, "timed runs per executable and workload") |
| 25 | + dir := flag.String("dir", "benchmark/portable", "directory containing the workloads") |
| 26 | + flag.Parse() |
| 27 | + |
| 28 | + if *runs < 1 { |
| 29 | + fmt.Fprintln(os.Stderr, "-runs must be at least 1") |
| 30 | + os.Exit(2) |
| 31 | + } |
| 32 | + |
| 33 | + for _, workload := range workloads { |
| 34 | + path := filepath.Join(*dir, workload) |
| 35 | + glojureOutput, _, err := run(*glojure, path) |
| 36 | + check(err, *glojure, path) |
| 37 | + letGoOutput, _, err := run(*letGo, path) |
| 38 | + check(err, *letGo, path) |
| 39 | + if !bytes.Equal(glojureOutput, letGoOutput) { |
| 40 | + fmt.Fprintf(os.Stderr, "%s produced different output\n", workload) |
| 41 | + fmt.Fprintf(os.Stderr, "Glojure: %q\nlet-go: %q\n", glojureOutput, letGoOutput) |
| 42 | + os.Exit(1) |
| 43 | + } |
| 44 | + |
| 45 | + glojureTimes := make([]time.Duration, 0, *runs) |
| 46 | + letGoTimes := make([]time.Duration, 0, *runs) |
| 47 | + for iteration := 0; iteration < *runs; iteration++ { |
| 48 | + if iteration%2 == 0 { |
| 49 | + glojureTimes = append(glojureTimes, timedRun(*glojure, path)) |
| 50 | + letGoTimes = append(letGoTimes, timedRun(*letGo, path)) |
| 51 | + } else { |
| 52 | + letGoTimes = append(letGoTimes, timedRun(*letGo, path)) |
| 53 | + glojureTimes = append(glojureTimes, timedRun(*glojure, path)) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + glojureMedian := median(glojureTimes) |
| 58 | + letGoMedian := median(letGoTimes) |
| 59 | + fmt.Printf( |
| 60 | + "%-22s glojure=%-12s let-go=%-12s ratio=%.3f\n", |
| 61 | + workload, |
| 62 | + glojureMedian, |
| 63 | + letGoMedian, |
| 64 | + float64(glojureMedian)/float64(letGoMedian), |
| 65 | + ) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func timedRun(binary, workload string) time.Duration { |
| 70 | + _, elapsed, err := run(binary, workload) |
| 71 | + check(err, binary, workload) |
| 72 | + return elapsed |
| 73 | +} |
| 74 | + |
| 75 | +func run(binary, workload string) ([]byte, time.Duration, error) { |
| 76 | + start := time.Now() |
| 77 | + output, err := exec.Command(binary, workload).CombinedOutput() |
| 78 | + return output, time.Since(start), err |
| 79 | +} |
| 80 | + |
| 81 | +func check(err error, binary, workload string) { |
| 82 | + if err == nil { |
| 83 | + return |
| 84 | + } |
| 85 | + fmt.Fprintf(os.Stderr, "%s failed while running %s: %v\n", binary, workload, err) |
| 86 | + os.Exit(1) |
| 87 | +} |
| 88 | + |
| 89 | +func median(values []time.Duration) time.Duration { |
| 90 | + sorted := slices.Clone(values) |
| 91 | + slices.Sort(sorted) |
| 92 | + middle := len(sorted) / 2 |
| 93 | + if len(sorted)%2 == 1 { |
| 94 | + return sorted[middle] |
| 95 | + } |
| 96 | + return (sorted[middle-1] + sorted[middle]) / 2 |
| 97 | +} |
0 commit comments