Skip to content

Commit b13b4a6

Browse files
authored
Merge pull request #656 from einorler/issue616
implemented the display of ongr_es performance in Symfony profiler
2 parents 2fb6391 + 51897c0 commit b13b4a6

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

DependencyInjection/ONGRElasticsearchExtension.php

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Config\FileLocator;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\ContainerInterface;
1617
use Symfony\Component\DependencyInjection\Loader;
1718
use Symfony\Component\DependencyInjection\Definition;
1819
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
@@ -49,6 +50,12 @@ public function load(array $configs, ContainerBuilder $container)
4950
]
5051
);
5152
$definition->addMethodCall('setEventDispatcher', [new Reference('event_dispatcher')]);
53+
$definition->addMethodCall(
54+
'setStopwatch',
55+
[
56+
new Reference('debug.stopwatch', ContainerInterface::NULL_ON_INVALID_REFERENCE)
57+
]
58+
);
5259
$container->setDefinition('es.manager_factory', $definition);
5360
}
5461
}

ONGRElasticsearchBundle.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace ONGR\ElasticsearchBundle;
1313

14+
use ONGR\ElasticsearchBundle\DependencyInjection\Compiler\ManagerFactoryPass;
1415
use ONGR\ElasticsearchBundle\DependencyInjection\Compiler\MappingPass;
1516
use Symfony\Component\DependencyInjection\ContainerBuilder;
1617
use Symfony\Component\HttpKernel\Bundle\Bundle;

Service/Manager.php

+40-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use ONGR\ElasticsearchBundle\Result\Result;
2626
use ONGR\ElasticsearchDSL\Search;
2727
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
28+
use Symfony\Component\Stopwatch\Stopwatch;
2829

2930
/**
3031
* Manager class.
@@ -103,6 +104,11 @@ class Manager
103104
*/
104105
private $eventDispatcher;
105106

107+
/**
108+
* @var Stopwatch
109+
*/
110+
private $stopwatch;
111+
106112
/**
107113
* @param string $name Manager name
108114
* @param array $config Manager configuration
@@ -161,6 +167,14 @@ public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
161167
$this->eventDispatcher = $eventDispatcher;
162168
}
163169

170+
/**
171+
* @param Stopwatch $stopwatch
172+
*/
173+
public function setStopwatch(Stopwatch $stopwatch)
174+
{
175+
$this->stopwatch = $stopwatch;
176+
}
177+
164178
/**
165179
* Returns repository by document class.
166180
*
@@ -270,7 +284,11 @@ public function search(array $types, array $query, array $queryStringParams = []
270284
$params = array_merge($queryStringParams, $params);
271285
}
272286

273-
return $this->client->search($params);
287+
$this->stopwatch('start', 'search');
288+
$result = $this->client->search($params);
289+
$this->stopwatch('stop', 'search');
290+
291+
return $result;
274292
}
275293

276294
/**
@@ -347,10 +365,15 @@ public function commit(array $params = [])
347365
new CommitEvent($this->getCommitMode(), $bulkQueries)
348366
);
349367

368+
$this->stopwatch('start', 'bulk');
350369
$bulkResponse = $this->client->bulk($bulkQueries);
370+
$this->stopwatch('stop', 'bulk');
371+
351372
$this->bulkQueries = [];
352373
$this->bulkCount = 0;
353374

375+
$this->stopwatch('start', 'refresh');
376+
354377
switch ($this->getCommitMode()) {
355378
case 'flush':
356379
$this->flush($params);
@@ -365,6 +388,8 @@ public function commit(array $params = [])
365388
new CommitEvent($this->getCommitMode(), $bulkResponse)
366389
);
367390

391+
$this->stopwatch('stop', 'refresh');
392+
368393
return $bulkResponse;
369394
}
370395

@@ -424,6 +449,7 @@ public function bulk($operation, $type, array $query)
424449
$this->bulkCount++;
425450

426451
$response = null;
452+
427453
if ($this->bulkCommitSize === $this->bulkCount) {
428454
$response = $this->commit();
429455
}
@@ -703,4 +729,17 @@ private function resolveTypeName($className)
703729

704730
return $className;
705731
}
732+
733+
/**
734+
* Starts and stops an event in the stopwatch
735+
*
736+
* @param string $action only 'start' and 'stop'
737+
* @param string $name name of the event
738+
*/
739+
private function stopwatch($action, $name)
740+
{
741+
if (isset($this->stopwatch)) {
742+
$this->stopwatch->$action('ongr_es: '.$name, 'ongr_es');
743+
}
744+
}
706745
}

Service/ManagerFactory.php

+18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ONGR\ElasticsearchBundle\Result\Converter;
1717
use Psr\Log\LoggerInterface;
1818
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19+
use Symfony\Component\Stopwatch\Stopwatch;
1920

2021
/**
2122
* Elasticsearch Manager factory class.
@@ -47,6 +48,11 @@ class ManagerFactory
4748
*/
4849
private $eventDispatcher;
4950

51+
/**
52+
* @var Stopwatch
53+
*/
54+
private $stopwatch;
55+
5056
/**
5157
* @param MetadataCollector $metadataCollector Metadata collector service.
5258
* @param Converter $converter Converter service to transform arrays to objects and visa versa.
@@ -69,6 +75,14 @@ public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
6975
$this->eventDispatcher = $eventDispatcher;
7076
}
7177

78+
/**
79+
* @param Stopwatch $stopwatch
80+
*/
81+
public function setStopwatch(Stopwatch $stopwatch)
82+
{
83+
$this->stopwatch = $stopwatch;
84+
}
85+
7286
/**
7387
* Factory function to create a manager instance.
7488
*
@@ -125,6 +139,10 @@ public function createManager($managerName, $connection, $analysis, $managerConf
125139
$this->converter
126140
);
127141

142+
if (isset($this->stopwatch)) {
143+
$manager->setStopwatch($this->stopwatch);
144+
}
145+
128146
$manager->setCommitMode($managerConfig['commit_mode']);
129147
$manager->setEventDispatcher($this->eventDispatcher);
130148
$manager->setBulkCommitSize($managerConfig['bulk_size']);

0 commit comments

Comments
 (0)