forked from amal/AzaThread
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.php
227 lines (198 loc) · 4.46 KB
/
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
<?php
/**
* Performance testing
*
* @project Anizoptera CMF
* @package system.thread
*/
require __DIR__ . '/inc.thread.php';
/**
* 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
#############
// Disable to use sync mode
//CThread::$useForks = false;
// Manually specify the type of data transfer between threads
//CThread::$ipcDataMode = CThread::IPC_IGBINARY;
$data = 0; // Whether to transfer data (do not work with "work")
$work = 0; // Whether to do some work in threads
$tests = 10; // Number of tests to do
$jobs = 10000; // Number of jobs in test
$threads = 8; // Number of threads
#############
# Test
#############
/**
* Test thread
*/
class TestThreadNothing extends CThread
{
/**
* Main processing.
*
* @return mixed
*/
protected function process()
{
}
}
/**
* Test thread
*/
class TestThreadReturn extends CThread
{
/**
* Main processing.
*
* @return mixed
*/
protected function process()
{
return array(123456789, 'abcdefghigklmnopqrstuvwxyz', 123.7456328);
}
}
/**
* Test thread
*/
class TestThreadWork extends CThread
{
/**
* Main processing.
*
* @return mixed
*/
protected function process()
{
$r = null;
for ($i = 0; $i < 1000; $i++) {
$r = mt_rand(0, PHP_INT_MAX) * mt_rand(0, PHP_INT_MAX);
}
return $r;
}
}
var_dump("Threads: $threads; Tests: $tests; Starting tests...");
/** @var $thread CThread */
$thread = $work ? 'TestThreadWork' : ($data ? 'TestThreadReturn' : 'TestThreadNothing');
$pool = new CThreadPool($thread, $threads);
$arg1 = (object)array('foobarbaz' => 1234567890, 12.9876543);
$arg2 = 123/16;
$res = array();
for ($j = 0; $j < $tests; ++$j) {
$start = microtime(true);
$num = $jobs;
$i = 0;
$maxI = ceil($jobs * 1.5);
do {
while ($pool->hasWaiting()) {
if ($data) {
$pool->run($arg1, $arg2);
} else {
$pool->run();
}
}
if ($results = $pool->wait()) {
$num -= count($results);
}
$i++;
} while ($num > 0 && $i < $maxI);
$end = bcsub(microtime(true), $start, 99);
$oneJob = bcdiv($end, $jobs, 99);
$res[] = bcdiv(1, $oneJob, 99);
$jps = bcdiv(1, $oneJob);
var_dump("Threads: $threads; Jobs: $jobs; Iteration: $j; Jobs per second: $jps");
}
$sum = 0;
foreach ($res as $r) {
$sum = bcadd($sum, $r, 99);
}
$avJps = bcdiv($sum, $tests);
var_dump("Threads: $threads; Tests: $tests; Average jobs per second: $avJps");
$pool->cleanup();