-
Notifications
You must be signed in to change notification settings - Fork 6
/
load-test.ss
145 lines (123 loc) · 4.05 KB
/
load-test.ss
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
#lang scheme
(require (planet untyped/unlib:3/log)
"snooze.ss"
"postgresql8/postgresql8.ss")
; Database connection ----------------------------
(define (test-postgresql8-database
#:server [server "localhost"]
#:port [port 5432]
#:database [database "snoozetest"]
#:username [username "snooze"]
#:password [password #f])
(make-postgresql8-database
#:server server
#:port port
#:database database
#:username username
#:password password
#:pool-connections? #t))
(current-snooze (make-snooze (test-postgresql8-database)))
; Test data --------------------------------------
(define job-size 20)
(define-entity test
([str string]
[num integer]))
(define next-id
(let ([id 0])
(lambda ()
(begin0 id (set! id (add1 id))))))
(define-struct worker (id starter thread state)
#:mutable
#:transparent
#:property prop:custom-write
(lambda (self out write?)
((if write? write display)
(vector 'worker (worker-id self) (worker-state self))
out)))
(define (start-worker starter)
(restart-worker (make-worker (next-id) starter #f #f)))
(define (restart-worker worker [kill? #f])
(if (and (worker-thread worker) (thread-running? (worker-thread worker)))
(if kill?
(begin
(kill-thread (worker-thread worker))
(worker-log worker 'killed)
(restart-worker worker))
worker)
(begin
(set-worker-state! worker #f)
(worker-log worker 'about-to-start)
(set-worker-thread! worker (thread (lambda () ((worker-starter worker) worker))))
worker)))
(define (worker-log worker state)
(set-worker-state! worker state)
(log-info* (worker-id worker)
(worker-state worker)))
(define (start-save-worker)
(start-worker
(lambda (self)
(worker-log self 'started)
(with-connection
(worker-log self 'acquired)
(with-transaction #:metadata "save-worker"
(for ([i (in-range 0 job-size)])
;(worker-log self i)
(save! (make-test (format "test~a" i) i))))
(worker-log self 'finished))
(worker-log self 'released))))
(define (start-delete-worker)
(start-worker
(lambda (self)
(worker-log self 'started)
(with-connection
(let ([num (random job-size)])
(worker-log self 'acquired)
(with-transaction #:metadata (format "Delete ~a" num)
(for-each delete! (find-tests #:num num)))
(worker-log self 'finished))
(worker-log self 'released)))))
(define (start-select-worker)
(start-worker
(lambda (self)
(worker-log self 'started)
(with-connection
(worker-log self 'acquired)
(let ([num (random job-size)])
(let-alias ([a test]
[b test])
(select-all #:what (a b)
#:from (outer a b)
#:where (or (= a.num ,num)
(= b.num ,num))))
(worker-log self 'finished))
(worker-log self 'released)))))
(define (start-any-worker)
(match (random 3)
[0 (start-save-worker)]
[1 (start-delete-worker)]
[2 (start-select-worker)]))
(define (worker-counts workers)
(let ([counts (make-hasheq)])
(for ([worker (in-vector workers)])
(let* ([state (worker-state worker)]
[count (dict-ref counts state 0)])
(dict-set! counts state (add1 count))))
counts))
; Threads ----------------------------------------
; Sleeps for the specified number of milliseconds.
;
; integer -> void
(define (sleep/ms ms)
(sync (alarm-evt (+ (current-inexact-milliseconds) ms))))
(start-log-output 'info)
(with-connection
(drop-table test)
(create-table test))
(let* ([num-workers 10]
[workers (apply vector (for/list ([i (in-range 0 num-workers)])
(start-any-worker)))])
(for ([i (in-naturals)])
(let ([j (random num-workers)])
(log-info* "counts" (worker-counts workers))
(sleep/ms 50)
(restart-worker (vector-ref workers j)))))