Skip to content
This repository has been archived by the owner on Jan 8, 2021. It is now read-only.

It doesn't count queries. #33

Closed
joshribakoff opened this issue Jul 11, 2013 · 1 comment
Closed

It doesn't count queries. #33

joshribakoff opened this issue Jul 11, 2013 · 1 comment

Comments

@joshribakoff
Copy link

I can confirm that it does profile PDO prepared statements. It does not however profile raw SQL, which is a problem because I need to write a performance test that asserts my code does some operation in only 1 query.

Here's a test case that exposes the flaw in your code.

function testBjy()
{
    // insert some data & select it back, so we can see queries actually ran. THis is 2 queries
    $this->db->query("INSERT INTO `product` (sku) value ('test')", \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
    $result = $this->db->query("SELECT * FROM `product`", \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
    print_r($result->toArray());

    // running 2 more queries!
    $this->db->query("SELECT count(*) FROM `product`", \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
    $this->db->query("SELECT * FROM `product`", \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);

    // lets ask it how many queries its seen, should be 4 :)
    $profiler = $this->db->getProfiler();
    $queryProfiles = $profiler->getQueryProfiles();
    $afterCount = count($queryProfiles);

    // it says it has seen no queries :/
    var_dump($afterCount); // 0

    // some sanity checking.
    var_dump(get_class($profiler));
    var_dump(get_class($this->db));
}
$ phpunit --filter=testBjy
.Array
(
    [0] => Array
        (
            [id] => 135
            [sku] => test
        )
)

# here its saying theres 0 queries
int(0)  

# this proves its indeed your code running.
string(32) "BjyProfiler\Db\Profiler\Profiler" 
string(39) "BjyProfiler\Db\Adapter\ProfilingAdapter"

I have the following version of your code loaded: 1ca3161

Here's my profiler object, the bitwise filter is set to all types clearly:

class BjyProfiler\Db\Profiler\Profiler#396 (3) {
  protected $profiles =>
  array(0) {
  }
  protected $enabled =>
  bool(true)
  protected $filterTypes =>
  int(127)
}

I threw a debug statement into Query::start() -

echo 'got here';exit;

It does not hit that line when running my above test.

@joshribakoff
Copy link
Author

These seems to patch the flaw. In Db/Adapter/ProfilingAdapter.php add this:

public function query($sql, $parametersOrQueryMode = self::QUERY_MODE_PREPARE)
{
    $this->getProfiler()->startQuery($sql);
    $return = parent::query($sql, $parametersOrQueryMode);
    $this->getProfiler()->endQuery();
    return $return;
}

You'll probably want to do some more testing before you add that, but it seems to count raw queries now, and its not double counting any of my other prepared queries that Zend's table gateway generates.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant