diff options
Diffstat (limited to 'vendor/ruflin/elastica/test/lib/Elastica/Test/Transport')
7 files changed, 1174 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/AbstractTransportTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/AbstractTransportTest.php new file mode 100644 index 00000000..20573cc7 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/AbstractTransportTest.php @@ -0,0 +1,80 @@ +<?php +namespace Elastica\Test\Transport; + +use Elastica\Connection; +use Elastica\Transport\AbstractTransport; +use Elastica\Transport\Http; + +class AbstractTransportTest extends \PHPUnit_Framework_TestCase +{ + /** + * Return transport configuration and the expected HTTP method. + * + * @return array[] + */ + public function getValidDefinitions() + { + $connection = new Connection(); + + return array( + array('Http'), + array(array('type' => 'Http')), + array(array('type' => new Http())), + array(new Http()), + ); + } + + /** + * @group unit + * @dataProvider getValidDefinitions + */ + public function testCanCreateTransportInstances($transport) + { + $connection = new Connection(); + $params = array(); + $transport = AbstractTransport::create($transport, $connection, $params); + $this->assertInstanceOf('Elastica\Transport\AbstractTransport', $transport); + $this->assertSame($connection, $transport->getConnection()); + } + + public function getInvalidDefinitions() + { + return array( + array(array('transport' => 'Http')), + array('InvalidTransport'), + ); + } + + /** + * @group unit + * @dataProvider getInvalidDefinitions + * @expectedException Elastica\Exception\InvalidException + * @expectedExceptionMessage Invalid transport + */ + public function testThrowsExecptionOnInvalidTransportDefinition($transport) + { + AbstractTransport::create($transport, new Connection()); + } + + /** + * @group unit + */ + public function testCanInjectParamsWhenUsingArray() + { + $connection = new Connection(); + $params = array( + 'param1' => 'some value', + 'param3' => 'value3', + ); + + $transport = AbstractTransport::create(array( + 'type' => 'Http', + 'param1' => 'value1', + 'param2' => 'value2', + ), $connection, $params); + + $this->assertSame('value1', $transport->getParam('param1')); + $this->assertSame('value2', $transport->getParam('param2')); + $this->assertSame('value3', $transport->getParam('param3')); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/GuzzleTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/GuzzleTest.php new file mode 100644 index 00000000..04e7ee2d --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/GuzzleTest.php @@ -0,0 +1,180 @@ +<?php +namespace Elastica\Test\Transport; + +use Elastica\Document; +use Elastica\Query; +use Elastica\ResultSet; +use Elastica\Test\Base as BaseTest; + +class GuzzleTest extends BaseTest +{ + public static function setUpBeforeClass() + { + if (!class_exists('GuzzleHttp\\Client')) { + self::markTestSkipped('guzzlehttp/guzzle package should be installed to run guzzle transport tests'); + } + } + + /** + * Return transport configuration and the expected HTTP method. + * + * @return array[] + */ + public function getConfig() + { + return array( + array( + array('persistent' => false, 'transport' => 'Guzzle'), + 'GET', + ), + array( + array('persistent' => false, 'transport' => array('type' => 'Guzzle', 'postWithRequestBody' => false)), + 'GET', + ), + array( + array('persistent' => false, 'transport' => array('type' => 'Guzzle', 'postWithRequestBody' => true)), + 'POST', + ), + ); + } + + /** + * @group functional + * @dataProvider getConfig + */ + public function testDynamicHttpMethodBasedOnConfigParameter(array $config, $httpMethod) + { + $client = $this->_getClient($config); + + $index = $client->getIndex('dynamic_http_method_test'); + $index->create(array(), true); + $type = $index->getType('test'); + $type->addDocument(new Document(1, array('test' => 'test'))); + $index->refresh(); + $resultSet = $index->search('test'); + $info = $resultSet->getResponse()->getTransferInfo(); + $this->assertStringStartsWith($httpMethod, $info['request_header']); + } + + /** + * @group functional + * @dataProvider getConfig + */ + public function testDynamicHttpMethodOnlyAffectsRequestsWithBody(array $config, $httpMethod) + { + $client = $this->_getClient($config); + + $status = $client->getStatus(); + $info = $status->getResponse()->getTransferInfo(); + $this->assertStringStartsWith('GET', $info['request_header']); + } + + /** + * @group functional + */ + public function testWithEnvironmentalProxy() + { + putenv('http_proxy='.$this->_getProxyUrl().'/'); + + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + + $client->getConnection()->setProxy(null); // will not change anything + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + + putenv('http_proxy='); + } + + /** + * @group functional + */ + public function testWithEnabledEnvironmentalProxy() + { + putenv('http_proxy='.$this->_getProxyUrl403().'/'); + + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(403, $transferInfo['http_code']); + + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); + $client->getConnection()->setProxy(''); + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + + putenv('http_proxy='); + } + + /** + * @group functional + */ + public function testWithProxy() + { + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); + $client->getConnection()->setProxy($this->_getProxyUrl()); + + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + } + + /** + * @group functional + */ + public function testWithoutProxy() + { + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); + $client->getConnection()->setProxy(''); + + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + } + + /** + * @group functional + */ + public function testBodyReuse() + { + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); + + $index = $client->getIndex('elastica_body_reuse_test'); + $index->create(array(), true); + $this->_waitForAllocation($index); + + $type = $index->getType('test'); + $type->addDocument(new Document(1, array('test' => 'test'))); + + $index->refresh(); + + $resultSet = $index->search(array( + 'query' => array( + 'query_string' => array( + 'query' => 'pew pew pew', + ), + ), + )); + + $this->assertEquals(0, $resultSet->getTotalHits()); + + $response = $index->request('/_search', 'POST'); + $resultSet = new ResultSet($response, Query::create(array())); + + $this->assertEquals(1, $resultSet->getTotalHits()); + } + + /** + * @group unit + * @expectedException Elastica\Exception\Connection\GuzzleException + */ + public function testInvalidConnection() + { + $client = $this->_getClient(array('transport' => 'Guzzle', 'port' => 4500, 'persistent' => false)); + $response = $client->request('_status', 'GET'); + } + + protected function tearDown() + { + parent::tearDown(); + putenv('http_proxy='); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/HttpTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/HttpTest.php new file mode 100644 index 00000000..53ee105f --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/HttpTest.php @@ -0,0 +1,246 @@ +<?php +namespace Elastica\Test\Transport; + +use Elastica\Document; +use Elastica\Query; +use Elastica\ResultSet; +use Elastica\Test\Base as BaseTest; + +class HttpTest extends BaseTest +{ + /** + * Return transport configuration and the expected HTTP method. + * + * @return array[] + */ + public function getConfig() + { + return array( + array( + array('transport' => 'Http', 'curl' => array(CURLINFO_HEADER_OUT => true)), + 'GET', + ), + array( + array('transport' => array('type' => 'Http', 'postWithRequestBody' => false, 'curl' => array(CURLINFO_HEADER_OUT => true))), + 'GET', + ), + array( + array('transport' => array('type' => 'Http', 'postWithRequestBody' => true, 'curl' => array(CURLINFO_HEADER_OUT => true))), + 'POST', + ), + ); + } + + /** + * @group functional + * @dataProvider getConfig + */ + public function testDynamicHttpMethodBasedOnConfigParameter(array $config, $httpMethod) + { + $client = $this->_getClient($config); + + $index = $client->getIndex('dynamic_http_method_test'); + $index->create(array(), true); + $this->_waitForAllocation($index); + + $type = $index->getType('test'); + $type->addDocument(new Document(1, array('test' => 'test'))); + + $index->refresh(); + + $resultSet = $index->search('test'); + + $info = $resultSet->getResponse()->getTransferInfo(); + $this->assertStringStartsWith($httpMethod, $info['request_header']); + } + + /** + * @group functional + * @dataProvider getConfig + */ + public function testDynamicHttpMethodOnlyAffectsRequestsWithBody(array $config, $httpMethod) + { + $client = $this->_getClient($config); + + $status = $client->getStatus(); + $info = $status->getResponse()->getTransferInfo(); + $this->assertStringStartsWith('GET', $info['request_header']); + } + + /** + * @group functional + */ + public function testCurlNobodyOptionIsResetAfterHeadRequest() + { + $client = $this->_getClient(); + $index = $client->getIndex('curl_test'); + $index->create(array(), true); + $this->_waitForAllocation($index); + + $type = $index->getType('item'); + // Force HEAD request to set CURLOPT_NOBODY = true + $index->exists(); + + $id = 1; + $data = array('id' => $id, 'name' => 'Item 1'); + $doc = new \Elastica\Document($id, $data); + + $type->addDocument($doc); + + $index->refresh(); + + $doc = $type->getDocument($id); + + // Document should be retrieved correctly + $this->assertSame($data, $doc->getData()); + $this->assertEquals($id, $doc->getId()); + } + + /** + * @group functional + */ + public function testUnicodeData() + { + $client = $this->_getClient(); + $index = $client->getIndex('curl_test'); + $index->create(array(), true); + $this->_waitForAllocation($index); + + $type = $index->getType('item'); + + // Force HEAD request to set CURLOPT_NOBODY = true + $index->exists(); + + $id = 22; + $data = array('id' => $id, 'name' => ' + Сегодня, я вижу, особенно грустен твой взгляд, / + И руки особенно тонки, колени обняв. / + Послушай: далеко, далеко, на озере Чад / + Изысканный бродит жираф.'); + + $doc = new \Elastica\Document($id, $data); + + $type->addDocument($doc); + + $index->refresh(); + + $doc = $type->getDocument($id); + + // Document should be retrieved correctly + $this->assertSame($data, $doc->getData()); + $this->assertEquals($id, $doc->getId()); + } + + /** + * @group functional + */ + public function testWithEnvironmentalProxy() + { + putenv('http_proxy='.$this->_getProxyUrl().'/'); + + $client = $this->_getClient(); + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + + $client->getConnection()->setProxy(null); // will not change anything + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + + putenv('http_proxy='); + } + + /** + * @group functional + */ + public function testWithEnabledEnvironmentalProxy() + { + putenv('http_proxy='.$this->_getProxyUrl403().'/'); + $client = $this->_getClient(); + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(403, $transferInfo['http_code']); + $client = $this->_getClient(); + $client->getConnection()->setProxy(''); + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + putenv('http_proxy='); + } + + /** + * @group functional + */ + public function testWithProxy() + { + $client = $this->_getClient(); + $client->getConnection()->setProxy($this->_getProxyUrl()); + + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + } + + /** + * @group functional + */ + public function testWithoutProxy() + { + $client = $this->_getClient(); + $client->getConnection()->setProxy(''); + + $transferInfo = $client->request('/_nodes')->getTransferInfo(); + $this->assertEquals(200, $transferInfo['http_code']); + } + + /** + * @group functional + */ + public function testBodyReuse() + { + $client = $this->_getClient(); + + $index = $client->getIndex('elastica_body_reuse_test'); + $index->create(array(), true); + $this->_waitForAllocation($index); + + $type = $index->getType('test'); + $type->addDocument(new Document(1, array('test' => 'test'))); + + $index->refresh(); + + $resultSet = $index->search(array( + 'query' => array( + 'query_string' => array( + 'query' => 'pew pew pew', + ), + ), + )); + + $this->assertEquals(0, $resultSet->getTotalHits()); + + $response = $index->request('/_search', 'POST'); + $resultSet = new ResultSet($response, Query::create(array())); + + $this->assertEquals(1, $resultSet->getTotalHits()); + } + + /** + * @group functional + */ + public function testPostWith0Body() + { + $client = $this->_getClient(); + + $index = $client->getIndex('elastica_0_body'); + $index->create(array(), true); + $this->_waitForAllocation($index); + $index->refresh(); + + $tokens = $index->analyze('0'); + + $this->assertNotEmpty($tokens); + } + + protected function tearDown() + { + parent::tearDown(); + putenv('http_proxy='); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/MemcacheTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/MemcacheTest.php new file mode 100644 index 00000000..30897073 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/MemcacheTest.php @@ -0,0 +1,176 @@ +<?php +namespace Elastica\Test\Transport; + +use Elastica\Document; +use Elastica\Query; +use Elastica\Query\QueryString; +use Elastica\Request; +use Elastica\Test\Base as BaseTest; + +class MemcacheTest extends BaseTest +{ + public static function setUpBeforeClass() + { + if (!extension_loaded('Memcache')) { + self::markTestSkipped('pecl/memcache must be installed to run this test case'); + } + } + + protected function _getMemcacheClient() + { + return $this->_getClient(array( + 'host' => $this->_getHost(), + 'port' => 11211, + 'transport' => 'Memcache', + )); + } + + /** + * @group functional + */ + public function testConstruct() + { + $client = $this->_getMemcacheClient(); + $this->assertEquals($this->_getHost(), $client->getConnection()->getHost()); + $this->assertEquals(11211, $client->getConnection()->getPort()); + } + + /** + * @group functional + */ + public function testCreateDocument() + { + $index = $this->_createIndex(); + $this->_waitForAllocation($index); + $type = $index->getType('foo'); + + // Create document + $document = new Document(1, array('username' => 'John Doe')); + $type->addDocument($document); + $index->refresh(); + + // Check it was saved + $document = $type->getDocument(1); + $this->assertEquals('John Doe', $document->get('username')); + } + + /** + * @group functional + * @expectedException Elastica\Exception\NotFoundException + */ + public function testDeleteDocument() + { + $index = $this->_createIndex(); + $this->_waitForAllocation($index); + $type = $index->getType('foo'); + + // Create document + $document = new Document(1, array('username' => 'John Doe')); + $type->addDocument($document); + $index->refresh(); + + // Delete document + $type->deleteById(1); + + // Check if document is not exists + $document = $type->getDocument(1); + } + + /** + * @group functional + */ + public function testUpdateDocument() + { + $index = $this->_createIndex(); + $this->_waitForAllocation($index); + $type = $index->getType('foo'); + + // Create document + $document = new Document(1, array('username' => 'John Doe')); + $type->addDocument($document); + $index->refresh(); + + // Check it was saved + $savedDocument = $type->getDocument(1); + $this->assertEquals('John Doe', $savedDocument->get('username')); + + // Update document + $newDocument = new Document(1, array('username' => 'Doe John')); + $type->updateDocument($newDocument); + $index->refresh(); + + // Check it was updated + $newSavedDocument = $type->getDocument(1); + $this->assertEquals('Doe John', $newSavedDocument->get('username')); + } + + /** + * @group functional + */ + public function testSearchDocument() + { + $index = $this->_createIndex(); + $this->_waitForAllocation($index); + $type = $index->getType('fruits'); + + // Create documents + $docs = array( + new Document(1, array('name' => 'banana')), + new Document(2, array('name' => 'apple')), + new Document(3, array('name' => 'orange')), + ); + $type->addDocuments($docs); + $index->refresh(); + + // Search documents + $queryString = new QueryString('orange'); + $query = new Query($queryString); + $resultSet = $type->search($query); + + // Check if correct document was found + $this->assertEquals(1, $resultSet->getTotalHits()); + $this->assertEquals(3, $resultSet[0]->getId()); + $data = $resultSet[0]->getData(); + $this->assertEquals('orange', $data['name']); + } + + /** + * @group functional + * @expectedException Elastica\Exception\InvalidException + * @expectedExceptionMessage is not supported in memcache transport + */ + public function testHeadRequest() + { + $client = $this->_getMemcacheClient(); + $client->request('foo', Request::HEAD); + } + + /** + * @group functional + * @expectedException Elastica\Exception\InvalidException + * @expectedExceptionMessage is not supported in memcache transport + */ + public function testInvalidRequest() + { + $client = $this->_getMemcacheClient(); + $client->request('foo', 'its_fail'); + } + + /** + * @group functional + * @expectedException Elastica\Exception\Connection\MemcacheException + * @expectedExceptionMessage is too long + */ + public function testRequestWithLongPath() + { + $client = $this->_getMemcacheClient(); + $index = $client->getIndex('memcache-test'); + $index->create(); + + $this->_waitForAllocation($index); + + $queryString = new QueryString(str_repeat('z', 300)); + $query = new Query($queryString); + $index->search($query); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/NullTransportTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/NullTransportTest.php new file mode 100644 index 00000000..cea3e3ba --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/NullTransportTest.php @@ -0,0 +1,96 @@ +<?php +namespace Elastica\Test\Transport; + +use Elastica\Connection; +use Elastica\Query; +use Elastica\Request; +use Elastica\Test\Base as BaseTest; +use Elastica\Transport\NullTransport; + +/** + * Elastica Null Transport Test. + * + * @author James Boehmer <james.boehmer@jamesboehmer.com> + */ +class NullTransportTest extends BaseTest +{ + /** + * @group functional + */ + public function testEmptyResult() + { + // Creates a client with any destination, and verify it returns a response object when executed + $client = $this->_getClient(); + $connection = new Connection(array('transport' => 'NullTransport')); + $client->setConnections(array($connection)); + + $index = $client->getIndex('elasticaNullTransportTest1'); + + $resultSet = $index->search(new Query()); + $this->assertNotNull($resultSet); + + $response = $resultSet->getResponse(); + $this->assertNotNull($response); + + // Validate most of the expected fields in the response data. Consumers of the response + // object have a reasonable expectation of finding "hits", "took", etc + $responseData = $response->getData(); + $this->assertContains('took', $responseData); + $this->assertEquals(0, $responseData['took']); + $this->assertContains('_shards', $responseData); + $this->assertContains('hits', $responseData); + $this->assertContains('total', $responseData['hits']); + $this->assertEquals(0, $responseData['hits']['total']); + $this->assertContains('params', $responseData); + + $took = $response->getEngineTime(); + $this->assertEquals(0, $took); + + $errorString = $response->getError(); + $this->assertEmpty($errorString); + + $shards = $response->getShardsStatistics(); + $this->assertContains('total', $shards); + $this->assertEquals(0, $shards['total']); + $this->assertContains('successful', $shards); + $this->assertEquals(0, $shards['successful']); + $this->assertContains('failed', $shards); + $this->assertEquals(0, $shards['failed']); + } + + /** + * @group functional + */ + public function testExec() + { + $request = new Request('/test'); + $params = array('name' => 'ruflin'); + $transport = new NullTransport(); + $response = $transport->exec($request, $params); + + $this->assertInstanceOf('\Elastica\Response', $response); + + $data = $response->getData(); + $this->assertEquals($params, $data['params']); + } + + /** + * @group functional + */ + public function testOldObject() + { + if (version_compare(phpversion(), 7, '>=')) { + self::markTestSkipped('These objects are not supported in PHP 7'); + } + + $request = new Request('/test'); + $params = array('name' => 'ruflin'); + $transport = new \Elastica\Transport\Null(); + $response = $transport->exec($request, $params); + + $this->assertInstanceOf('\Elastica\Response', $response); + + $data = $response->getData(); + $this->assertEquals($params, $data['params']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/ThriftTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/ThriftTest.php new file mode 100644 index 00000000..b73ef4f7 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/ThriftTest.php @@ -0,0 +1,135 @@ +<?php +namespace Elastica\Test\Transport; + +use Elastica\Connection; +use Elastica\Document; +use Elastica\Index; +use Elastica\Test\Base as BaseTest; + +class ThriftTest extends BaseTest +{ + public static function setUpBeforeClass() + { + if (!class_exists('Elasticsearch\\RestClient')) { + self::markTestSkipped('munkie/elasticsearch-thrift-php package should be installed to run thrift transport tests'); + } + } + + /** + * @group unit + */ + public function testConstruct() + { + $host = $this->_getHost(); + $port = 9500; + $client = $this->_getClient(array('host' => $host, 'port' => $port, 'transport' => 'Thrift')); + + $this->assertEquals($host, $client->getConnection()->getHost()); + $this->assertEquals($port, $client->getConnection()->getPort()); + } + + /** + * @group functional + * @dataProvider configProvider + */ + public function testSearchRequest($config) + { + $this->_checkPlugin(); + + // Creates a new index 'xodoa' and a type 'user' inside this index + $client = $this->_getClient($config); + + $index = $client->getIndex('elastica_test1'); + $index->create(array(), true); + + $type = $index->getType('user'); + + // Adds 1 document to the index + $doc1 = new Document(1, + array('username' => 'hans', 'test' => array('2', '3', '5')) + ); + $doc1->setVersion(0); + $type->addDocument($doc1); + + // Adds a list of documents with _bulk upload to the index + $docs = array(); + $docs[] = new Document(2, + array('username' => 'john', 'test' => array('1', '3', '6')) + ); + $docs[] = new Document(3, + array('username' => 'rolf', 'test' => array('2', '3', '7')) + ); + $type->addDocuments($docs); + + // Refresh index + $index->refresh(); + $resultSet = $type->search('rolf'); + + $this->assertEquals(1, $resultSet->getTotalHits()); + } + + /** + * @group unit + * @expectedException \Elastica\Exception\ConnectionException + */ + public function testInvalidHostRequest() + { + $this->_checkPlugin(); + + $client = $this->_getClient(array('host' => 'unknown', 'port' => 9555, 'transport' => 'Thrift')); + $client->getStatus(); + } + + /** + * @group functional + * @expectedException \Elastica\Exception\ResponseException + */ + public function testInvalidElasticRequest() + { + $this->_checkPlugin(); + + $connection = new Connection(); + $connection->setHost($this->_getHost()); + $connection->setPort(9500); + $connection->setTransport('Thrift'); + + $client = $this->_getClient(); + $client->addConnection($connection); + + $index = new Index($client, 'missing_index'); + $index->getStatus(); + } + + public function configProvider() + { + return array( + array( + array( + 'host' => $this->_getHost(), + 'port' => 9500, + 'transport' => 'Thrift', + ), + ), + array( + array( + 'host' => $this->_getHost(), + 'port' => 9500, + 'transport' => 'Thrift', + 'config' => array( + 'framedTransport' => false, + 'sendTimeout' => 10000, + 'recvTimeout' => 20000, + ), + ), + ), + ); + } + + protected function _checkPlugin() + { + $nodes = $this->_getClient()->getCluster()->getNodes(); + if (!$nodes[0]->getInfo()->hasPlugin('transport-thrift')) { + $this->markTestSkipped('transport-thrift plugin not installed.'); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/TransportBenchmarkTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/TransportBenchmarkTest.php new file mode 100644 index 00000000..11a16a34 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/TransportBenchmarkTest.php @@ -0,0 +1,261 @@ +<?php +namespace Elastica\Test\Transport; + +use Elastica\Document; +use Elastica\Index; +use Elastica\Query; +use Elastica\Test\Base as BaseTest; + +class TransportBenchmarkTest extends BaseTest +{ + protected $_max = 1000; + + protected $_maxData = 20; + + protected static $_results = array(); + + public static function tearDownAfterClass() + { + self::printResults(); + } + + /** + * @param array $config + * + * @return \Elastica\Type + */ + protected function getType(array $config) + { + $client = $this->_getClient($config); + $index = $client->getIndex('benchmark'.uniqid()); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + + return $index->getType('benchmark'); + } + + /** + * @dataProvider providerTransport + * @group benchmark + */ + public function testAddDocument(array $config, $transport) + { + $this->_checkThrift($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 + * @group benchmark + */ + public function testRandomRead(array $config, $transport) + { + $this->_checkThrift($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 \Elastica\Query\MatchAll()); + $query->setPostFilter(new \Elastica\Filter\Term(array('test' => $test))); + $result = $type->search($query); + $times[] = $result->getResponse()->getQueryTime(); + } + + self::logResults('random read', $transport, $times); + } + + /** + * @depends testAddDocument + * @dataProvider providerTransport + * @group benchmark + */ + 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 + * @group benchmark + */ + public function testGetMapping(array $config, $transport) + { + $client = $this->_getClient($config); + $index = $client->getIndex('benchmark'); + $index->create(array(), true); + $type = $index->getType('mappingTest'); + + // Define mapping + $mapping = new \Elastica\Type\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', \Elastica\Request::GET); + $times[] = $response->getQueryTime(); + } + self::logResults('get mapping', $transport, $times); + } + + public function providerTransport() + { + return array( + array( + array( + 'transport' => 'Http', + 'host' => $this->_getHost(), + 'port' => $this->_getPort(), + 'persistent' => false, + ), + 'Http:NotPersistent', + ), + array( + array( + 'transport' => 'Http', + 'host' => $this->_getHost(), + 'port' => $this->_getPort(), + 'persistent' => true, + ), + 'Http:Persistent', + ), + array( + array( + 'transport' => 'Thrift', + 'host' => $this->_getHost(), + '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"; + } + } + + protected function _checkThrift($transport) + { + if (strpos($transport, 'Thrift') !== false && !class_exists('Elasticsearch\\RestClient')) { + self::markTestSkipped('munkie/elasticsearch-thrift-php package should be installed to run thrift transport tests'); + } + } +} |