Completed
Push — master ( 9146c8...39ecb4 )
by Nicolas
04:52
created

TransportBenchmarkTest::testBulk()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
rs 9.6333
cc 3
nc 3
nop 2
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
        $this->_checkTransport($config, $transport);
42
        $index = $this->getIndex($config);
43
        $index->create([], true);
44
45
        $times = [];
46
        for ($i = 0; $i < $this->_max; ++$i) {
47
            $data = $this->getData($i);
48
            $doc = new Document($i, $data);
49
            $result = $index->addDocument($doc);
50
            $times[] = $result->getQueryTime();
51
            $this->assertTrue($result->isOk());
52
        }
53
54
        $index->refresh();
55
56
        self::logResults('insert', $transport, $times);
57
    }
58
59
    /**
60
     * @depends testAddDocument
61
     * @dataProvider providerTransport
62
     *
63
     * @param mixed $transport
64
     */
65
    public function testRandomRead(array $config, $transport): void
66
    {
67
        $this->_checkTransport($config, $transport);
68
        $index = $this->getIndex($config);
69
        $index->search('test');
70
71
        $times = [];
72
        for ($i = 0; $i < $this->_max; ++$i) {
73
            $test = \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
        $this->_checkTransport($config, $transport);
93
        $index = $this->getIndex($config);
94
95
        $times = [];
96
        for ($i = 0; $i < $this->_max; ++$i) {
97
            $docs = [];
98
            for ($j = 0; $j < 10; ++$j) {
99
                $data = $this->getData($i.$j);
100
                $docs[] = new Document($i, $data);
101
            }
102
103
            $result = $index->addDocuments($docs);
104
            $times[] = $result->getQueryTime();
105
        }
106
107
        self::logResults('bulk', $transport, $times);
108
    }
109
110
    /**
111
     * @dataProvider providerTransport
112
     */
113
    public function testGetMapping(array $config, string $transport): void
114
    {
115
        $this->_checkTransport($config, $transport);
116
117
        $client = $this->_getClient($config);
118
        $index = $client->getIndex('benchmark');
119
        $index->create([], true);
120
121
        // Define mapping
122
        $mapping = new Mapping();
123
        $mapping->setParam('_boost', ['name' => '_boost', 'null_value' => 1.0]);
124
        $mapping->setProperties([
125
            'id' => ['type' => 'integer'],
126
            'user' => [
127
                'type' => 'object',
128
                'properties' => [
129
                    'name' => ['type' => 'text', 'copy_to' => 'allincluded'],
130
                    'fullName' => ['type' => 'text', 'copy_to' => 'allincluded'],
131
                ],
132
            ],
133
            'msg' => ['type' => 'text', 'copy_to' => 'allincluded'],
134
            'tstamp' => ['type' => 'date'],
135
            'location' => ['type' => 'geo_point'],
136
            '_boost' => ['type' => 'float'],
137
            'allincluded' => ['type' => 'text'],
138
        ]);
139
140
        $index->setMapping($mapping);
141
        $index->refresh();
142
143
        $times = [];
144
        for ($i = 0; $i < $this->_max; ++$i) {
145
            $response = $index->request('_mapping', Request::GET);
146
            $times[] = $response->getQueryTime();
147
        }
148
        self::logResults('get mapping', $transport, $times);
149
    }
150
151
    public function providerTransport(): iterable
152
    {
153
        yield [[
154
            'transport' => 'Http',
155
            'host' => $this->_getHost(),
156
            'port' => $this->_getPort(),
157
            'persistent' => false,
158
        ],
159
            'Http:NotPersistent',
160
        ];
161
        yield [[
162
            'transport' => 'Http',
163
            'host' => $this->_getHost(),
164
            'port' => $this->_getPort(),
165
            'persistent' => true,
166
        ],
167
            'Http:Persistent',
168
        ];
169
    }
170
171
    protected function getIndex(array $config): Index
172
    {
173
        $client = $this->_getClient($config);
174
        $index = $client->getIndex('benchmark'.self::buildUniqueId());
175
        $index->create(['index' => ['number_of_shards' => 1, 'number_of_replicas' => 0]], true);
176
177
        return $index;
178
    }
179
180
    protected function getData(string $test): array
181
    {
182
        $data = [
183
            'test' => $test,
184
            'name' => [],
185
        ];
186
        for ($i = 0; $i < $this->_maxData; ++$i) {
187
            $data['name'][] = self::buildUniqueId();
188
        }
189
190
        return $data;
191
    }
192
193
    protected static function logResults(string $name, string $transport, array $times): void
194
    {
195
        self::$_results[$name][$transport] = [
196
            'count' => \count($times),
197
            'max' => \max($times) * 1000,
198
            'min' => \min($times) * 1000,
199
            'mean' => (\array_sum($times) / \count($times)) * 1000,
200
        ];
201
    }
202
203
    protected static function printResults(): void
204
    {
205
        if (!\count(self::$_results)) {
206
            return;
207
        }
208
209
        echo \sprintf(
210
            "\n%-12s | %-20s | %-12s | %-12s | %-12s | %-12s\n\n",
211
            'NAME',
212
            'TRANSPORT',
213
            'COUNT',
214
            'MAX',
215
            'MIN',
216
            'MEAN'
217
        );
218
        foreach (self::$_results as $name => $values) {
219
            $means = [];
220
            foreach ($values as $times) {
221
                $means[] = $times['mean'];
222
            }
223
            $minMean = \min($means);
224
            foreach ($values as $transport => $times) {
225
                $perc = 0;
226
227
                if (0 !== $minMean) {
228
                    $perc = (($times['mean'] - $minMean) / $minMean) * 100;
229
                }
230
231
                echo \sprintf(
232
                    "%-12s | %-20s | %-12d | %-12.2f | %-12.2f | %-12.2f | %+03.2f\n",
233
                    $name,
234
                    $transport,
235
                    $times['count'],
236
                    $times['max'],
237
                    $times['min'],
238
                    $times['mean'],
239
                    $perc
240
                );
241
            }
242
            echo "\n";
243
        }
244
    }
245
246
    protected function _checkTransport(array $config, $transport): void
247
    {
248
        $this->_checkConnection($config['host'], $config['port']);
249
    }
250
}
251