I created a custom helper class module which I’m calling thusly:
$bench = Mage::helper('benchmark');
// ... do stuff ...
$bench->addBenchmark();
// ... more stuff ...
$bench->logResults();
This outputs the results of time benchmarks in an array.
The class is really simple, it basically just adds the current microtime() to an array then spits that array out when logResults() is called.
The problem is, I have a loop where I’m calling the code above, but instead of logging a “fresh” set of benchmark figures each loop, it’s compounding itself and adding to the existing benchmarks array!
I want it to create a new set of benchmarks each time Mage::helper('benchmark'); is called.
Any help is appreciated.
With a helper that is not possible, unless you manually flush the content of the array upon calling the
addBenchmarkmethod.The problem lies in the following. Within
Mage.phpyour requested helper is stored in the registry. Upon next call the exact same helper object will be retrieved from the registry, instead of a new one. It only creates an instance the first time the helper is called.You could opt for a model, since
getModel()returns a new instance each time the factory method is called. Though in my opinion that is a case of bad programming and also hurts you on performance.I would opt for flushing the latest data upon making a new call to
addBenchmark. If you want to store all data in the helper for later processing perhaps always return the latest key in the array? Though, thats a matter of choice and flavor, so I will leave that part up to you.Hopefully this helps you a bit!