-
-
Save ingvar/85f0f94020875d75bf96 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Talis\SwiftForumBundle\Controller; | |
use Doctrine\ORM\EntityManager; | |
use Talis\SwiftForumBundle\Controller\BaseController; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Symfony\Component\HttpFoundation\Response; | |
class BasicController extends BaseController | |
{ | |
private $start; | |
private $pause_time; | |
/* start the timer */ | |
private function start() { | |
$this->start = $this->get_time(); | |
$this->pause_time = 0; | |
} | |
/* pause the timer */ | |
private function pause() { | |
$this->pause_time = $this->get_time(); | |
} | |
/* unpause the timer */ | |
private function unpause() { | |
$this->start += ($this->get_time() - $this->pause_time); | |
$this->pause_time = 0; | |
} | |
/* get the current timer value */ | |
private function get_current($decimals = 8) { | |
return round(($this->get_time() - $this->start),$decimals); | |
} | |
/* format the time in seconds */ | |
private function get_time() { | |
list($usec,$sec) = explode(' ', microtime()); | |
return ((float)$usec + (float)$sec); | |
} | |
/** | |
* Return the list of icons in JSON format. | |
* | |
* @Route("/icons", name="basic_icons") | |
*/ | |
public function iconsAction() | |
{ | |
/** @var EntityManager $em */ | |
$em = $this->getDoctrine()->getManager(); | |
$iterations = 500; | |
$noCaching = 0; | |
$setKey = 0; | |
$noKey = 0; | |
$testNoCaching = 1; | |
$testSetKey = 1; | |
$testNoKey = 1; | |
if($testNoCaching) | |
{ | |
$this->start(); | |
for($x = 0; $x < $iterations; $x++) { | |
$iconsNoCaching = $em->getRepository($this->getNameSpace() . ':Icons') | |
->getIconsNoCaching(); | |
} | |
$noCaching = $this->get_current(); | |
} | |
if($testNoKey) | |
{ | |
$this->start(); | |
for($x = 0; $x < $iterations; $x++) { | |
$iconsNoKey = $em->getRepository($this->getNameSpace() . ':Icons') | |
->getIconsAutoKey(); | |
} | |
$noKey = $this->get_current(); | |
} | |
if($testSetKey) | |
{ | |
$this->start(); | |
for($x = 0; $x < $iterations; $x++) { | |
if($em->getConfiguration()->getResultCacheImpl()->contains('icons')) { | |
$iconsSetKey = $this->getDoctrine()->getManager()->getConfiguration()->getResultCacheImpl()->fetch('icons'); | |
} else { | |
$iconsSetKey = $em->getRepository($this->getNameSpace() . ':Icons') | |
->getIcons(); | |
} | |
} | |
$setKey = $this->get_current(); | |
} | |
return new Response('No Caching: ' . $noCaching . '<br>Set Key: ' . $setKey . '<br>No Key: ' . $noKey); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Talis\SwiftForumBundle\Model; | |
use Doctrine\ORM\EntityRepository; | |
class IconsRepository extends EntityRepository | |
{ | |
/** | |
* Gets the list of icons | |
* | |
* @return array | |
*/ | |
public function getIcons() | |
{ | |
return $this->createQueryBuilder('i') | |
->orderBy('i.id', 'asc') | |
->getQuery() | |
->useResultCache(true, null, 'icons') | |
->getResult(); | |
} | |
/** | |
* Gets the list of icons | |
* | |
* @return array | |
*/ | |
public function getIconsNoCaching() | |
{ | |
return $this->createQueryBuilder('i') | |
->orderBy('i.id', 'asc') | |
->getQuery() | |
->useQueryCache(false) | |
->useResultCache(false) | |
->getResult(); | |
} | |
/** | |
* Gets the list of icons | |
* | |
* @return array | |
*/ | |
public function getIconsAutoKey() | |
{ | |
return $this->createQueryBuilder('i') | |
->orderBy('i.id', 'asc') | |
->getQuery() | |
->useResultCache(true) | |
->getResult(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Automatically generated key - useResultCache(true) - 1000 iterations: | |
-- Emptied all caches -- | |
Attempt 1: 23.97340798 | |
Attempt 2: 24.97743416 | |
Attempt 3: 31.65670395 | |
Attempt 4: 24.22964978 | |
Attempt 5: 25.81283689 | |
Set key - useResultCache(true, null, 'icons') - 1000 iterations: | |
-- Emptied all caches -- | |
Attempt 1: 0.71829009 | |
Attempt 2: 0.6626091 | |
Attempt 3: 0.65458798 | |
Attempt 4: 0.65469503 | |
Attempt 5: 0.66013002 | |
500 Iterations: | |
-- Emptied all caches -- | |
No Caching: 16.3678689 | |
Set Key: 0.41256189 | |
No Key: 14.29288006 | |
No Caching: 14.808321 | |
Set Key: 0.4795301 | |
No Key: 15.10666299 | |
No Caching: 15.28655291 | |
Set Key: 0.34830499 | |
No Key: 13.46338701 | |
No Caching: 13.26075292 | |
Set Key: 0.52053404 | |
No Key: 12.38359118 | |
No Caching: 15.70081902 | |
Set Key: 0.37106895 | |
No Key: 13.44588995 | |
Turning on the general MySQL log shows that everything behaves as expected: | |
No Caching = every iteration queries the database | |
Set Key & No Key = query database only the first time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment