forked from scheme-requests-for-implementation/srfi-135
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexts-search-test.sps
407 lines (359 loc) · 13.8 KB
/
texts-search-test.sps
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
;;; Copyright (C) William D Clinger 2015. All Rights Reserved.
;;;
;;; Permission is hereby granted, free of charge, to any person
;;; obtaining a copy of this software and associated documentation
;;; files (the "Software"), to deal in the Software without restriction,
;;; including without limitation the rights to use, copy, modify, merge,
;;; publish, distribute, sublicense, and/or sell copies of the Software,
;;; and to permit persons to whom the Software is furnished to do so,
;;; subject to the following conditions:
;;;
;;; The above copyright notice and this permission notice shall be
;;; included in all copies or substantial portions of the Software.
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
;;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
;;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
;;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
;;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
;;; This is a stress test and benchmark for text-contains and friends.
;;; Its main purpose is to help select the thresholds that determine
;;; which search algorithm is used when by text-contains.
;;; To run this program, 135.sld must be modified to export the
;;; following procedures:
;;;
;;; %textual-contains:naive
;;; %textual-contains:rabin-karp
;;; %textual-contains:boyer-moore
(import (scheme base)
(scheme char)
(scheme write)
(scheme time)
(only (srfi 27) random-integer)
(srfi 135))
(define (writeln . xs)
(for-each write xs)
(newline))
(define (displayln . xs)
(for-each display xs)
(newline))
(define (fail token . more)
(displayln "Error: test failed: ")
(writeln token)
(if (not (null? more))
(for-each writeln more))
(newline)
#f)
;;; FIXME
(define-syntax test
(syntax-rules ()
((_ expr expected)
(let ((actual expr))
(or (equal? actual expected)
(fail 'expr actual expected))))))
(define-syntax test-assert
(syntax-rules ()
((_ expr)
(or expr (fail 'expr)))))
(define-syntax test-deny
(syntax-rules ()
((_ expr)
(or (not expr) (fail 'expr)))))
(define (timed-test name thunk expected)
(define name
(if (string? name)
(string->symbol name)
name))
(define (rounded jiffies)
(let* ((usec (round (/ (* 1000000.0 jiffies) (jiffies-per-second)))))
(/ usec 1000000.0)))
(let* ((j0 (current-jiffy))
(result (thunk))
(j1 (current-jiffy)))
(if (not (equal? result expected))
(begin (fail name result expected)
(write (map char->integer
(string->list (textual->string *txt1*))))
(newline)
(write (map char->integer
(string->list (textual->string *txt2*))))
(newline)
(newline)))
(rounded (- j1 j0))))
(define (seconds->milliseconds t)
(exact (round (* 1000.0 t))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (textual-contains:naive txt1 txt2)
(%textual-contains:naive txt1 txt2
0 (text-length txt1)
0 (text-length txt2)))
(define (textual-contains:rabin-karp txt1 txt2)
(textual-contains txt1 txt2
0 (text-length txt1)
0 (text-length txt2)))
(define (textual-contains:boyer-moore txt1 txt2)
(%textual-contains:boyer-moore txt1 txt2
0 (text-length txt1)
0 (text-length txt2)))
(define (make-random-string n)
(make-random-ascii-string n))
;; Never uses tilde
(define (make-random-ascii-string n)
(do ((s (make-string n))
(i 0 (+ i 1)))
((= i n) s)
(string-set! s i (integer->char (random-integer 126)))))
(define (make-random-unicode-string n)
(do ((s (make-string n))
(i 0 (+ i 1)))
((= i n) s)
(let loop ((bits (random-integer #x10ffff)))
(if (or (<= #xd800 bits #xdfff)
(= bits (char->integer #\~)))
(loop (random-integer 32768))
(string-set! s i (integer->char bits))))))
(define *txt1* (text))
(define *txt2* (text))
(define name:length 22) ; FIXME
(define (random-search testname make-random-string m n)
(let* ((n1 (random-integer n))
(n2 (max 1 (- n n1 1)))
(m1 (random-integer (max 1 (- m n))))
(m2 (max 1 (- m m1 n)))
(P (string-append (make-random-string n1)
"~"
(make-random-string n2)))
(T (string-append (make-random-string m1)
P
(make-random-string m2)))
(txt1 (string->text T))
(txt2 (string->text P))
(ignored (begin (set! *txt1* txt1)
(set! *txt2* txt2)))
(expected m1)
(name (string-append testname
(number->string m)
" "
(number->string n)))
(t1 (timed-test name
(lambda ()
(dotimes (quotient total-work m)
(lambda ()
(textual-contains:naive txt1 txt2))))
expected))
(t2 (timed-test name
(lambda ()
(dotimes
(quotient total-work m)
(lambda ()
(textual-contains:rabin-karp txt1 txt2))))
expected))
(t3 (timed-test name
(lambda ()
(dotimes
(quotient total-work m)
(lambda ()
(textual-contains:boyer-moore txt1 txt2))))
expected))
(t4 (timed-test name
(lambda ()
(dotimes (quotient total-work m)
(lambda ()
(textual-contains txt1 txt2))))
expected))
(t0 (min t1 t2 t3)))
(display name)
(display (make-string (- name:length (string-length name)) #\space))
(display (cond ((= t0 t1)
"naive ")
((= t0 t2)
"Rabin-Karp ")
((= t0 t3)
"Boyer-Moore ")))
(write (cons (/ (round (* 10.0 (/ t4 t0))) 10.0)
(map seconds->milliseconds (list t1 t2 t3 t4))))
(newline)))
(define (random-search-failing testname make-random-string m n)
(let* ((n1 (random-integer n))
(n2 (max 1 (- n n1 1)))
(P (string-append (make-random-string n1)
"~"
(make-random-string n2)))
(T (string-append (make-random-string m)))
(txt1 (string->text T))
(txt2 (string->text P))
(ignored (begin (set! *txt1* txt1)
(set! *txt2* txt2)))
(name (string-append testname
(number->string m)
" "
(number->string n)))
(t1 (timed-test name
(lambda ()
(dotimes (quotient total-work m)
(lambda ()
(textual-contains:naive txt1 txt2))))
#f))
(t2 (timed-test name
(lambda ()
(dotimes
(quotient total-work m)
(lambda ()
(textual-contains:rabin-karp txt1 txt2))))
#f))
(t3 (timed-test name
(lambda ()
(dotimes
(quotient total-work m)
(lambda ()
(textual-contains:boyer-moore txt1 txt2))))
#f))
(t4 (timed-test name
(lambda ()
(dotimes (quotient total-work m)
(lambda ()
(textual-contains txt1 txt2))))
#f))
(t0 (min t1 t2 t3)))
(display name)
(display (make-string (- name:length (string-length name)) #\space))
(display (cond ((= t0 t1)
"naive ")
((= t0 t2)
"Rabin-Karp ")
((= t0 t3)
"Boyer-Moore ")))
(write (cons (/ (round (* 10.0 (/ t4 t0))) 10.0)
(map seconds->milliseconds (list t1 t2 t3 t4))))
(newline)))
(define (hard-case left/right make-random-string m n)
(let* ((P (make-string (- n 1) #\a))
(P (case left/right
((R) (string-append P "b"))
((L) (string-append "b" P))))
(T (string-append (make-string (- m n) #\a) P))
(txt1 (string->text T))
(txt2 (string->text P))
(ignored (begin (set! *txt1* txt1)
(set! *txt2* txt2)))
(expected (- m n))
(name (string-append "hard("
(symbol->string left/right)
"): "
(number->string m)
" "
(number->string n)))
(t1 (timed-test name
(lambda ()
(dotimes (quotient total-work (* m n))
(lambda ()
(textual-contains:naive txt1 txt2))))
expected))
(t2 (timed-test name
(lambda ()
(dotimes
(quotient total-work (* m n))
(lambda ()
(textual-contains:rabin-karp txt1 txt2))))
expected))
(t3 (timed-test name
(lambda ()
(dotimes
(quotient total-work (* m n))
(lambda ()
(textual-contains:boyer-moore txt1 txt2))))
expected))
(t4 (timed-test name
(lambda ()
(dotimes (quotient total-work (* m n))
(lambda ()
(textual-contains txt1 txt2))))
expected))
(t0 (min t1 t2 t3)))
(display name)
(display (make-string (- name:length (string-length name)) #\space))
(display (cond ((= t0 t1)
"naive ")
((= t0 t2)
"Rabin-Karp ")
((= t0 t3)
"Boyer-Moore ")))
(write (cons (/ (round (* 10.0 (/ t4 t0))) 10.0)
(map seconds->milliseconds (list t1 t2 t3 t4))))
(newline)))
(define (random-ascii-search-successful m n)
(random-search "success: " make-random-ascii-string m n))
(define (random-ascii-search-failing m n)
(random-search-failing "failure: " make-random-ascii-string m n))
(define (hard-case-naive-ascii-search-successful m n)
(hard-case 'R make-random-ascii-string m n))
(define (hard-case-BM-ascii-search-successful m n)
(hard-case 'L make-random-ascii-string m n))
(define (random-unicode-search-successful m n)
(random-search "success: " make-random-unicode-string m n))
(define (hard-case-naive-unicode-search-successful m n)
(hard-case 'R make-random-unicode-string m n))
(define (hard-case-BM-unicode-search-successful m n)
(hard-case 'L make-random-unicode-string m n))
(define (dotimes n thunk)
(if (> n 1)
(begin (thunk) (dotimes (- n 1) thunk))
(thunk)))
(define total-work 1000000)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define heading
"test fastest (def/best naive Rabin-Karp Boyer-Moore def)")
(display heading)
(newline)
(newline)
(display "ASCII")
(newline)
(newline)
(define lengths1
'(1 10 100 500 1000 5000 10000 20000))
(define lengths2
'(1 2 3 4 5 10 20 50 100 1000))
(for-each (lambda (m)
(for-each (lambda (n)
(if (<= n m)
(hard-case-naive-ascii-search-successful m n)))
lengths2))
lengths1)
(for-each (lambda (m)
(for-each (lambda (n)
(if (<= n m)
(hard-case-BM-ascii-search-successful m n)))
lengths2))
lengths1)
(for-each (lambda (m)
(for-each (lambda (n)
(if (<= n m)
(random-ascii-search-successful m n)))
lengths2))
lengths1)
(newline)
(newline)
(display "Unicode")
(newline)
(newline)
(for-each (lambda (m)
(for-each (lambda (n)
(if (<= n m)
(hard-case-naive-unicode-search-successful m n)))
lengths2))
lengths1)
(for-each (lambda (m)
(for-each (lambda (n)
(if (<= n m)
(hard-case-BM-unicode-search-successful m n)))
lengths2))
lengths1)
(for-each (lambda (m)
(for-each (lambda (n)
(if (<= n m)
(random-unicode-search-successful m n)))
lengths2))
lengths1)
; eof