Skip to content

Commit 425aa3d

Browse files
committed
Merge pull request #645 from merk/stopwatch
Add stopwatch support for Elastica\Client
2 parents 21e5d90 + 815b836 commit 425aa3d

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Elastica/Client.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Elastica\Client as BaseClient;
66
use Elastica\Request;
77
use FOS\ElasticaBundle\Logger\ElasticaLogger;
8+
use Symfony\Component\Stopwatch\Stopwatch;
89

910
/**
1011
* Extends the default Elastica client to provide logging for errors that occur
@@ -21,11 +22,22 @@ class Client extends BaseClient
2122
*/
2223
private $indexCache = array();
2324

25+
/**
26+
* Symfony's debugging Stopwatch
27+
*
28+
* @var Stopwatch|null
29+
*/
30+
private $stopwatch;
31+
2432
/**
2533
* {@inheritdoc}
2634
*/
2735
public function request($path, $method = Request::GET, $data = array(), array $query = array())
2836
{
37+
if ($this->stopwatch) {
38+
$this->stopwatch->start('es_request', 'fos_elastica');
39+
}
40+
2941
$start = microtime(true);
3042
$response = parent::request($path, $method, $data, $query);
3143

@@ -44,6 +56,10 @@ public function request($path, $method = Request::GET, $data = array(), array $q
4456
$this->_logger->logQuery($path, $method, $data, $time, $connection_array, $query);
4557
}
4658

59+
if ($this->stopwatch) {
60+
$this->stopwatch->stop('es_request');
61+
}
62+
4763
return $response;
4864
}
4965

@@ -55,4 +71,14 @@ public function getIndex($name)
5571

5672
return $this->indexCache[$name] = new Index($this, $name);
5773
}
74+
75+
/**
76+
* Sets a stopwatch instance for debugging purposes.
77+
*
78+
* @param Stopwatch $stopwatch
79+
*/
80+
public function setStopwatch(Stopwatch $stopwatch = null)
81+
{
82+
$this->stopwatch = $stopwatch;
83+
}
5884
}

Resources/config/config.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<service id="fos_elastica.client_prototype" class="%fos_elastica.client.class%" abstract="true">
1717
<argument type="collection" /> <!-- configuration -->
1818
<argument /> <!-- callback -->
19+
20+
<call method="setStopwatch">
21+
<argument type="service" id="debug.stopwatch" />
22+
</call>
1923
</service>
2024

2125
<service id="fos_elastica.config_manager" class="FOS\ElasticaBundle\Configuration\ConfigManager">

0 commit comments

Comments
 (0)