1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Transport; |
4
|
|
|
|
5
|
|
|
use Elastica\Document; |
6
|
|
|
use Elastica\Index; |
7
|
|
|
use Elastica\Mapping; |
8
|
|
|
use Elastica\Query; |
9
|
|
|
use Elastica\Query\MatchAll; |
10
|
|
|
use Elastica\Query\Term; |
11
|
|
|
use Elastica\Request; |
12
|
|
|
use Elastica\Test\Base as BaseTest; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @group benchmark |
16
|
|
|
* |
17
|
|
|
* @internal |
18
|
|
|
*/ |
19
|
|
|
class TransportBenchmarkTest extends BaseTest |
20
|
|
|
{ |
21
|
|
|
protected $_max = 1000; |
22
|
|
|
protected $_maxData = 20; |
23
|
|
|
protected static $_results = []; |
24
|
|
|
|
25
|
|
|
public static function tearDownAfterClass(): void |
26
|
|
|
{ |
27
|
|
|
self::printResults(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function setUp(): void |
31
|
|
|
{ |
32
|
|
|
parent::setUp(); |
33
|
|
|
$this->markTestSkipped('Benchmarks currently skipped: it has to be reworked'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @dataProvider providerTransport |
38
|
|
|
*/ |
39
|
|
|
public function testAddDocument(array $config, string $transport): void |
40
|
|
|
{ |
41
|
|
|
$index = $this->getIndex($config); |
42
|
|
|
$index->create([], [ |
43
|
|
|
'recreate' => true, |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
$times = []; |
47
|
|
|
for ($i = 0; $i < $this->_max; ++$i) { |
48
|
|
|
$data = $this->getData($i); |
49
|
|
|
$doc = new Document($i, $data); |
50
|
|
|
$result = $index->addDocument($doc); |
51
|
|
|
$times[] = $result->getQueryTime(); |
52
|
|
|
$this->assertTrue($result->isOk()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$index->refresh(); |
56
|
|
|
|
57
|
|
|
self::logResults('insert', $transport, $times); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @depends testAddDocument |
62
|
|
|
* @dataProvider providerTransport |
63
|
|
|
* |
64
|
|
|
* @param mixed $transport |
65
|
|
|
*/ |
66
|
|
|
public function testRandomRead(array $config, $transport): void |
67
|
|
|
{ |
68
|
|
|
$index = $this->getIndex($config); |
69
|
|
|
$index->search('test'); |
70
|
|
|
|
71
|
|
|
$times = []; |
72
|
|
|
for ($i = 0; $i < $this->_max; ++$i) { |
73
|
|
|
$test = \mt_rand(1, $this->_max); |
74
|
|
|
$query = new Query(); |
75
|
|
|
$query->setQuery(new MatchAll()); |
76
|
|
|
$query->setPostFilter(new Term(['test' => $test])); |
77
|
|
|
$result = $index->search($query); |
78
|
|
|
$times[] = $result->getResponse()->getQueryTime(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
self::logResults('random read', $transport, $times); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @depends testAddDocument |
86
|
|
|
* @dataProvider providerTransport |
87
|
|
|
* |
88
|
|
|
* @param mixed $transport |
89
|
|
|
*/ |
90
|
|
|
public function testBulk(array $config, $transport): void |
91
|
|
|
{ |
92
|
|
|
$index = $this->getIndex($config); |
93
|
|
|
|
94
|
|
|
$times = []; |
95
|
|
|
for ($i = 0; $i < $this->_max; ++$i) { |
96
|
|
|
$docs = []; |
97
|
|
|
for ($j = 0; $j < 10; ++$j) { |
98
|
|
|
$data = $this->getData($i.$j); |
99
|
|
|
$docs[] = new Document($i, $data); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$result = $index->addDocuments($docs); |
103
|
|
|
$times[] = $result->getQueryTime(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
self::logResults('bulk', $transport, $times); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @dataProvider providerTransport |
111
|
|
|
*/ |
112
|
|
|
public function testGetMapping(array $config, string $transport): void |
113
|
|
|
{ |
114
|
|
|
$client = $this->_getClient($config); |
115
|
|
|
$index = $client->getIndex('benchmark'); |
116
|
|
|
$index->create([], [ |
117
|
|
|
'recreate' => true, |
118
|
|
|
]); |
119
|
|
|
|
120
|
|
|
// Define mapping |
121
|
|
|
$mapping = new Mapping(); |
122
|
|
|
$mapping->setParam('_boost', ['name' => '_boost', 'null_value' => 1.0]); |
123
|
|
|
$mapping->setProperties([ |
124
|
|
|
'id' => ['type' => 'integer'], |
125
|
|
|
'user' => [ |
126
|
|
|
'type' => 'object', |
127
|
|
|
'properties' => [ |
128
|
|
|
'name' => ['type' => 'text', 'copy_to' => 'allincluded'], |
129
|
|
|
'fullName' => ['type' => 'text', 'copy_to' => 'allincluded'], |
130
|
|
|
], |
131
|
|
|
], |
132
|
|
|
'msg' => ['type' => 'text', 'copy_to' => 'allincluded'], |
133
|
|
|
'tstamp' => ['type' => 'date'], |
134
|
|
|
'location' => ['type' => 'geo_point'], |
135
|
|
|
'_boost' => ['type' => 'float'], |
136
|
|
|
'allincluded' => ['type' => 'text'], |
137
|
|
|
]); |
138
|
|
|
|
139
|
|
|
$index->setMapping($mapping); |
140
|
|
|
$index->refresh(); |
141
|
|
|
|
142
|
|
|
$times = []; |
143
|
|
|
for ($i = 0; $i < $this->_max; ++$i) { |
144
|
|
|
$response = $index->request('_mapping', Request::GET); |
145
|
|
|
$times[] = $response->getQueryTime(); |
146
|
|
|
} |
147
|
|
|
self::logResults('get mapping', $transport, $times); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function providerTransport(): iterable |
151
|
|
|
{ |
152
|
|
|
yield [[ |
153
|
|
|
'transport' => 'Http', |
154
|
|
|
'host' => $this->_getHost(), |
155
|
|
|
'port' => $this->_getPort(), |
156
|
|
|
'persistent' => false, |
157
|
|
|
], |
158
|
|
|
'Http:NotPersistent', |
159
|
|
|
]; |
160
|
|
|
yield [[ |
161
|
|
|
'transport' => 'Http', |
162
|
|
|
'host' => $this->_getHost(), |
163
|
|
|
'port' => $this->_getPort(), |
164
|
|
|
'persistent' => true, |
165
|
|
|
], |
166
|
|
|
'Http:Persistent', |
167
|
|
|
]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
protected function getIndex(array $config): Index |
171
|
|
|
{ |
172
|
|
|
$client = $this->_getClient($config); |
173
|
|
|
$index = $client->getIndex('benchmark'.self::buildUniqueId()); |
174
|
|
|
$index->create(['index' => ['number_of_shards' => 1, 'number_of_replicas' => 0]], true); |
175
|
|
|
|
176
|
|
|
return $index; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
protected function getData(string $test): array |
180
|
|
|
{ |
181
|
|
|
$data = [ |
182
|
|
|
'test' => $test, |
183
|
|
|
'name' => [], |
184
|
|
|
]; |
185
|
|
|
for ($i = 0; $i < $this->_maxData; ++$i) { |
186
|
|
|
$data['name'][] = self::buildUniqueId(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $data; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
protected static function logResults(string $name, string $transport, array $times): void |
193
|
|
|
{ |
194
|
|
|
self::$_results[$name][$transport] = [ |
195
|
|
|
'count' => \count($times), |
196
|
|
|
'max' => \max($times) * 1000, |
197
|
|
|
'min' => \min($times) * 1000, |
198
|
|
|
'mean' => (\array_sum($times) / \count($times)) * 1000, |
199
|
|
|
]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
protected static function printResults(): void |
203
|
|
|
{ |
204
|
|
|
if (!\count(self::$_results)) { |
205
|
|
|
return; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
echo \sprintf( |
209
|
|
|
"\n%-12s | %-20s | %-12s | %-12s | %-12s | %-12s\n\n", |
210
|
|
|
'NAME', |
211
|
|
|
'TRANSPORT', |
212
|
|
|
'COUNT', |
213
|
|
|
'MAX', |
214
|
|
|
'MIN', |
215
|
|
|
'MEAN' |
216
|
|
|
); |
217
|
|
|
foreach (self::$_results as $name => $values) { |
218
|
|
|
$means = []; |
219
|
|
|
foreach ($values as $times) { |
220
|
|
|
$means[] = $times['mean']; |
221
|
|
|
} |
222
|
|
|
$minMean = \min($means); |
223
|
|
|
foreach ($values as $transport => $times) { |
224
|
|
|
$perc = 0; |
225
|
|
|
|
226
|
|
|
if (0 !== $minMean) { |
227
|
|
|
$perc = (($times['mean'] - $minMean) / $minMean) * 100; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
echo \sprintf( |
231
|
|
|
"%-12s | %-20s | %-12d | %-12.2f | %-12.2f | %-12.2f | %+03.2f\n", |
232
|
|
|
$name, |
233
|
|
|
$transport, |
234
|
|
|
$times['count'], |
235
|
|
|
$times['max'], |
236
|
|
|
$times['min'], |
237
|
|
|
$times['mean'], |
238
|
|
|
$perc |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
echo "\n"; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|