Skip to content

Commit

Permalink
merged branch pulzarraider/memcache_profiler_update (PR #4150)
Browse files Browse the repository at this point in the history
Commits
-------

1f6c8d5 [HttpKernel] Added mock objects for Memcache(d) and Redis
e17217b [HttpKernel] Remove destructive flush() from memcache(d) storage profilers

Discussion
----------

[HttpKernel] Memcache and Redis profiler storage update

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -

Changes of this PR:

- change ```purge()``` method of memcache(d) profiler storage to delete only required items and be less destructive,
- mock objects for Redis and Memcache(d) storages were added to make unit tests independent from memcache(d)/redis extensions and memcache(d)/redis servers running on localhost.
  • Loading branch information
fabpot committed Apr 30, 2012
2 parents 33c7743 + 1f6c8d5 commit dbd9568
Show file tree
Hide file tree
Showing 11 changed files with 767 additions and 79 deletions.
1 change: 0 additions & 1 deletion phpunit.xml.dist
Expand Up @@ -22,7 +22,6 @@
<groups>
<exclude>
<group>benchmark</group>
<group>memcached</group>
</exclude>
</groups>

Expand Down
Expand Up @@ -95,7 +95,28 @@ public function find($ip, $url, $limit, $method)
*/
public function purge()
{
$this->flush();
// delete only items from index
$indexName = $this->getIndexName();

$indexContent = $this->getValue($indexName);

if (!$indexContent) {
return false;
}

$profileList = explode("\n", $indexContent);

foreach ($profileList as $item) {
if ($item == '') {
continue;
}

if (false !== $pos = strpos($item, "\t")) {
$this->delete($this->getItemName(substr($item, 0, $pos)));
}
}

return $this->delete($indexName);
}

/**
Expand Down Expand Up @@ -172,11 +193,13 @@ abstract protected function getValue($key);
abstract protected function setValue($key, $value, $expiration = 0);

/**
* Flush all existing items at the memcache server
* Delete item from the memcache server
*
* @param string $key
*
* @return boolean
*/
abstract protected function flush();
abstract protected function delete($key);

/**
* Append data to an existing item on the memcache server
Expand Down
Expand Up @@ -50,6 +50,16 @@ protected function getMemcache()
return $this->memcache;
}

/**
* Set instance of the Memcache
*
* @param Memcache $memcache
*/
public function setMemcache($memcache)
{
$this->memcache = $memcache;
}

/**
* {@inheritdoc}
*/
Expand All @@ -69,9 +79,9 @@ protected function setValue($key, $value, $expiration = 0)
/**
* {@inheritdoc}
*/
protected function flush()
protected function delete($key)
{
return $this->getMemcache()->flush();
return $this->getMemcache()->delete($key);
}

/**
Expand Down
Expand Up @@ -54,6 +54,16 @@ protected function getMemcached()
return $this->memcached;
}

/**
* Set instance of the Memcached
*
* @param Memcached $memcached
*/
public function setMemcached($memcached)
{
$this->memcached = $memcached;
}

/**
* {@inheritdoc}
*/
Expand All @@ -73,9 +83,9 @@ protected function setValue($key, $value, $expiration = 0)
/**
* {@inheritdoc}
*/
protected function flush()
protected function delete($key)
{
return $this->getMemcached()->flush();
return $this->getMemcached()->delete($key);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpKernel/Profiler/RedisProfilerStorage.php
Expand Up @@ -211,6 +211,16 @@ protected function getRedis()
return $this->redis;
}

/**
* Set instance of the Redis
*
* @param Redis $redis
*/
public function setRedis($redis)
{
$this->redis = $redis;
}

private function createProfileFromData($token, $data, $parent = null)
{
$profile = new Profile($token);
Expand Down
Expand Up @@ -12,48 +12,30 @@
namespace Symfony\Component\HttpKernel\Tests\Profiler;

use Symfony\Component\HttpKernel\Profiler\MemcacheProfilerStorage;
use Symfony\Component\HttpKernel\Tests\Profiler\Mock\MemcacheMock;

class DummyMemcacheProfilerStorage extends MemcacheProfilerStorage
{
public function getMemcache()
{
return parent::getMemcache();
}
}

/**
* @group memcached
*/
class MemcacheProfilerStorageTest extends AbstractProfilerStorageTest
{
protected static $storage;

public static function tearDownAfterClass()
protected function setUp()
{
$memcacheMock = new MemcacheMock();
$memcacheMock->addServer('127.0.0.1', 11211);

self::$storage = new MemcacheProfilerStorage('memcache://127.0.0.1:11211', '', '', 86400);
self::$storage->setMemcache($memcacheMock);

if (self::$storage) {
self::$storage->purge();
}
}

protected function setUp()
protected function tearDown()
{
if (!extension_loaded('memcache')) {
$this->markTestSkipped('MemcacheProfilerStorageTest requires that the extension memcache is loaded');
}

self::$storage = new DummyMemcacheProfilerStorage('memcache://127.0.0.1:11211', '', '', 86400);
try {
self::$storage->getMemcache();
$stats = self::$storage->getMemcache()->getExtendedStats();
if (!isset($stats['127.0.0.1:11211']) || $stats['127.0.0.1:11211'] === false) {
throw new \Exception();
}
} catch (\Exception $e) {
$this->markTestSkipped('MemcacheProfilerStorageTest requires that there is a Memcache server present on localhost');
}

if (self::$storage) {
self::$storage->purge();
self::$storage = false;
}
}

Expand Down
Expand Up @@ -12,44 +12,30 @@
namespace Symfony\Component\HttpKernel\Tests\Profiler;

use Symfony\Component\HttpKernel\Profiler\MemcachedProfilerStorage;
use Symfony\Component\HttpKernel\Tests\Profiler\Mock\MemcachedMock;

class DummyMemcachedProfilerStorage extends MemcachedProfilerStorage
{
public function getMemcached()
{
return parent::getMemcached();
}
}

/**
* @group memcached
*/
class MemcachedProfilerStorageTest extends AbstractProfilerStorageTest
{
protected static $storage;

public static function tearDownAfterClass()
protected function setUp()
{
$memcachedMock = new MemcachedMock();
$memcachedMock->addServer('127.0.0.1', 11211);

self::$storage = new MemcachedProfilerStorage('memcached://127.0.0.1:11211', '', '', 86400);
self::$storage->setMemcached($memcachedMock);

if (self::$storage) {
self::$storage->purge();
}
}

protected function setUp()
protected function tearDown()
{
if (!extension_loaded('memcached')) {
$this->markTestSkipped('MemcachedProfilerStorageTest requires that the extension memcached is loaded');
}

self::$storage = new DummyMemcachedProfilerStorage('memcached://127.0.0.1:11211', '', '', 86400);
try {
self::$storage->getMemcached();
} catch (\Exception $e) {
$this->markTestSkipped('MemcachedProfilerStorageTest requires that there is a Memcache server present on localhost');
}

if (self::$storage) {
self::$storage->purge();
self::$storage = false;
}
}

Expand Down

0 comments on commit dbd9568

Please sign in to comment.