forked from amal/AzaThread
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspeed_test.php
324 lines (277 loc) · 6.63 KB
/
speed_test.php
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
<?php
namespace Aza\Components\Thread;
use Aza\Components\Socket\Socket;
require __DIR__ . '/../vendor/autoload.php';
//require __DIR__ . '/../../../../example.bootstrap.php';
/**
* Performance testing
*
* @project Anizoptera CMF
* @package system.thread
* @author Amal Samally <amal.samally at gmail.com>
* @license MIT
*/
/**
* Threads speed test results in jobs per second
*
* =================================================
*
* Intel Core i3 540 3.07 Ghz (Ubuntu 11.04) results
*
* IPC (empty jobs):
*
* 20219 - 1 thread (Sync)
* 19900 - 1 thread (Sync, Data transfer)
*
* 4460 - 1 thread (Async)
* 3082 - 1 thread (Async, Data transfer)
*
* 5224 - 2 threads (Async)
* 3894 - 2 threads (Async, Data transfer)
*
* 5718 - 4 threads (Async)
* 4295 - 4 threads (Async, Data transfer)
*
* 5806 - 8 threads (Async)
* 4371 - 8 threads (Async, Data transfer)
*
* 5842 - 10 threads (Async)
* 4303 - 10 threads (Async, Data transfer)
*
* 5890 - 12 threads (Async)
* 4333 - 12 threads (Async, Data transfer)
*
* 6018 - 16 threads (Async)
* 4234 - 16 threads (Async, Data transfer)
*
* Working jobs:
*
* 553 - 1 thread (Sync)
* 330 - 1 thread (Async)
* 580 - 2 threads (Async)
* 1015 - 4 threads (Async)
* 1040 - 8 threads (Async)
* 1027 - 10 threads (Async)
* 970 - 12 threads (Async)
* 958 - 16 threads (Async)
*
* =================================================
*
* Intel Core i7 2600K 3.40 Ghz (Ubuntu 11.04 on VMware virtual machine) results
*
* IPC (empty jobs):
*
* 26394 - 1 thread (Sync)
* 25032 - 1 thread (Sync, Data transfer)
*
* 4928 - 1 thread (Async)
* 4164 - 1 thread (Async, Data transfer)
*
* 7210 - 2 threads (Async)
* 5910 - 2 threads (Async, Data transfer)
*
* 7129 - 4 threads (Async)
* 6261 - 4 threads (Async, Data transfer)
*
* 7633 - 8 threads (Async)
* 6630 - 8 threads (Async, Data transfer)
*
* 7810 - 10 threads (Async)
* 6715 - 10 threads (Async, Data transfer)
*
* 7641 - 12 threads (Async)
* 6540 - 12 threads (Async, Data transfer)
*
* 7587 - 16 threads (Async)
* 6514 - 16 threads (Async, Data transfer)
*
* Working jobs:
*
* 763 - 1 thread (Sync)
* 669 - 1 thread (Async)
* 1254 - 2 threads (Async)
* 2188 - 4 threads (Async)
* 2618 - 8 threads (Async)
* 2719 - 10 threads (Async)
* 2739 - 12 threads (Async)
* 2904 - 16 threads (Async)
* 2830 - 18 threads (Async)
* 2730 - 20 threads (Async)
*
* =================================================
*/
#############
# Settings
#############
$data = true; // Transmit data
$work = true; // Do some work
$tests = 6; // Number of iterations in tests
$jobsT = $work ? 2500 : 10000; // Number of jobs to do in one thread
$jobsP = $work ? 10000 : 20000; // Number of jobs to do in pools
$poolMin = 2; // Minimum threads number in pool to test
// Disable to use sync mode
//Thread::$useForks = false;
// Manually specify the type of data transfer between threads
//Thread::$ipcDataMode = Thread::IPC_IGBINARY;
// Use stream sockets
//Socket::$useSockets = false;
#############
# Test
#############
/**
* Test thread
*/
class TestThreadNothing extends Thread
{
/**
* Main processing.
*
* @return mixed
*/
function process() {}
}
/**
* Test thread
*/
class TestThreadReturn extends Thread
{
/**
* Main processing.
*
* @return mixed
*/
function process()
{
return array(123456789, 'abcdefghigklmnopqrstuvwxyz', 123.7456328);
}
}
/**
* Test thread
*/
class TestThreadWork extends Thread
{
/**
* Main processing.
*
* @return mixed
*/
function process()
{
$r = null;
$i = 1000;
while ($i--) {
$r = mt_rand(0, PHP_INT_MAX) * mt_rand(0, PHP_INT_MAX);
}
return $r;
}
}
/**
* Prints message
*
* @parma string $msg Message
* @parma int $newline Newlines count
*/
$print = function($msg = '', $newline = 1) {
echo $msg . str_repeat(PHP_EOL, (int)$newline);
@ob_flush(); @flush();
};
$line = str_repeat('-', 80);
if (!Thread::$useForks) {
$print(
'ERROR: You need Forks, LibEvent, PCNTL and POSIX'
.' support with CLI sapi to fully test Threads'
);
return;
}
/** @var $threadClass Thread */
$threadClass = $work ? 'TestThreadWork' : ($data ? 'TestThreadReturn' : 'TestThreadNothing');
$threadClass = __NAMESPACE__ . '\\' . $threadClass;
$arg1 = (object)array('foobarbaz' => 1234567890, 12.9876543);
$arg2 = 123/16;
// Test one thread
$print($line);
$print("One thread test; Jobs: $jobsT; Iterations: $tests");
$print($line, 2);
/** @var $thread Thread */
$thread = new $threadClass;
$thread->wait();
$res = array();
for ($j = 0; $j < $tests; ++$j) {
$start = microtime(true);
for ($i = 0; $i < $jobsT; $i++) {
$data ? $thread->run($arg1, $arg2) : $thread->run();
$thread->wait()->getResult();
}
$end = bcsub(microtime(true), $start, 99);
$oneJob = bcdiv($end, $jobsT, 99);
$res[] = bcdiv(1, $oneJob, 99);
$jps = bcdiv(1, $oneJob);
$print("Iteration: ".($j+1)."; Jobs per second: $jps");
}
$sum = 0;
foreach ($res as $r) {
$sum = bcadd($sum, $r, 99);
}
$averageOneThreadJps = bcdiv($sum, $tests);
$print("Average jobs per second: $averageOneThreadJps", 3);
$thread->cleanup();
// Test pools
$bestJps = 0;
$bestThreadsNum = 0;
$regression = 0;
$lastJps = 0;
$print($line);
$print("Pool test; Jobs: $jobsP; Iterations: $tests");
$print($line, 2);
$threads = $poolMin;
$pool = new ThreadPool($threadClass, $threads);
do {
$print("Threads: $threads");
$print($line);
$res = array();
for ($j = 0; $j < $tests; ++$j) {
$start = microtime(true);
$num = $jobsP;
$i = 0;
$maxI = ceil($jobsP * 1.5);
do {
while ($pool->hasWaiting()) {
$data ? $pool->run($arg1, $arg2) : $pool->run();
}
if ($results = $pool->wait()) {
$num -= count($results);
}
$i++;
} while ($num > 0 && $i < $maxI);
$end = bcsub(microtime(true), $start, 99);
$oneJob = bcdiv($end, $jobsP, 99);
$res[] = bcdiv(1, $oneJob, 99);
$jps = bcdiv(1, $oneJob);
$print("Iteration: ".($j+1)."; Jobs per second: $jps");
}
$sum = 0;
foreach ($res as $r) {
$sum = bcadd($sum, $r, 99);
}
$avJps = bcdiv($sum, $tests);
$print("Average jobs per second: $avJps", 2);
if ($bestJps < $avJps) {
$bestJps = $avJps;
$bestThreadsNum = $threads;
}
if ($avJps < $bestJps || $avJps < $lastJps) {
$regression++;
}
if ($regression >= 3) {
break;
}
$lastJps = $avJps;
// Increase number of threads
$threads++;
$pool->setMaxThreads($threads);
} while (true);
$pool->cleanup();
$print('', 3);
$print("Best number of threads for your system: {$bestThreadsNum} ({$bestJps} jobs per second)");
$boost = bcdiv($bestJps, bcdiv($averageOneThreadJps, 100, 99), 2);
$print("Performance boost in relation to a single thread: {$boost}%");