summaryrefslogtreecommitdiffstats
path: root/src/benchmarkrunner/main.go
blob: 40e6f5b10b9375bff5ff592c0fa331f480232215 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main

import (
	"archive/zip"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"code.qt.io/qt/qtqa.git/src/goqtestlib"
	"github.com/kardianos/osext"
)

func recordResults(resultsDirectory string, workingDir string) error {
	os.Unsetenv("TESTRUNNER")

	makeCommand := exec.Command("make", "benchmark")
	makeCommand.Stdout = os.Stdout
	makeCommand.Stderr = os.Stderr
	runner := func(extraArgs []string) error {
		arguments := strings.Join(extraArgs, " ")
		benchmarkArgs := os.Getenv("QT_BENCHMARK_ARGS")
		if benchmarkArgs != "" {
			arguments = arguments + " " + benchmarkArgs
		}

		defer goqtestlib.SetEnvironmentVariableAndRestoreOnExit("TESTARGS", arguments)()
		return makeCommand.Run()
	}

	name, err := filepath.Rel(os.Getenv("QT_BENCHMARK_BASE_DIRECTORY"), workingDir)
	if err != nil {
		return fmt.Errorf("Could not determine relative directory: %s", err)
	}
	if name == "." {
		name = "testcase"
	}

	repetitions := 0
	result, err := goqtestlib.GenerateTestResult(name, resultsDirectory, repetitions, runner)
	if err != nil {
		return err
	}

	return os.Rename(result.PathToResultsXML, filepath.Join(resultsDirectory, name+".xml"))
}

func archiveResults(resultsDir string, outputFile io.Writer) error {
	archiver := zip.NewWriter(outputFile)
	defer archiver.Close()

	return filepath.Walk(resultsDir, func(path string, info os.FileInfo, err error) error {
		if info == nil || info.IsDir() {
			return nil
		}

		relativePath, err := filepath.Rel(resultsDir, path)
		if err != nil {
			return err
		}

		sourceFile, err := os.Open(path)
		if err != nil {
			return err
		}
		defer sourceFile.Close()

		header, err := zip.FileInfoHeader(info)
		if err != nil {
			return err
		}
		header.Name = relativePath

		file, err := archiver.CreateHeader(header)
		if err != nil {
			return err
		}

		n, err := io.Copy(file, sourceFile)
		if n != info.Size() {
			return fmt.Errorf("Incorrect number of bytes written to archive for %s: Wrote %v expected %v", path, n, info.Size())
		}
		return err
	})
}

func collectResults(workingDir string) error {
	self, err := osext.Executable()
	if err != nil {
		return fmt.Errorf("Unable to determine current executable name: %s", err)
	}

	resultsDir, err := ioutil.TempDir("", "")
	if err != nil {
		return err
	}
	defer os.RemoveAll(resultsDir)

	os.Setenv("TESTRUNNER", self)
	os.Setenv("QT_BENCHMARK_RECORDING_DIRECTORY", resultsDir)
	os.Setenv("QT_BENCHMARK_BASE_DIRECTORY", workingDir)
	os.Setenv("QT_HASH_SEED", "0")

	var outputFileName string
	flag.StringVar(&outputFileName, "output-file", "results.zip", "Write collected benchmark results into specified file")
	flag.Parse()

	os.Setenv("QT_BENCHMARK_ARGS", strings.Join(flag.Args(), " "))

	makeCommand := exec.Command("make", "benchmark")
	makeCommand.Stdout = os.Stdout
	makeCommand.Stderr = os.Stderr

	if err := makeCommand.Run(); err != nil {
		return fmt.Errorf("Error running make benchmark: %s", err)
	}

	outputFile, err := os.Create(outputFileName)
	if err != nil {
		return fmt.Errorf("Error creating output file: %s", err)
	}
	defer outputFile.Close()

	return archiveResults(resultsDir, outputFile)
}

func appMain() error {
	workingDir, err := os.Getwd()
	if err != nil {
		return fmt.Errorf("Error determining current working directory: %s", err)
	}

	if os.Getenv("QT_BENCHMARK_RECORDING_DIRECTORY") != "" {
		return recordResults(os.Getenv("QT_BENCHMARK_RECORDING_DIRECTORY"), workingDir)
	}

	return collectResults(workingDir)
}

func main() {

	if err := appMain(); err != nil {
		fmt.Printf("%s\n", err)
		os.Exit(2)
	}
}