|
1 "{ Package: 'jv:calipel/s/benchmarks/micro' }" |
|
2 |
|
3 "{ NameSpace: Smalltalk }" |
|
4 |
|
5 Benchmark subclass:#BenchmarkLocking |
|
6 instanceVariableNames:'nesting threads lock' |
|
7 classVariableNames:'' |
|
8 poolDictionaries:'' |
|
9 category:'CalipeL-S-Benchmarks-Micro' |
|
10 ! |
|
11 |
|
12 !BenchmarkLocking class methodsFor:'queries'! |
|
13 |
|
14 isAbstract |
|
15 "Return if this class is an abstract class. |
|
16 True is returned here for myself only; false for subclasses. |
|
17 Abstract subclasses must redefine this again." |
|
18 |
|
19 ^ self == BenchmarkLocking. |
|
20 ! ! |
|
21 |
|
22 !BenchmarkLocking methodsFor:'benchmarks'! |
|
23 |
|
24 benchmarkCritical |
|
25 <benchmark: '#critical:'> |
|
26 |
|
27 | blocker iterations body | |
|
28 |
|
29 nesting isNil ifTrue:[ nesting := 1 ]. |
|
30 iterations := 10000000. |
|
31 blocker := Semaphore new: (threads - 1) negated. |
|
32 body := [ |
|
33 iterations timesRepeat:[ self workloadCritical: nesting ]. |
|
34 blocker signal. |
|
35 ]. |
|
36 1 to: threads do:[:i | |
|
37 | t | |
|
38 |
|
39 t := body newProcess. |
|
40 t name: 'Benchmark ''', self class name, ' >> #critical:'', thread ', i printString. |
|
41 t resume. |
|
42 ]. |
|
43 blocker wait. |
|
44 |
|
45 " |
|
46 BenchmarkRecursionLock run: #benchmarkCritical. |
|
47 BenchmarkSemaphore run: #benchmarkCritical |
|
48 " |
|
49 |
|
50 "Created: / 25-08-2017 / 08:46:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
51 "Modified: / 28-08-2017 / 21:32:46 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
52 |
|
53 ! |
|
54 |
|
55 benchmarkCriticalN |
|
56 <benchmark: '#critical: (no work)'> |
|
57 |
|
58 | blocker iterations body | |
|
59 |
|
60 nesting isNil ifTrue:[ nesting := 1 ]. |
|
61 iterations := 10000000. |
|
62 blocker := Semaphore new: (threads - 1) negated. |
|
63 body := [ |
|
64 iterations timesRepeat:[ self justpureCritical: nesting ]. |
|
65 blocker signal. |
|
66 ]. |
|
67 1 to: threads do:[:i | |
|
68 | t | |
|
69 |
|
70 t := body newProcess. |
|
71 t name: 'Benchmark ''', self class name, ' >> #critical:'', thread ', i printString. |
|
72 t resume. |
|
73 ]. |
|
74 blocker wait. |
|
75 |
|
76 " |
|
77 BenchmarkRecursionLock run: #benchmarkCritical. |
|
78 BenchmarkSemaphore run: #benchmarkCritical |
|
79 " |
|
80 |
|
81 "Created: / 25-08-2017 / 08:46:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
82 "Modified: / 28-08-2017 / 21:32:46 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
83 |
|
84 ! |
|
85 |
|
86 |
|
87 benchmarkBase |
|
88 <benchmark: 'workload (no locks)'> |
|
89 |
|
90 | blocker iterations body | |
|
91 |
|
92 nesting isNil ifTrue:[ nesting := 1 ]. |
|
93 iterations := 10000000. |
|
94 blocker := Semaphore new: (threads - 1) negated. |
|
95 body := [ |
|
96 iterations timesRepeat:[ self workloadBase: nesting ]. |
|
97 blocker signal. |
|
98 ]. |
|
99 1 to: threads do:[:i | |
|
100 | t | |
|
101 |
|
102 t := body newProcess. |
|
103 t name: 'Benchmark ''', self class name, ' >> benchmarkBase, thread ', i printString. |
|
104 t resume. |
|
105 ]. |
|
106 blocker wait. |
|
107 |
|
108 " |
|
109 BenchmarkRecursionLock run: #benchmarkCritical. |
|
110 BenchmarkSemaphore run: #benchmarkCritical |
|
111 " |
|
112 |
|
113 "Created: / 25-08-2017 / 08:46:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
114 "Modified: / 28-08-2017 / 21:32:46 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
115 |
|
116 ! ! |
|
117 |
|
118 !BenchmarkLocking methodsFor:'parameters'! |
|
119 |
|
120 threads: anInteger |
|
121 <parameter: #threads type: #Integer values: #(1 2 5)> |
|
122 threads := anInteger. |
|
123 |
|
124 "Created: / 25-08-2017 / 08:52:54 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
125 ! ! |
|
126 |
|
127 !BenchmarkLocking methodsFor:'private'! |
|
128 |
|
129 justpureCritical: n |
|
130 lock critical:[ |
|
131 n > 1 ifTrue:[ |
|
132 self justpureCritical: n - 1. |
|
133 ]. |
|
134 ]. |
|
135 |
|
136 "Created: / 25-08-2017 / 08:55:53 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
137 "Modified: / 28-08-2017 / 21:29:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
138 ! |
|
139 |
|
140 |
|
141 workloadCritical: n |
|
142 lock critical:[ |
|
143 self doSomeWork. |
|
144 n > 1 ifTrue:[ |
|
145 self workloadCritical: n - 1. |
|
146 ]. |
|
147 ]. |
|
148 |
|
149 "Created: / 25-08-2017 / 08:55:53 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
150 "Modified: / 28-08-2017 / 21:29:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
151 ! |
|
152 |
|
153 workloadBase: n |
|
154 self doSomeWork. |
|
155 n > 1 ifTrue:[ |
|
156 self workloadBase: n - 1. |
|
157 ]. |
|
158 |
|
159 "Created: / 25-08-2017 / 08:55:53 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
160 "Modified: / 28-08-2017 / 21:29:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
161 |
|
162 ! |
|
163 |
|
164 doSomeWork |
|
165 1 perform: #+ with: 1 |
|
166 |
|
167 "Created: / 25-08-2017 / 09:04:23 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
168 "Modified: / 28-08-2017 / 21:11:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
169 ! ! |
|
170 |