-
Notifications
You must be signed in to change notification settings - Fork 46
/
performanceTest.wls
executable file
·175 lines (156 loc) · 8.07 KB
/
performanceTest.wls
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env wolframscript
Check[
Needs["GeneralUtilities`"];
Needs["GitLink`"];
$setReplaceRoot = FileNameDrop[$InputFileName, -1];
Get[FileNameJoin[{$setReplaceRoot, "DevUtils", "init.m"}]];
Get[FileNameJoin[{$setReplaceRoot, "Kernel", "init.m"}]];
,
Print["Could not get one of the dependencies. Exiting."];
Exit[1];
];
Check[
tests = Hold @ <|
"Single-input rule" -> WolframModel[{{1, 2}} -> {{1, 3}, {1, 3}, {3, 2}}, Automatic, 10],
"Medium rule" -> WolframModel[
{{1, 2, 3}, {4, 3, 5}, {3, 6}} -> {{6, 7, 8}, {6, 9, 10}, {11, 8, 10}, {5, 2, 9}, {9, 9}, {1, 9}, {7, 5}, {8, 5}},
Automatic, 13],
"Sequential rule" -> WolframModel[{{1, 2, 2}, {3, 2, 4}} -> {{5, 4, 4}, {4, 3, 5}, {3, 5, 1}}, Automatic, 10000],
"Large rule" -> WolframModel[
{{1, 2}, {2, 1}, {1, 3}, {2, 3}, {3, 1}, {3, 2}} ->
{{1, 2}, {2, 1}, {1, 3}, {2, 3}, {3, 1}, {3, 2}, {1, 4}, {2, 4}, {3, 4}, {4, 1}, {4, 2}, {4, 3}},
Automatic, 4],
"Exponential-match-count rule" -> WolframModel[{{1}, {1}, {1}} -> {{1}, {1}, {1}, {1}}, Automatic, 10],
"CA emulator" -> WolframModel[
{{{18, 18, 3}, {3, 19, 3}, {3, 3, 3, 3, 3}} ->
{{1, 1, 1, 12}, {1, 1, 14, 1}, {1, 10, 10}, {1, 1, 5}, {5, 5, 5, 5, 5}, {5, 4, 5}, {1, 19, 1}, {1},
{4, 12, 12}, {4}, {17, 14, 14}, {17}, {18, 18, 17}},
{{18, 16, 18}, {16, 16, 20}, {16, 16}} ->
{{1, 1, 14, 1}, {1, 1, 1, 12}, {1, 10, 10}, {1, 6, 1}, {6, 6}, {6, 6, 17}, {1, 1, 20}, {1}, {17, 14, 14},
{17}, {4, 12, 12}, {4}, {18, 4, 18}},
{{1, 1, 2}, {2, 11, 11}, {1, 15, 1}, {15, 13, 13}, {1, 1, 1, 12}, {1, 1, 14, 1}, {1, 10, 10},
{1, 1, 1, 1, 1, 1}, {2, 2, 2, 2, 2, 2}, {15, 15, 15, 15, 15, 15}} ->
{{10, 10, 11}, {10, 13, 10}, {10, 7, 7}, {10, 10, 10, 8}, {10, 10, 9, 10}, {10}, {12}, {14}, {12, 8, 8},
{14, 9, 9}},
{{1, 1, 2}, {2, 11, 11}, {1, 15, 1}, {15, 13, 13}, {1, 1, 1, 12}, {1, 1, 14, 1}, {1, 10, 10},
{1, 1, 1, 1, 1, 1}, {2, 2, 2, 2, 2, 2}, {15}} ->
{{10, 10, 11}, {10, 13, 10}, {10, 7, 7}, {10, 10, 10, 8}, {10, 10, 9, 10}, {10, 10, 10, 10, 10, 10},
{12, 12, 12, 12, 12, 12}, {14, 14, 14, 14, 14, 14}, {12, 8, 8}, {14, 9, 9}},
{{1, 1, 2}, {2, 11, 11}, {1, 15, 1}, {15, 13, 13}, {1, 1, 1, 12}, {1, 1, 14, 1}, {1, 10, 10}, {1},
{2, 2, 2, 2, 2, 2}, {15, 15, 15, 15, 15, 15}} ->
{{10, 10, 11}, {10, 13, 10}, {10, 7, 7}, {10, 10, 10, 8}, {10, 10, 9, 10}, {10, 10, 10, 10, 10, 10},
{12, 12, 12, 12, 12, 12}, {14, 14, 14, 14, 14, 14}, {12, 8, 8}, {14, 9, 9}},
{{1, 1, 2}, {2, 11, 11}, {1, 15, 1}, {15, 13, 13}, {1, 1, 1, 12}, {1, 1, 14, 1}, {1, 10, 10}, {1},
{2, 2, 2, 2, 2, 2}, {15}} ->
{{10, 10, 11}, {10, 13, 10}, {10, 7, 7}, {10, 10, 10, 8}, {10, 10, 9, 10}, {10}, {12}, {14}, {12, 8, 8},
{14, 9, 9}},
{{1, 1, 2}, {2, 11, 11}, {1, 15, 1}, {15, 13, 13}, {1, 1, 1, 12}, {1, 1, 14, 1}, {1, 10, 10},
{1, 1, 1, 1, 1, 1}, {2}, {15, 15, 15, 15, 15, 15}} ->
{{10, 10, 11}, {10, 13, 10}, {10, 7, 7}, {10, 10, 10, 8}, {10, 10, 9, 10}, {10, 10, 10, 10, 10, 10},
{12, 12, 12, 12, 12, 12}, {14, 14, 14, 14, 14, 14}, {12, 8, 8}, {14, 9, 9}},
{{1, 1, 2}, {2, 11, 11}, {1, 15, 1}, {15, 13, 13}, {1, 1, 1, 12}, {1, 1, 14, 1}, {1, 10, 10},
{1, 1, 1, 1, 1, 1}, {2}, {15}} ->
{{10, 10, 11}, {10, 13, 10}, {10, 7, 7}, {10, 10, 10, 8}, {10, 10, 9, 10}, {10, 10, 10, 10, 10, 10},
{12, 12, 12, 12, 12, 12}, {14, 14, 14, 14, 14, 14}, {12, 8, 8}, {14, 9, 9}},
{{1, 1, 2}, {2, 11, 11}, {1, 15, 1}, {15, 13, 13}, {1, 1, 1, 12}, {1, 1, 14, 1}, {1, 10, 10}, {1}, {2},
{15, 15, 15, 15, 15, 15}} ->
{{10, 10, 11}, {10, 13, 10}, {10, 7, 7}, {10, 10, 10, 8}, {10, 10, 9, 10}, {10, 10, 10, 10, 10, 10},
{12, 12, 12, 12, 12, 12}, {14, 14, 14, 14, 14, 14}, {12, 8, 8}, {14, 9, 9}},
{{1, 1, 2}, {2, 11, 11}, {1, 15, 1}, {15, 13, 13}, {1, 1, 1, 12}, {1, 1, 14, 1}, {1, 10, 10}, {1}, {2}, {15}} ->
{{10, 10, 11}, {10, 13, 10}, {10, 7, 7}, {10, 10, 10, 8}, {10, 10, 9, 10}, {10}, {12}, {14}, {12, 8, 8},
{14, 9, 9}}},
{{3, 3, 3, 6}, {3, 3, 7, 3}, {3, 5, 5}, {3, 3, 1}, {3, 2, 3}, {3, 3, 3, 3, 3, 3}, {4, 6, 6}, {4, 4, 4, 4, 4, 4},
{8, 7, 7}, {8, 8, 8, 8, 8, 8}, {1, 4, 1}, {1, 1, 1, 1, 1}, {2, 2, 8}, {2, 2}},
64],
"Multiset addition" -> GenerateMultihistory[
MultisetSubstitutionSystem[{a_, b_} :> {a + b}],
MaxGeneration -> 2, MinEventInputs -> 2, MaxEventInputs -> 2] @ {1, 2, 3, 4}
|>;
$defaultMeasurementsCount = 5;
$measurementsCount = If[Length @ $ScriptCommandLine >= 4,
ToExpression[$ScriptCommandLine[[4]]]
,
$defaultMeasurementsCount
];
If[!IntegerQ[$measurementsCount] || $measurementsCount < 2,
Print["The third argument should be an integer measurements count of at least 2."];
Exit[1];
];
Attributes[meanAroundTiming] = {HoldFirst};
meanAroundTiming[expr_, resultsFunction_] :=
MeanAround @ Table[First @ AbsoluteTiming[resultsFunction @ expr], $measurementsCount];
runTests[repo_, sha_, tests_, resultsFunction_] := ModuleScope[
Print["Testing ", sha];
GitCheckoutReference[repo, sha];
CloseKernels[];
{kernel} = LaunchKernels[1];
result = ParallelEvaluate[
Get[FileNameJoin[{$setReplaceRoot, "Kernel", "init.m"}]];
Check[
Function[{test}, meanAroundTiming[test, resultsFunction], HoldFirst] @@@
KeyMap[ReleaseHold, ReleaseHold @ Map[Hold, tests, {3}]]
,
$Failed
]
,
kernel
];
Print[""];
result
];
speedupDelta[0, new_] := Infinity;
speedupDelta[old_, new_] := (old - new) / old;
$gitRepo = GitOpen[$SetReplaceRoot];
$currentSHA = GitSHA[$gitRepo, "HEAD"];
$cleanQ = AllTrue[# === {} &] @ GitStatus[$gitRepo];
If[!$cleanQ,
Print["Current git tree must be clean."];
Exit[1];
];
argumentReferenceToSHA[ref_] := ModuleScope[
revParseOutput = RunProcess[{"git", "rev-parse", ref}];
If[revParseOutput["ExitCode"] === 0,
StringExtract[revParseOutput["StandardOutput"], 1] (* remove \n at the end *)
,
Print["Reference ", ref, " is not recognized."];
Exit[1];
]
];
$oldSHA = If[Length @ $ScriptCommandLine >= 2, argumentReferenceToSHA @ $ScriptCommandLine[[2]], "master"];
$newSHA = If[Length @ $ScriptCommandLine >= 3, argumentReferenceToSHA @ $ScriptCommandLine[[3]], $currentSHA];
$symbolicRef = RunProcess[{"git", "symbolic-ref", "--short", "HEAD"}];
$originalRef = If[$symbolicRef["ExitCode"] === 0, StringExtract[$symbolicRef["StandardOutput"], 1], $currentSHA];
$oldResults = runTests[$gitRepo, $oldSHA, tests, Quiet];
$newResults = runTests[$gitRepo, $newSHA, tests, Identity];
RunProcess[{"git", "checkout", "-q", $originalRef}];
(* We need to return to the original branch before exiting. *)
If[$oldResults === $Failed || $newResults === $Failed,
Print["Messages occured while running the tests. Exiting."];
Exit[1];
];
$speedup = speedupDelta[$oldResults, $newResults];
$redColor = "\033[0;31m";
$greenColor = "\033[0;32m";
$whiteColor = "\033[0;37m";
$endColor = "\033[0m";
differenceString[meanAround_] := With[{
magnitude = QuantityMagnitude[meanAround, "Percent"]},
(* Due to a weed in WL, MeanAround does not return Around if all values in the list are the same. *)
magnitudeMean = If[Head[magnitude] === Around, magnitude["Value"], magnitude];
magnitudeUncertainty = If[Head[magnitude] === Around, magnitude["Uncertainty"], 0];
If[5 * magnitudeUncertainty < Abs[magnitudeMean], If[magnitudeMean > 0, $greenColor, $redColor], $whiteColor] <>
FirstCase[MakeBoxes[magnitude, StandardForm],
TemplateBox[{value_, error_}, "Around", ___] :> value <> " \[PlusMinus] " <> error <> " %",
ToString[magnitudeMean] <> " \[PlusMinus] 0 %"] <>
$endColor
];
KeyValueMap[
Print[
#1,
StringJoin[ConstantArray[" ", Max[40 - StringLength[#1], 1]]],
differenceString[#2]] &,
$speedup];
,
Print["Message occurred while running the script. Exiting."];
Exit[1];
]