-
Notifications
You must be signed in to change notification settings - Fork 33
/
dotest.scm
407 lines (367 loc) · 14.4 KB
/
dotest.scm
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
;; ***** BEGIN LICENSE BLOCK *****
;; Roadsend PHP Compiler
;; Copyright (C) 2007 Roadsend, Inc.
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 2
;; of the License, or (at your option) any later version.
;;
;; This program 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 this program; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
;; ***** END LICENSE BLOCK *****
(module dotest
(main dotest)
; (library "common")
(extern
(include "sys/time.h")
(include "runtime/ext/standard/windows-time.h")
(type timeval
(struct (tv-sec::elong "tv_sec")
(tv-usec::elong "tv_usec"))
"struct timeval")
(type timezone* opaque "struct timezone*")
(macro gettimeofday::int (::timeval* ::timezone*) "gettimeofday") ) )
(define *phpoo* (string-append (or (getenv "PCC_HOME") "@PCC_HOME@")
(string (file-separator))
"compiler"
(string (file-separator))
"pcc " (or (getenv "PCC_OPTS") "")))
(define *php-bin* "php")
(when (getenv "PHP")
(set! *php-bin* (getenv "PHP")))
(when (getenv "PHP5")
(set! *php-bin* (getenv "PHP5")))
(define *php* (string-append *php-bin* " -d display_errors=off")) ;-n? what if my mysql.sock is someplace else?
(define *diff* (cond-expand (PCC_MINGW "diff -u -w ") (else "diff -u ")))
; optimize?
(when (getenv "OPTIMIZE")
(flush-fprint (current-error-port) "compiling all tests with optimization")
(set! *phpoo* (string-append *phpoo* " -O")))
; static?
(when (getenv "STATIC")
(flush-fprint (current-error-port) "using static linking")
(set! *phpoo* (string-append *phpoo* " --static")))
(when (getenv "TEST_CYCLES")
(flush-fprint (current-error-port) (format "operating with ~a test cycles" (getenv "TEST_CYCLES"))))
(define *timing-file* #f)
(when (getenv "STORE_RESULTS")
(if (file-exists? (getenv "STORE_RESULTS"))
(set! *timing-file* (getenv "STORE_RESULTS"))
(set! *timing-file* "timing-data.txt"))
(flush-fprint (current-error-port) (format "saving timing results to: ~a " *timing-file*)))
(flush-fprint (current-error-port) "using roadsend command: " *phpoo*)
(flush-fprint (current-error-port) "using zend command : " *php*)
(define *phpoo-interpreter* (string-append "pcc " (or (getenv "PCC_OPTS") "") " -f "))
(define *run* 0)
(define *build-fail* 0)
(define *run-fail* 0)
(define *interpret-fail* 0)
(define *run-failures* '())
(define *interpret-failures* '())
(define *faster-compiled* 0)
(define *faster-interpreted* 0)
(define *php-run-total* 0.0)
(define *raven-run-total* 0.0)
(define *raven-interp-total* 0.0)
; storing timing information
(define *zend-times* (make-hashtable))
(define *pcc-times* (make-hashtable))
(define *pcci-times* (make-hashtable))
(define-macro (time . prog)
`(let ((t1 (make-timeval*))
(t2 (make-timeval*))
(tz (pragma::timezone* "((struct timezone*)NULL)")))
(gettimeofday t1 tz)
,@prog
(gettimeofday t2 tz)
(time-difference t1 t2)))
(define (average-time cycles thunk)
(/ (time
(let loop ((i 0))
(when (< i cycles)
(thunk)
(loop (+ i 1)))))
cycles))
(define (aborting-system . args)
(let ((retval (apply system args)))
(if (= retval 2)
(error 'aborted (format "~A was aborted, exiting." (car args)) retval)
retval)))
(define (add-run-fail what)
(set! *run-fail* (+ *run-fail* 1))
(set! *run-failures* (cons what *run-failures*)))
(define (add-interpreter-fail what)
(set! *interpret-fail* (+ *interpret-fail* 1))
(set! *interpret-failures* (cons what *interpret-failures*)))
(define (test file home target cycles)
"Copy FILE from HOME to TARGET, run it CYCLES times with Zend and Raven,
and compare the results"
(aborting-system "cp " home "/" file " " target)
(set! *run* (+ 1 *run*))
(let ((zend-time 0)
(raven-time 0)
(raven-interpreter-time 0)
(build-time 0)
(retval 0)
(mydir (pwd))
(pass (color green "[PASS]"))
(fail (color red "[FAIL]"))
;(basename (string-append target "/" (prefix file)));
(basename (prefix file))
(zend (lambda (f)
(aborting-system *php* " -f " f ".php > " f ".ee.out") ))
(raven-build (lambda (f)
(aborting-system *phpoo* " " f ".php > " f ".log 2>&1") ))
(raven-interpret (lambda (f)
(aborting-system *phpoo-interpreter* " " f ".php > "
f ".rebel.interpreter.out 2> " f ".rebel.interpreter.log")))
(raven-run (lambda (f)
;; XXX without this ./, tests like "sort" might run the wrong command
(aborting-system "." (cond-expand (PCC_MINGW "\\") (else "/"))
f " > " f ".rebel.out")))
(diff (lambda (f)
(let* ((dfile (string-append f ".diff"))
(retval (aborting-system *diff* f ".ee.out " f ".rebel.out > " dfile)))
; clean empty diff files
(when (and (file-exists? dfile)
(= 0 (file-size dfile)))
(delete-file dfile))
retval))))
(flush-print "[" file "]")
(bind-exit (return)
; chdir
(chdir target)
;;Evil Empire
(set! zend-time (time (zend basename)))
;;Rebel Alliance Build
(set! build-time (time (set! retval (raven-build basename))))
(if (= 0 retval)
(display (format "build: ~a ( ~a ) " pass build-time))
(begin
(flush-print "build: " fail)
(set! *build-fail* (+ 1 *build-fail*))
(return #f)
))
;;Rebel Alliance Run
(set! raven-time (time (set! retval (raven-run basename))))
;;diff
(if (= 0 retval)
(if (= 0 (diff basename))
(flush-print "run: " pass)
(begin
(flush-print "run: " fail)
(add-run-fail file)
(return #f)
))
(begin
(add-run-fail file)
(flush-print "run: " fail " (retval " retval ")\n")
(return #f)
)))
(when (getenv "INTERPRETER")
(set! raven-interpreter-time (time (raven-interpret basename)))
(if (= 0 (aborting-system *diff* basename ".ee.out "
basename ".rebel.interpreter.out > " basename ".interpreter.diff"))
(flush-print "interpreter: " pass)
(begin
(flush-print "interpreter: " fail)
(add-interpreter-fail file)))
; clean empty diff files
(when (and (file-exists? (string-append basename ".interpreter.diff"))
(= 0 (file-size (string-append basename ".interpreter.diff"))))
(delete-file (string-append basename ".interpreter.diff"))))
;;benchmark
(unless (< cycles 2)
(set! zend-time (average-time cycles (lambda () (zend basename))))
(set! raven-time (average-time cycles (lambda () (raven-run basename))))
(when (getenv "INTERPRETER")
(set! raven-interpreter-time (average-time cycles (lambda () (raven-interpret basename))))))
(hashtable-put! *zend-times* file zend-time)
(hashtable-put! *pcc-times* file raven-time)
(when (getenv "INTERPRETER")
(hashtable-put! *pcci-times* file raven-interpreter-time))
(set! *php-run-total* (+ *php-run-total* zend-time))
(set! *raven-run-total* (+ *raven-run-total* raven-time))
(set! *raven-interp-total* (+ *raven-interp-total* raven-interpreter-time))
(flush-print "zend: " zend-time " / raven : " raven-time
(let ((time-diff (/ (round (* (- raven-time zend-time) 1000)) 1000.0))
(xtime (number->string (if (> raven-time zend-time)
(/ raven-time zend-time)
(/ zend-time raven-time))))
(perc (truncate (* 100 (/ raven-time zend-time)))))
(set! xtime (substring xtime 0 (min 5 (string-length xtime))))
(if (< time-diff 0)
(begin
(set! *faster-compiled* (+ *faster-compiled* 1))
(color green (format " [~a ~a% | ~aX faster]" time-diff perc xtime)))
(color red (format " [+~a ~a% | ~aX slower]" time-diff perc xtime))))
(if (getenv "INTERPRETER")
""
"\n"))
(when (getenv "INTERPRETER")
(flush-print " / interpret: " raven-interpreter-time
(let ((time-diff (/ (round (* (- raven-interpreter-time zend-time) 1000)) 1000.0))
(xtime (number->string (if (> raven-interpreter-time zend-time)
(/ raven-interpreter-time zend-time)
(/ zend-time raven-interpreter-time))))
(perc (truncate (* 100 (/ raven-interpreter-time zend-time) ))))
(set! xtime (substring xtime 0 (min 5 (string-length xtime))))
(if (< time-diff 0)
(begin
(set! *faster-interpreted* (+ *faster-interpreted* 1))
(color green (format " [~a ~a% | ~aX faster]" time-diff perc xtime)))
(color red (format " [+~a ~a% | ~aX slower]" time-diff perc xtime))))
"\n"))
(chdir mydir)))
(define (time-difference t1::timeval* t2::timeval*)
"time-difference = t2 - t1"
(+ (- (elong->flonum (timeval*-tv-sec t2))
(elong->flonum (timeval*-tv-sec t1)))
(/ (- (elong->flonum (timeval*-tv-usec t2))
(elong->flonum (timeval*-tv-usec t1)))
1000000.0)))
(define (dotest argv)
(unless (>= (length argv) 3)
(flush-print "Wrong number of arguments: " argv)
(usage)
(exit 1))
(let ((home (cadr argv))
(target (caddr argv))
(cycles (string->integer
(cond ((> (length argv) 3) (cadddr argv))
((getenv "TEST_CYCLES"))
(else "1")))))
(unless (directory? home)
(flush-print "Test script directory is invalid: " home)
(exit 2))
(unless (directory? target)
(flush-print "Test target directory is invalid: " target)
(exit 3))
;;so that zend can find the include files
(aborting-system "cp " home "/*.inc " target "/")
(let ((single-test (getenv "TEST"))
(test-list '()))
(if single-test
(begin
(unless (string=? (suffix single-test) "php")
(set! single-test (string-append single-test ".php")))
(set! test-list (list single-test))
(test single-test home target cycles)
(dump-results single-test target))
(begin
(set! test-list (scripts home))
(for-each (lambda (t)
(test t home target cycles))
test-list)
(when (> (length *run-failures*) 0)
(flush-print "compiler run failures:")
(for-each (lambda (v)
(flush-print v))
*run-failures*)
(newline))
(when (> (length *interpret-failures*) 0)
(flush-print "interpreter failures:")
(for-each (lambda (v)
(flush-print v))
*interpret-failures*)
(newline))))
(when *timing-file*
; store results
(let ((fp (append-output-file *timing-file*)))
(if fp
(with-output-to-port fp
(lambda ()
(when (getenv "RUN_DESC")
(print "-------------" (getenv "RUN_DESC") "--------------"))
(print (current-date))
(print (string-replace (system->string "uname -a") #\Newline #\space))
(print "Roadsend PHP: " (string-replace (system->string "pcc --version") #\Newline #\space) " | " *phpoo*)
(print "Zend PHP: " (system->string "php -r 'echo phpversion();'") " | " *php*)
(print "test cycles: " cycles)
(format "~a~a~a~a~a"
"script"
#\tab
"zend"
#\tab
"compiled"
#\tab
"interpreted")
(for-each (lambda (t)
(print (format "~a~a~a~a~a"
t
#\tab
(hashtable-get *zend-times* t)
#\tab
(hashtable-get *pcc-times* t)
#\tab
(hashtable-get *pcci-times* t))))
test-list)
(print (format "AVERAGE ZEND TIME: ~a" (/ *php-run-total* *run*)))
(print (format "AVERAGE PCC TIME: ~a" (/ *raven-run-total* *run*)))
(print *run* " total tests, " *build-fail* " failed to compile and "
*run-fail* " ran wrong.")
(when (getenv "INTERPRETER")
(print (format "AVERAGE INTERPRETER TIME: ~a" (/ *raven-interp-total* *run*)))
(print *interpret-fail* " of " *run* " failed to interpret."))
(print)))
(flush-print "couldn't write to timing file: " *timing-file*)))))
(flush-print *run* " total tests, " *build-fail* " failed to compile and "
*run-fail* " ran wrong. \n")
(when (> *run* 1)
(flush-print (format "~a tests ran faster than zend php" *faster-compiled*))
(flush-print (format "AVERAGE ZEND TIME: ~a" (/ *php-run-total* *run*)))
(flush-print (format "AVERAGE PCC TIME: ~a" (/ *raven-run-total* *run*)))
)
(when (getenv "INTERPRETER")
(flush-print *interpret-fail* " of " *run* " failed to interpret.")
(when (> *run* 1)
(flush-print (format "AVERAGE INTERPRETER TIME: ~a" (/ *raven-interp-total* *run*)))
(flush-print (format "~a tests ran faster interpreted than zend php" *faster-interpreted*))))))
(define (usage)
(flush-print "Usage: " (executable-name) " TEST_HOME TEST_TARGET [CYCLES]")
(flush-print " Test scripts from TEST_HOME in scratch directory TEST_TARGET. Scripts")
(flush-print " are run CYCLES times, (for benchmarking).")
(flush-print)
(flush-print " The CYCLES option can be passed via environment variable TEST_CYCLES.")
(flush-print " If OPTIMIZE environment variable is set, -O is passed to pcc")
(flush-print " If STATIC environment variable is set, --static is passed to pcc")
(flush-print " If INTERPRETER environment variable is set, the tests will be run through interpreter as well")
(flush-print)
(flush-print " If the environment variable TEST is set, it will cause only that test")
(flush-print " to be run."))
(define (scripts dir)
(filter (lambda (a)
(pregexp-match "\\.php$" a))
(sort (directory->list dir) string<?)))
(define yellow 33)
(define red 31)
(define green 32)
(define (color col txt)
"colorize txt"
(if (getenv "NOCOLOR")
txt
(format "\033[~a;1m~a\033[0m" col txt)))
(define (dump-results test target)
(when (> *build-fail* 0)
(flush-print "Log:")
(aborting-system "cat " target "/" (prefix test) ".log"))
(when (> *run-fail* 0)
(flush-print "Differences: (diff <evil empire> <rebel alliance>)")
(aborting-system "cat " target "/" (prefix test) ".diff"))
(when (> *interpret-fail* 0)
(flush-print "Differences (interpreter): (diff <evil empire> <rebel alliance>)")
(aborting-system "cat " target "/" (prefix test) ".interpreter.diff"))
(flush-print "\n"))
(define (flush-fprint port . rest)
(apply fprint port rest)
(flush-output-port port))
(define (flush-print . rest)
(apply print rest)
(flush-output-port (current-output-port)))