Skip to content

Instantly share code, notes, and snippets.

@daniellockyer
Created November 23, 2018 03:59
Show Gist options
  • Save daniellockyer/2ed0f4a501e4ae055df491a7fd52ba68 to your computer and use it in GitHub Desktop.
Save daniellockyer/2ed0f4a501e4ae055df491a7fd52ba68 to your computer and use it in GitHub Desktop.
PHP response time histograms
<?php
require_once(__DIR__.'/vendor/autoload.php');
function remove_outliers($dataset, $magnitude = 1) {
$count = count($dataset);
$mean = array_sum($dataset) / $count;
$deviation = sqrt(array_sum(array_map("sd_square", $dataset, array_fill(0, $count, $mean))) / $count) * $magnitude;
return array_filter($dataset, function($x) use ($mean, $deviation) { return ($x <= $mean + $deviation && $x >= $mean - $deviation); });
}
function sd_square($x, $mean) { return pow($x - $mean, 2); }
$config = new \IcyApril\Tail\Config("/var/log/php7.2-fpm.access.log");
$config->setLines(100000);
$tail = new \IcyApril\Tail\Tail($config);
$lines = explode("\n", $tail->getTail());
$data = $finalData = array();
foreach($lines as $line) {
//if (!stripos($line, "<filter>")) continue; // Replace <filter> with something you want to filter by
$time = (int) round(strstr($line, " ", true));
array_push($data, $time);
}
$data = remove_outliers($data);
foreach(array_count_values($data) as $k => $v) array_push($finalData, array("x" => $k, "y" => $v));
?>
<!DOCTYPE HTML>
<html>
<body>
<div id="chart" style="height:480px; width:640px;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<script>
window.onload = function() {
var chart = new CanvasJS.Chart("chart", {
theme: "light2",
axisX: { title: "Time (ms)", titleFontSize: 14 },
axisY: { maximum: 500 },
data: [{ type: "column", dataPoints: <?php echo json_encode($finalData, JSON_NUMERIC_CHECK); ?> }]
});
chart.render();
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment