Advice for a Hack?
Moderator: General Moderators
Advice for a Hack?
Code removed 
Last edited by robindean on Fri Aug 20, 2010 3:07 pm, edited 1 time in total.
-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: Advice for a Hack?
I'm not reading that entire thing, maybe someone else will. Here's what you can do to try and find the bottleneck though:
before a section of code you want to test put
after the section put
You can put these 2 lines around different sections of code and the number of milliseconds that the section takes to execute will be printed to the screen. You can move them around to see how long different sections take to find the bottlneck(s).
Shawn
before a section of code you want to test put
Code: Select all
$start = microtime(true) * 1000;
Code: Select all
echo $start - microtime(true) * 1000;
Shawn
Re: Advice for a Hack?
Thank you very much for this. I'll likely use this timer extensively in the future! Good thinking!
-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: Advice for a Hack?
No problem. If you want you can try out this class I made:
It's pretty quick and dirty. basically you can do this with it though:
It will print a table showing you how long each section of code took to run. The string inside of save is optional, it's just a comment to make it clear to you what the time belongs to.
Shawn
Code: Select all
<?php
class Profiler
{
private $start;
private $log;
public function __construct()
{
$this->start = $this->get_time(false);
$this->log = array();
}
public function save($comment = "")
{
$backtrace = debug_backtrace();
$this->log[] = array
(
"file" => $backtrace[0]["file"],
"line" => $backtrace[0]["line"],
"comment" => $comment,
"time" => $this->get_time()
);
}
public function display()
{
if (count($this->log))
{
echo "<table id=\"profiler\">";
echo "<tr id=\"profiler_head\">";
foreach ($this->log[0] as $key => $value)
{
echo "<td>";
echo $key;
echo "</td>";
}
echo "</tr>";
foreach ($this->log as $key => $value)
{
echo "<tr>";
foreach ($this->log[$key] as $subkey => $subvalue)
{
echo "<td>";
echo $subvalue;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
}
}
private function get_time($normalize = true)
{
$time = microtime(true) * 1000;
if ($normalize)
{
$time = $time - $this->start;
}
$time = substr($time, 0, strpos($time, ".") + 4);
return $time;
}
}
?>
Code: Select all
$profile = new Profiler();
$profile->save('start');
//do some stuff
$profile->save('done some stuff');
//do some other stuff
$profile->save('done');
$profile->display();
Shawn
Re: Advice for a Hack?
Nice class 
I've scaled my effort to base-level for the moment by exported a .txt with the query string as a filename for every action.
Also, my "start/end" code is as follows ...
At the very top of my php doc, I have:
At the very bottom of my php doc, I have:
I'm seeing some very, very fast load times (accept for the index page which is about .75 to 1.5 seconds).
I'm using ob_start() and ob_end_flush() along with ob_gzhandler($buffer, 9).
My service is hostgator(.com)
Any ideas why a php doc might load slowly based on this info? Again, my document output suggests really fast processing. All opinions are welcome.
I've scaled my effort to base-level for the moment by exported a .txt with the query string as a filename for every action.
Also, my "start/end" code is as follows ...
At the very top of my php doc, I have:
Code: Select all
$start = microtime(true);Code: Select all
$ps = $_SERVER["QUERY_STRING"];
if (!$ps) $ps = 'index';
$location = 'debug/'.$ps.'.txt';
$file = fopen($location, 'w');
fwrite($file, $ps.': '.(microtime(true) - $start));
fclose($file);I'm using ob_start() and ob_end_flush() along with ob_gzhandler($buffer, 9).
My service is hostgator(.com)
Any ideas why a php doc might load slowly based on this info? Again, my document output suggests really fast processing. All opinions are welcome.
-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: Advice for a Hack?
Save a few more time readings throughout the page. Then you can see which section(s) are taking long. Once you find the offending loop or function, you/we can work from there.
Re: Advice for a Hack?
The culprit is my use of ob_end_flush in conjunction with ob_gzhandler.
I removed ob_end_flush and all went well ... however, I've read where ob_end_flush is a good thing to have in there. The question I have now is, if it is to be included, where do I put it so that it doesn't bog the load times?
I removed ob_end_flush and all went well ... however, I've read where ob_end_flush is a good thing to have in there. The question I have now is, if it is to be included, where do I put it so that it doesn't bog the load times?
Re: Advice for a Hack?
Here's my point ...
So ... is ob_end_flush something I DO want to use or no?
Code: Select all
function thin($buffer) {
global $debug;
if (!$debug) {
$buffer = str_replace(array("\r", "\n", "\t"), '', $buffer);
$buffer = str_replace('> <', '><', $buffer);
global $d;
global $mobile;
if ($d == 'js' || $mobile) $buffer = preg_replace('#</?script[^>]*>.*?</script>#', '', $buffer);
if ($mobile) {
$buffer = str_replace('<i>', '', $buffer);
$buffer = str_replace('</i>', '', $buffer);
$i = 1;
while (preg_match('#<a href#', $buffer)) {
$buffer = preg_replace('#<a href#', '<a accesskey="' . $i . '" href', $buffer, 1);
$i++;
}
} else {
$buffer = str_replace('/>', '>', $buffer);
$buffer = ob_gzhandler($buffer, 9);
}
} else {
$buffer = str_replace("\n\n\n", "\n\n", $buffer);
}
return $buffer;
}
ob_start('thin');-
shawngoldw
- Forum Contributor
- Posts: 212
- Joined: Mon Apr 05, 2010 3:38 pm
Re: Advice for a Hack?
I'm sorry, you're leaving topics I can help with. I only started to play with output buffers recently. I also don't see ob_end_flush in that function, I'm guessing you took it out? Anyways, maybe someone else can weigh in at this point about output buffers.
Shawn
Shawn