Skip to content

Instantly share code, notes, and snippets.

@totten
Last active July 25, 2019 01:19
Benchmarking DB_common::modifyQuery

The following are different implementations of DB_common::modifyQuery().

To benchmark them, I ran each one 5 times locally on a laptop (i7-4650U, PHP 7.0.32). The results:

  • common-none.php: (9.420 + 9.370 + 9.341 + 9.549 + 9.541)/5 = 9.444
  • common-ghe.php: (10.549 + 10.161 + 10.081 + 10.448 + 10.253)/5 = 10.298
  • common-qed.php (10.043 + 9.980 + 9.988 + 9.918 + 10.018)/5 = 9.989
  • common-qes.php: (9.531 + 9.871 + 9.801 + 9.782 + 9.952)/5 = 9.787
#!/bin/bash
for FILE in common-{none,ghe,qes,qed}.php ; do
echo "== Using $FILE"
cp -f packages/DB/$FILE packages/DB/common.php
cv scr /tmp/runqueries.php
cv scr /tmp/runqueries.php
cv scr /tmp/runqueries.php
cv scr /tmp/runqueries.php
cv scr /tmp/runqueries.php
done
<?php
$start = microtime(1);
for ($i = 0; $i < 10000; $i++) {
$x = CRM_Core_DAO::executeQuery("SELECT * from civicrm_contact where display_name like '$i'")->fetchAll();
}
$end = microtime(1);
// printf("%.3f => %.3f or %.3f\n", $start, $end, $end-$start);
printf("%.3f\n", $end-$start);
<?php
function modifyQuery($query)
{
if (class_exists('Civi\Core\Container') && \Civi\Core\Container::isContainerBooted()) {
Civi\Core\Container::singleton()->get('dispatcher')->dispatch('civi.db.query',
\Civi\Core\Event\GenericHookEvent::create(array('query' => &$query))
);
}
return $query;
}
<?php
function modifyQuery($query)
{
return $query;
}
<?php
function modifyQuery($query)
{
if (class_exists('Civi\Core\Container') && \Civi\Core\Container::isContainerBooted()) {
$e = new \Civi\Core\Event\QueryEvent($query);
\Civi\Core\Container::singleton()->get('dispatcher')->dispatch('civi.db.query', $e);
return $e->query;
}
return $query;
}
<?php
function modifyQuery($query)
{
// This section of code may run hundreds or thousands of times in a given request.
// Consequently, it is micro-optimized to use single lookup in typical case.
if (!isset(Civi::$statics['db_common_dispatcher'])) {
if (class_exists('Civi\Core\Container') && \Civi\Core\Container::isContainerBooted()) {
Civi::$statics['db_common_dispatcher'] = Civi\Core\Container::singleton()->get('dispatcher');
}
else {
return $query;
}
}
$e = new \Civi\Core\Event\QueryEvent($query);
Civi::$statics['db_common_dispatcher']->dispatch('civi.db.query', $e);
return $e->query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment