|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ONGR\ElasticsearchBundle\Tests\Functional\Profiler; |
| 4 | + |
| 5 | +use Sineflow\ElasticsearchBundle\Profiler\ElasticsearchProfiler; |
| 6 | +use Sineflow\ElasticsearchBundle\Tests\AbstractElasticsearchTestCase; |
| 7 | +use Sineflow\ElasticsearchBundle\Tests\app\fixture\Acme\BarBundle\Document\Product; |
| 8 | +use Symfony\Component\HttpFoundation\Request; |
| 9 | +use Symfony\Component\HttpFoundation\Response; |
| 10 | + |
| 11 | +class ElasticsearchProfilerTest extends AbstractElasticsearchTestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * {@inheritdoc} |
| 15 | + */ |
| 16 | + protected function getDataArray() |
| 17 | + { |
| 18 | + return [ |
| 19 | + 'default' => [ |
| 20 | + 'product' => [ |
| 21 | + [ |
| 22 | + '_id' => 1, |
| 23 | + 'title' => 'foo', |
| 24 | + ], |
| 25 | + [ |
| 26 | + '_id' => 2, |
| 27 | + 'title' => 'bar', |
| 28 | + ], |
| 29 | + [ |
| 30 | + '_id' => 3, |
| 31 | + 'title' => 'pizza', |
| 32 | + ], |
| 33 | + ], |
| 34 | + ], |
| 35 | + ]; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Tests if right amount of queries are caught |
| 40 | + */ |
| 41 | + public function testGetQueryCount() |
| 42 | + { |
| 43 | + $imWithoutAliases = $this->getIndexManager('bar'); |
| 44 | + $imWithoutAliases->getConnection()->setAutocommit(false); |
| 45 | + |
| 46 | + // Setting up the test takes 3 queries: |
| 47 | + // 1. DELETE query to remove existing index, |
| 48 | + // 2. Internal call to GET /_aliases to check if index/alias already exists |
| 49 | + // 3. PUT request to create index |
| 50 | + $this->assertEquals(3, $this->getCollector()->getQueryCount()); |
| 51 | + |
| 52 | + $product = new Product(); |
| 53 | + $product->title = 'tuna'; |
| 54 | + $imWithoutAliases->persist($product); |
| 55 | + $imWithoutAliases->getConnection()->commit(); |
| 56 | + |
| 57 | + // It takes 3 more queries to insert the new document: |
| 58 | + // 1. GET /sineflow-esb-test-bar/_alias to check if more than one index should be written to |
| 59 | + // 2. POST bulk request to enter the data |
| 60 | + // 3. GET /_refresh, because of the $forceRefresh param of ->commit() |
| 61 | + $this->assertEquals(6, $this->getCollector()->getQueryCount()); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Tests if correct time is being returned. |
| 66 | + */ |
| 67 | + public function testGetTime() |
| 68 | + { |
| 69 | + $imWithoutAliases = $this->getIndexManager('bar'); |
| 70 | + $imWithoutAliases->getConnection()->setAutocommit(true); |
| 71 | + $imWithoutAliases->getRepository('AcmeBarBundle:Product')->getById(3); |
| 72 | + |
| 73 | + $this->assertGreaterThan(0.0, $this->getCollector()->getTime(), 'Time should be greater than 0ms'); |
| 74 | + $this->assertLessThan(1000.0, $this->getCollector()->getTime(), 'Time should be less than 1s'); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Tests if logged data seems correct |
| 79 | + */ |
| 80 | + public function testCorrectDataLogged() |
| 81 | + { |
| 82 | + $imWithoutAliases = $this->getIndexManager('bar'); |
| 83 | + $imWithoutAliases->getConnection()->setAutocommit(true); |
| 84 | + $imWithoutAliases->getRepository('AcmeBarBundle:Product')->getById(3); |
| 85 | + |
| 86 | + |
| 87 | + $queries = $this->getCollector()->getQueries(); |
| 88 | + |
| 89 | + $lastQuery = end($queries[ElasticsearchProfiler::UNDEFINED_ROUTE]); |
| 90 | + $this->checkQueryParameters($lastQuery); |
| 91 | + |
| 92 | + $esHostAndPort = explode(':', $imWithoutAliases->getConnection()->getConnectionSettings()['hosts'][0]); |
| 93 | + |
| 94 | + $this->assertArraySubset( |
| 95 | + [ |
| 96 | + "curlRequest" => "curl -XGET 'http://".implode(':', $esHostAndPort)."/sineflow-esb-test-bar/product/3?pretty=true'", |
| 97 | + "senseRequest" => "GET /sineflow-esb-test-bar/product/3", |
| 98 | + "backtrace" => null, |
| 99 | + "scheme" => "http", |
| 100 | + "host" => $esHostAndPort[0], |
| 101 | + "port" => (int) $esHostAndPort[1], |
| 102 | + "path" => "/sineflow-esb-test-bar/product/3", |
| 103 | + ], |
| 104 | + $lastQuery, |
| 105 | + 'Logged data did not match expected data.' |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Checks query parameters that are not static. |
| 111 | + * |
| 112 | + * @param array $query |
| 113 | + */ |
| 114 | + public function checkQueryParameters($query) |
| 115 | + { |
| 116 | + $this->assertArrayHasKey('time', $query, 'Query should have time set.'); |
| 117 | + $this->assertGreaterThan(0.0, $query['time'], 'Time should be greater than 0'); |
| 118 | + |
| 119 | + $this->assertArrayHasKey('curlRequest', $query, 'Query should have curlRequest set.'); |
| 120 | + $this->assertNotEmpty($query['curlRequest'], 'curlRequest should not be empty'); |
| 121 | + |
| 122 | + $this->assertArrayHasKey('senseRequest', $query, 'Query should have senseRequest set.'); |
| 123 | + $this->assertNotEmpty($query['senseRequest'], 'senseRequest should not be empty.'); |
| 124 | + |
| 125 | + $this->assertArrayHasKey('backtrace', $query, 'Query should have backtrace set.'); |
| 126 | + |
| 127 | + $this->assertArrayHasKey('scheme', $query, 'Query should have scheme set.'); |
| 128 | + $this->assertNotEmpty($query['scheme'], 'scheme should not be empty.'); |
| 129 | + |
| 130 | + $this->assertArrayHasKey('host', $query, 'Query should have host set.'); |
| 131 | + $this->assertNotEmpty($query['host'], 'Host should not be empty'); |
| 132 | + |
| 133 | + $this->assertArrayHasKey('port', $query, 'Query should have port set.'); |
| 134 | + $this->assertNotEmpty($query['port'], 'port should not be empty.'); |
| 135 | + |
| 136 | + $this->assertArrayHasKey('path', $query, 'Query should have host path set.'); |
| 137 | + $this->assertNotEmpty($query['path'], 'Path should not be empty.'); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * @return ElasticsearchProfiler |
| 142 | + */ |
| 143 | + private function getCollector() |
| 144 | + { |
| 145 | + $collector = $this->getContainer()->get('sfes.profiler'); |
| 146 | + $collector->collect(new Request(), new Response()); |
| 147 | + |
| 148 | + return $collector; |
| 149 | + } |
| 150 | +} |
0 commit comments