summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/benchmark/TransportTest.php
blob: 42d6ac0c7ae1e7c84204c9f6f58e516353fd35a2 (plain)
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
<?php

use Elastica\Client;
use Elastica\Request;
use Elastica\Document;
use Elastica\Query;
use Elastica\Type\Mapping;
use Elastica\Query\MatchAll as MatchAllQuery;
use Elastica\Filter\Term as TermFilter;

class TransportTest extends \PHPUnit_Framework_TestCase
{
    protected $_max = 1000;

    protected $_maxData = 20;

    static protected $_results = array();

    public static function setUpBeforeClass()
    {
        if (!defined('DEBUG')) {
            define('DEBUG', true);
        } else if (false == DEBUG) {
            self::markTestIncomplete('DEBUG const is set to false, it prevents query time measuring.');
        }
    }

    public static function tearDownAfterClass()
    {
        self::printResults();
    }

    /**
     * @param array $config
     * @return \Elastica\Type
     */
    protected function getType(array $config)
    {
        $client = new Client($config);
        $index = $client->getIndex('test');
        return $index->getType('test');
    }

    /**
     * @dataProvider providerTransport
     */
    public function testAddDocument(array $config, $transport)
    {
        $type = $this->getType($config);
        $index = $type->getIndex();
        $index->create(array(), true);

        $times = array();
        for ($i = 0; $i < $this->_max; $i++) {
            $data = $this->getData($i);
            $doc = new Document($i, $data);
            $result = $type->addDocument($doc);
            $times[] = $result->getQueryTime();
            $this->assertTrue($result->isOk());
        }

        $index->refresh();

        self::logResults('insert', $transport, $times);
    }

    /**
     * @depends testAddDocument
     * @dataProvider providerTransport
     */
    public function testRandomRead(array $config, $transport)
    {
        $type = $this->getType($config);

        $type->search('test');

        $times = array();
        for ($i = 0; $i < $this->_max; $i++) {
            $test = rand(1, $this->_max);
            $query = new Query();
            $query->setQuery(new MatchAllQuery());
            $query->setFilter(new TermFilter(array('test' => $test)));
            $result = $type->search($query);
            $times[] = $result->getResponse()->getQueryTime();
        }

        self::logResults('random read', $transport, $times);
    }

    /**
     * @depends testAddDocument
     * @dataProvider providerTransport
     */
    public function testBulk(array $config, $transport)
    {
        $type = $this->getType($config);

        $times = array();
        for ($i = 0; $i < $this->_max; $i++) {
            $docs = array();
            for ($j = 0; $j < 10; $j++) {
                $data = $this->getData($i . $j);
                $docs[] = new Document($i, $data);
            }

            $result = $type->addDocuments($docs);
            $times[] = $result->getQueryTime();
        }

        self::logResults('bulk', $transport, $times);
    }

    /**
     * @dataProvider providerTransport
     */
    public function testGetMapping(array $config, $transport)
    {
        $client = new Client($config);
        $index = $client->getIndex('test');
        $index->create(array(), true);
        $type = $index->getType('mappingTest');

        // Define mapping
        $mapping = new Mapping();
        $mapping->setParam('_boost', array('name' => '_boost', 'null_value' => 1.0));
        $mapping->setProperties(array(
            'id' => array('type' => 'integer', 'include_in_all' => FALSE),
            'user' => array(
                'type' => 'object',
                'properties' => array(
                    'name' => array('type' => 'string', 'include_in_all' => TRUE),
                    'fullName' => array('type' => 'string', 'include_in_all' => TRUE)
                ),
            ),
            'msg' => array('type' => 'string', 'include_in_all' => TRUE),
            'tstamp' => array('type' => 'date', 'include_in_all' => FALSE),
            'location'=> array('type' => 'geo_point', 'include_in_all' => FALSE),
            '_boost' => array('type' => 'float', 'include_in_all' => FALSE)
        ));

        $type->setMapping($mapping);
        $index->refresh();

        $times = array();
        for ($i = 0; $i < $this->_max; $i++) {
            $response = $type->request('_mapping', Request::GET);
            $times[] = $response->getQueryTime();
        }
        self::logResults('get mapping', $transport, $times);
    }

    public function providerTransport()
    {
        return array(
            array(
                array(
                    'transport' => 'Http',
                    'host' => 'localhost',
                    'port' => 9200,
                    'persistent' => false,
                ),
                'Http:NotPersistent'
            ),
            array(
                array(
                    'transport' => 'Http',
                    'host' => 'localhost',
                    'port' => 9200,
                    'persistent' => true,
                ),
                'Http:Persistent'
            ),
            array(
                array(
                    'transport' => 'Thrift',
                    'host' => 'localhost',
                    'port' => 9500,
                    'config' => array(
                        'framedTransport' => false,
                    ),
                ),
                'Thrift:Buffered'
            ),
        );
    }

    /**
     * @param string $test
     * @return array
     */
    protected function getData($test)
    {
        $data = array(
            'test' => $test,
            'name' => array(),
        );
        for ($i = 0; $i < $this->_maxData; $i++) {
            $data['name'][] = uniqid();
        }
        return $data;
    }

    /**
     * @param $name
     * @param $transport
     * @param array $times
     */
    protected static function logResults($name, $transport, array $times)
    {
        self::$_results[$name][$transport] = array(
            'count' => count($times),
            'max'   => max($times) * 1000,
            'min'   => min($times) * 1000,
            'mean'  => (array_sum($times) / count($times)) * 1000,
        );
    }

    protected static function printResults()
    {
        echo sprintf(
            "\n%-12s | %-20s | %-12s | %-12s | %-12s | %-12s\n\n",
            'NAME',
            'TRANSPORT',
            'COUNT',
            'MAX',
            'MIN',
            'MEAN',
            '%'
        );
        foreach (self::$_results as $name => $values) {
            $means = array();
            foreach ($values as $times) {
                $means[] = $times['mean'];
            }
            $minMean = min($means);
            foreach ($values as $transport => $times) {
                $perc = (($times['mean'] - $minMean) / $minMean) * 100;
                echo sprintf(
                    "%-12s | %-20s | %-12d | %-12.2f | %-12.2f | %-12.2f | %+03.2f\n",
                    $name,
                    $transport,
                    $times['count'],
                    $times['max'],
                    $times['min'],
                    $times['mean'],
                    $perc
                );
            }
            echo "\n";
        }
    }
}