This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy path_benchmarkrunner.livecodescript
More file actions
355 lines (282 loc) · 10.5 KB
/
_benchmarkrunner.livecodescript
File metadata and controls
355 lines (282 loc) · 10.5 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
script "BenchmarkRunner"
/*
Copyright (C) 2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
-- FIXME provide this on the command line
constant kLogFilename = "_benchmark_suite.log"
-- This is the message dispatched before invoking each benchmark command
constant kSetupMessage = "BenchmarkSetup"
-- And this message is dispatched *after* each benchmark command
constant kTeardownMessage = "BenchmarkTearDown"
on startup
send "BenchmarkRunnerMain" to me in 0
end startup
----------------------------------------------------------------
-- Command-line processing
----------------------------------------------------------------
private function getCommandLineInfo
local tRawArg, tSelfCommand, tSelfScript, tInArgs, tArgs
put false into tInArgs
-- Treat everything up to & including the first
-- ".livecodescript" file as the command for running the test
-- runner, and everything after it as test runner arguments
local tCommandArguments
if the version begins with "6.7" then
put $0 into tSelfCommand[1]
if the environment is "command line" then
put "-ui" into tCommandArguments[1]
end if
repeat with i = 1 to $#
put value("$" & i) into tCommandArguments[the number of elements in tCommandArguments + 1]
end repeat
else
do "put the commandName into tSelfCommand[1]"
do "put the commandArguments into tCommandArguments"
end if
repeat for each element tRawArg in tCommandArguments
if tInArgs then
put tRawArg into tArgs[1 + the number of elements in tArgs]
else
put tRawArg into tSelfCommand[1 + the number of elements in tSelfCommand]
if tRawArg ends with ".livecodescript" then
put tRawArg into tSelfScript
put true into tInArgs
end if
end if
end repeat
local tInfo
put tSelfCommand into tInfo["self-command"]
put tSelfScript into tInfo["self-script"]
put tArgs into tInfo["args"]
return tInfo
end getCommandLineInfo
----------------------------------------------------------------
-- Top-level actions
----------------------------------------------------------------
command BenchmarkRunnerMain
local tInfo
put getCommandLineInfo() into tInfo
switch tInfo["args"][1]
case "invoke"
doInvoke tInfo
break
case "run"
doRun tInfo
break
case "--help"
case "-h"
case "help"
doUsage 0
break
default
doUsage 1
break
end switch
quit 0
end BenchmarkRunnerMain
private command doInvoke pInfo
put pInfo["args"][2] into pInfo["invoke"]["script"]
put pInfo["args"][3] into pInfo["invoke"]["command"]
invokeLoadLibrary pInfo
invokeBenchmark pInfo
end doInvoke
private command doRun pInfo
local tScript, tCommand, tLog
put pInfo["args"][2] into tScript
put pInfo["args"][3] into tCommand
if tCommand is not empty then
runSingleCommand pInfo, tScript, tCommand
else if tScript is not empty and there is no folder tScript then
runSingleScript pInfo, tScript
else
runAllScripts pInfo, tScript
end if
put the result into tLog
-- Save the log to file
local tLogToWrite
put tLog into tLogToWrite
if the platform is "win32" then
replace return with numToChar(13) & numToChar(10) in tLogToWrite
end if
put tLogToWrite into url ("binfile:" & kLogFilename)
end doRun
private command doUsage pStatus
write "Usage: _benchmarkrunner.livecodescript run [SCRIPT [COMMAND]]" & return to stderr
quit pStatus
end doUsage
on errorDialog pExecutionError
write "ERROR:" && pExecutionError & return to stderr
quit 1
end errorDialog
----------------------------------------------------------------
-- Support for invoking test commands
----------------------------------------------------------------
-- Execute a benchmark
private command invokeBenchmark pInfo
local tStackName
-- This should auto-load the test script
put the name of stack pInfo["invoke"]["script"] into tStackName
-- Dispatch the test setup command, and the test command itself
dispatch kSetupMessage to tStackName
dispatch pInfo["invoke"]["command"] to tStackName
dispatch kTeardownMessage to tStackName
end invokeBenchmark
-- Add the unit test library stack to the backscripts
private command invokeLoadLibrary pInfo
local tStackName, tStackFile
-- This should auto-load the library
put invokeGetLibraryStack(pInfo) into tStackFile
put the name of stack tStackFile into tStackName
-- Add the library to the backscripts
insert the script of stack tStackName into back
end invokeLoadLibrary
-- Return the filename of the unit test library stack
private function invokeGetLibraryStack pInfo
local tFilename
put pInfo["self-script"] into tFilename
set the itemDelimiter to slash
put "_benchmarklib.livecodescript" into item -1 of tFilename
return tFilename
end invokeGetLibraryStack
----------------------------------------------------------------
-- Support for running tests
----------------------------------------------------------------
-- Run all the benchmark scripts that can be found below the current
-- directory
private command runAllScripts pInfo, pFolder
local tFile, tLog
repeat for each element tFile in runGetBenchmarkFileNames(pFolder)
runSingleScript pInfo, tFile
put the result after tLog
end repeat
return tLog
end runAllScripts
-- Run the benchmarks found in one specific script file
private command runSingleScript pInfo, pScriptFile
local tCommand, tLog, tMetadata
repeat for each element tCommand in runGetBenchmarkCommandNames(pScriptFile)
runSingleCommand pInfo, pScriptFile, tCommand
put the result after tLog
end repeat
return tLog
end runSingleScript
-- Run a specific named benchmark command tCommand in a script file
-- tScriptFile
private command runSingleCommand pInfo, pScriptFile, pCommand
local tBenchmarkName
put char 10 to -1 of pCommand into tBenchmarkName
write "Running " & tBenchmarkName & "..." & return to stdout
local tArg, tCommandLine
repeat for each element tArg in pInfo["self-command"]
put tArg & " " after tCommandLine
end repeat
put "invoke" && pScriptFile && pCommand after tCommandLine
-- Invoke the test in a subprocess. This ensures that we can detect
-- if a crash occurs
local tBenchmarkTimes, tBenchmarkExitStatus
put shell(tCommandLine) into tBenchmarkTimes
put the result into tBenchmarkExitStatus
-- The output from the shell will be a list of tab separated lines.
-- <variantname> tab <timetaken>
-- Check the exit status and process for logging.
if tBenchmarkExitStatus is not empty then
write tab & "failed" & return to stdout
write tBenchmarkTimes & return to stdout
return tBenchmarkName & tab & "failed" & return
end if
filter tBenchmarkTimes without "*TIS/TSM*"
local tLog
set the itemDelimiter to tab
repeat for each line tResult in tBenchmarkTimes
if item 1 of tResult is not empty then
write tab & item 1 of tResult & space & item 2 of tResult & " ms" & return to stdout
else
write tab & item 2 of tResult & " ms" & return to stdout
end if
put tBenchmarkName & tab & tResult & return after tLog
end repeat
return tLog
end runSingleCommand
-- Get all livecode script files beneath the CWD, apart from
-- filenames starting with "." or "_"
private function runGetBenchmarkFileNames pFolder
local tFiles, tCount
put empty into tFiles
put 0 into tCount
runGetBenchmarkFileNames_Recursive the defaultfolder & slash & pFolder, pFolder, tFiles, tCount
return tFiles
end runGetBenchmarkFileNames
-- Helper command used by runGetTestFileNames
private command runGetBenchmarkFileNames_Recursive pPath, pRelPath, @xFiles, @xCount
-- Save the CWD
local tSaveFolder
put the defaultfolder into tSaveFolder
set the defaultfolder to pPath
-- Process files in the current directory
local tFile
repeat for each line tFile in the files
if tFile ends with ".livecodescript" and \
not (tFile begins with "." or tFile begins with "_") then
if pRelPath is not empty then
put pRelPath & slash before tFile
end if
add 1 to xCount
put tFile into xFiles[xCount]
end if
end repeat
-- Process subdirectories
local tFolder, tFolderPath
repeat for each line tFolder in the folders
if tFolder begins with "." then
next repeat
end if
put pPath & slash & tFolder into tFolderPath
if pRelPath is not empty then
put pRelPath & slash before tFolder
end if
runGetBenchmarkFileNames_Recursive tFolderPath, tFolder, xFiles, xCount
end repeat
-- Restore the CWD
set the defaultfolder to tSaveFolder
end runGetBenchmarkFileNames_Recursive
-- Get a number-indexed array contain the names of all "test"
-- commands in pFilename.
private function runGetBenchmarkCommandNames pFilename
local tScript
-- Get the contents of the file
put url ("file:" & pFilename) into tScript
-- Scan the file for "on Benchmark*" definitions
local tCommandNames, tCount, tLine, tName
repeat for each line tLine in tScript
if token 1 of tLine is not "on" then
next repeat
end if
put token 2 of tLine into tName
if not (tName begins with "Benchmark") then
next repeat
end if
-- Exclude the test setup message
if tName is kSetupMessage or tName is kTeardownMessage then
next repeat
end if
add 1 to tCount
put tName into tCommandNames[tCount]
end repeat
return tCommandNames
end runGetBenchmarkCommandNames
-- Prettify a test name by removing a ".livecodescript" suffix
private function runGetPrettyBenchmarkName pFilename
if pFilename ends with ".livecodescript" then
set the itemDelimiter to "."
return item 1 to -2 of pFileName
end if
end runGetPrettyBenchmarkName