Skip to content

Commit 6785b80

Browse files
committed
Merge pull request #360 from Baachi/stopwatch
Add TimedGeocoder
2 parents 8cb6b10 + d0e17e6 commit 6785b80

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
"igorw/get-in": "~1.0"
1818
},
1919
"require-dev": {
20-
"geoip2/geoip2": "~0.6"
20+
"geoip2/geoip2": "~0.6",
21+
"symfony/stopwatch": "~2.5"
2122
},
2223
"suggest": {
2324
"ext-geoip": "Enabling the geoip extension allows you to use the MaxMindProvider.",
2425
"geoip/geoip": "If you are going to use the MaxMindBinaryProvider (conflict with geoip extension).",
25-
"geoip2/geoip2": "If you are going to use the GeoIP2DatabaseProvider."
26+
"geoip2/geoip2": "If you are going to use the GeoIP2DatabaseProvider.",
27+
"symfony/stopwatch": "If you want to use the TimedGeocoder"
2628
},
2729
"autoload": {
2830
"psr-0": { "Geocoder": "src/" }

src/Geocoder/TimedGeocoder.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder;
12+
13+
use Symfony\Component\Stopwatch\Stopwatch;
14+
15+
/**
16+
* This Geocoder allows you to profile your API/Database calls.
17+
*
18+
* @author Markus Bachmann <[email protected]>
19+
*/
20+
class TimedGeocoder implements Geocoder
21+
{
22+
private $delegate;
23+
24+
private $stopwatch;
25+
26+
public function __construct(Geocoder $delegate, Stopwatch $stopwatch)
27+
{
28+
$this->delegate = $delegate;
29+
$this->stopwatch = $stopwatch;
30+
}
31+
32+
public function geocode($value)
33+
{
34+
$this->stopwatch->start('geocode', 'geocoder');
35+
$result = $this->delegate->geocode($value);
36+
$this->stopwatch->stop('geocode');
37+
38+
return $result;
39+
}
40+
41+
public function reverse($latitude, $longitude)
42+
{
43+
$this->stopwatch->start('reverse', 'geocoder');
44+
$result = $this->delegate->reverse($latitude, $longitude);
45+
$this->stopwatch->stop('reverse');
46+
47+
return $result;
48+
}
49+
50+
public function __call($method, $args)
51+
{
52+
return call_user_func_array([$this->delegate, $method], $args);
53+
}
54+
}

tests/Geocoder/TestTimedGeocoder.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Geocoder\Tests;
4+
5+
use Geocoder\TimedGeocoder;
6+
use Symfony\Component\Stopwatch\Stopwatch;
7+
8+
class TimedGeocoderTest extends TestCase
9+
{
10+
protected function setUp()
11+
{
12+
$this->stopwatch = new Stopwatch();
13+
$this->delegate = $this->getMock('Geocoder\Geocoder');
14+
$this->geocoder = new TimedGeocoder($this->delegate, $this->stopwatch);
15+
}
16+
17+
public function testGeocode()
18+
{
19+
$this->delegate->expects($this->once())
20+
->method('geocode')
21+
->with($this->equalTo('foo'))
22+
->will($this->returnValue([]));
23+
24+
$this->geocoder->geocode('foo');
25+
26+
$this->assertCount(1, $this->stopwatch->getSectionEvents('__root__'));
27+
}
28+
29+
public function testReverse()
30+
{
31+
$this->delegate->expects($this->once())
32+
->method('reverse')
33+
->with($this->equalTo(0, 0))
34+
->will($this->returnValue([]));
35+
36+
$this->geocoder->reverse(0, 0);
37+
38+
$this->assertCount(1, $this->stopwatch->getSectionEvents('__root__'));
39+
}
40+
}

0 commit comments

Comments
 (0)