This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from aheinz-sg/mongodb
Bypass Doctrine in favor of Xhgui serialization
- Loading branch information
Showing
6 changed files
with
268 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Jns\Bundle\XhprofBundle\Document; | ||
|
||
use iXHProfRuns; | ||
use Doctrine\ODM\MongoDB\DocumentManager; | ||
|
||
class XhguiRuns implements iXHProfRuns | ||
{ | ||
/** | ||
* @var DocumentManager | ||
*/ | ||
private $documentManager; | ||
|
||
public function __construct(DocumentManager $documentManager) { | ||
$this->documentManager = $documentManager; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function get_run($run_id, $type, &$run_desc) { | ||
throw new \Exception('not implemented'); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function save_run($xhprof_data, $type, $run_id = null) { | ||
if (!class_exists('\Xhgui_Profiles') || !class_exists('\Xhgui_Saver_Mongo')) { | ||
throw new \Exception('composer require perftools/xhgui dev-master'); | ||
} | ||
$data = $this->prepareForSave($xhprof_data); | ||
$dbname = $this->documentManager->getConfiguration()->getDefaultDB(); | ||
$mongo = $this->documentManager->getConnection()->getMongoClient()->selectDB($dbname); | ||
$profiles = new \Xhgui_Profiles($mongo); | ||
$saver = new \Xhgui_Saver_Mongo($profiles); | ||
try { | ||
$saver->save($data); | ||
$run_id = (string)$data['_id']; | ||
} catch (\Exception $e) { | ||
error_log($e->getMessage()); | ||
} | ||
return $run_id; | ||
} | ||
|
||
/** | ||
* @see https://github.com/perftools/xhgui/blob/ad532c42e55cf8b3413b8d7a2241eea1140b537f/external/header.php#L88 | ||
* @todo instead of this copy pasta, refactor the perftools/xhgui side of things then reuse here | ||
*/ | ||
private function prepareForSave($xhprof_data) { | ||
$data = array('profile' => $xhprof_data); | ||
$uri = array_key_exists('REQUEST_URI', $_SERVER) | ||
? $_SERVER['REQUEST_URI'] | ||
: null; | ||
if (empty($uri) && isset($_SERVER['argv'])) { | ||
$cmd = basename($_SERVER['argv'][0]); | ||
$uri = $cmd . ' ' . implode(' ', array_slice($_SERVER['argv'], 1)); | ||
} | ||
$time = array_key_exists('REQUEST_TIME', $_SERVER) | ||
? $_SERVER['REQUEST_TIME'] | ||
: time(); | ||
$requestTimeFloat = explode('.', $_SERVER['REQUEST_TIME_FLOAT']); | ||
if (!isset($requestTimeFloat[1])) { | ||
$requestTimeFloat[1] = 0; | ||
} | ||
// if (Xhgui_Config::read('save.handler') === 'file') { | ||
// $requestTs = array('sec' => $time, 'usec' => 0); | ||
// $requestTsMicro = array('sec' => $requestTimeFloat[0], 'usec' => $requestTimeFloat[1]); | ||
// } else { | ||
$requestTs = new \MongoDate($time); | ||
$requestTsMicro = new \MongoDate($requestTimeFloat[0], $requestTimeFloat[1]); | ||
// } | ||
$data['meta'] = array( | ||
'url' => $uri, | ||
'SERVER' => $_SERVER, | ||
'get' => $_GET, | ||
'env' => $_ENV, | ||
'simple_url' => \Xhgui_Util::simpleUrl($uri), | ||
'request_ts' => $requestTs, | ||
'request_ts_micro' => $requestTsMicro, | ||
'request_date' => date('Y-m-d', $time), | ||
); | ||
return $data; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Jns\Bundle\XhprofBundle\Entity; | ||
|
||
use iXHProfRuns; | ||
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
class XhguiRuns implements iXHProfRuns, ContainerAwareInterface | ||
{ | ||
/** | ||
* @var ContainerInterface | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* Sets the Container associated with this Controller. | ||
* | ||
* @param ContainerInterface $container A ContainerInterface instance | ||
*/ | ||
public function setContainer(ContainerInterface $container = null) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
public function __construct($serverName, $uri) { | ||
$this->serverName = $serverName; | ||
$this->uri = $uri; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function get_run($run_id, $type, &$run_desc) { | ||
throw new \Exception('not implemented'); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function save_run($xhprof_data, $type, $run_id = null) { | ||
$pmu = isset($xhprof_data['main()']['pmu']) ? $xhprof_data['main()']['pmu'] : ''; | ||
$wt = isset($xhprof_data['main()']['wt']) ? $xhprof_data['main()']['wt'] : ''; | ||
$cpu = isset($xhprof_data['main()']['cpu']) ? $xhprof_data['main()']['cpu'] : ''; | ||
|
||
$doctrine = $this->container->get('doctrine'); | ||
if (empty($doctrine)) { | ||
throw new \Exception("Trying to save to database, but Doctrine was not set correctly"); | ||
} | ||
|
||
$runId = uniqid(); | ||
|
||
$em = $doctrine->getManager($this->container->getParameter('jns_xhprof.entity_manager')); | ||
$entityClass = $this->container->getParameter('jns_xhprof.entity_class'); | ||
|
||
$xhprofDetail = new $entityClass(); | ||
$xhprofDetail | ||
->setId($runId) | ||
->setUrl($this->uri) | ||
->setCanonicalUrl($this->getCanonicalUrl($uri)) | ||
->setServerName($this->serverName) | ||
->setPerfData(gzcompress(serialize(($xhprof_data)))) | ||
->setCookie(serialize($_COOKIE)) | ||
->setPost(serialize($_POST)) | ||
->setGet(serialize($_GET)) | ||
->setPmu($pmu) | ||
->setWt($wt) | ||
->setCpu($cpu) | ||
->setServerId(getenv('SERVER_NAME')) | ||
->setAggregateCallsInclude('') | ||
->setTimestamp(new \DateTime()) | ||
; | ||
|
||
$em->persist($xhprofDetail); | ||
$em->flush(); | ||
|
||
return $runId; | ||
} | ||
} | ||
?> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Jns\Bundle\XhprofBundle\Tests\DataCollector; | ||
|
||
use Jns\Bundle\XhprofBundle\DataCollector\XhprofCollector; | ||
|
||
class XhprofCollectorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var XhprofCollector | ||
*/ | ||
private $collector; | ||
|
||
/** | ||
* @var \Symfony\Component\DependencyInjection\ContainerInterface | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @var \Doctrine\Common\Persistence\ManagerRegistry | ||
*/ | ||
private $doctrine; | ||
|
||
public function setUp() { | ||
parent::setUp(); | ||
$this->container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->disableOriginalConstructor()->getMock(); | ||
$this->doctrine = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry'); | ||
$this->collector = new XhprofCollectorTestable($this->container, null, $this->doctrine); | ||
} | ||
|
||
/** | ||
* @dataProvider data_createRun | ||
*/ | ||
public function test_createRun($enable_xhgui, $manager_class, $runs_class) { | ||
$this->container->method('get')->with('manager_registry')->will($this->returnValue($this->doctrine)); | ||
$this->container->method('getParameter')->will($this->returnValueMap(array( | ||
array('jns_xhprof.enable_xhgui', $enable_xhgui), | ||
array('jns_xhprof.entity_manager', 'entity_manager'), | ||
array('jns_xhprof.manager_registry', 'manager_registry'), | ||
))); | ||
$this->doctrine->method('getManager')->with('entity_manager')->will($this->returnValue(!$manager_class ?: $this->getMock($manager_class))); | ||
$xhprof_runs = $this->collector->createRun('serverName', 'uri'); | ||
$this->assertInstanceOf($runs_class, $xhprof_runs); | ||
} | ||
|
||
public function data_createRun() { | ||
return array( | ||
array(false, null, '\XHProfRuns_Default'), | ||
array(true, '\Doctrine\ODM\MongoDB\DocumentManager', '\Jns\Bundle\XhprofBundle\Document\XhguiRuns'), | ||
array(true, '\Doctrine\ORM\EntityManager', '\Jns\Bundle\XhprofBundle\Entity\XhguiRuns'), | ||
); | ||
} | ||
} | ||
|
||
class XhprofCollectorTestable extends XhprofCollector | ||
{ | ||
public function createRun($serverName, $uri) { | ||
return parent::createRun($serverName, $uri); | ||
} | ||
} |
Oops, something went wrong.