Skip to content

Instantly share code, notes, and snippets.

@SitesByYogi
Created July 1, 2024 17:24
Show Gist options
  • Save SitesByYogi/c1c2607fb7277d0c1007d93f7b73c310 to your computer and use it in GitHub Desktop.
Save SitesByYogi/c1c2607fb7277d0c1007d93f7b73c310 to your computer and use it in GitHub Desktop.
<?php
function ezdump($variable){
echo '<pre>' . print_r($variable, true) . '</pre>';
}
$dataPoints1 = array();
function logToData($logname){
$logdata = file($logname);
$first = null;
foreach($logdata as $lineNumber => $logline){
$logline = explode(" " , $logline);
if(empty($first) && $lineNumber == 0){
$first = new DateTime($logline[0]);
}
$logline[0] = new DateTime($logline[0]);
$elapsed = $logline[0]->format('U') - $first->format('U') ;
$logdata[$lineNumber] = array("x" => $elapsed, "y" => str_replace("\n", "", $logline[1]));
}
return $logdata;
}
function getLoadAvg($data){
$sum = 0;
for ($i = 0; $i < count($data); $i++) {
$sum += $data[$i]["y"];
}
$avg = $sum / count($data);
return $avg;
}
$dataPoints1 = logToData('uptime.log');
$dataPoints2 = logToData('uptime2.log');
$avg1 = getLoadAvg($dataPoints1);
$avg2 = getLoadAvg($dataPoints2);
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<?php //ezdump($dataPoints1); ezdump($dataPoints2)?>
<div id="chartContainer" style="height: 370px; width: 80%; padding: 5%;"></div>
<div><h2>Averages</h2><p>Uncache: <?php echo $avg1; ?></p><p>Cache: <?php echo $avg2; ?></p></div>
<script src="https://cdn.canvasjs.com/canvasjs.min.js"></script>
<script>
window.onload = function () {
var data1 = <?php echo json_encode($dataPoints1, JSON_NUMERIC_CHECK); ?>;
var data2 = <?php echo json_encode($dataPoints2, JSON_NUMERIC_CHECK); ?>;
console.log(data1);
console.log(data2);
var chart = new CanvasJS.Chart("chartContainer", {
animationEnabled: true,
title:{
text: "WP Rest API caching effects on load average"
},
axisX:{
title: "Seconds into test"
},
axisY:{
title: "Load average",
},
legend:{
cursor: "pointer",
dockInsidePlotArea: true,
itemclick: toggleDataSeries
},
data: [{
type: "line",
name: "No Cache",
markerSize: 0,
toolTipContent: "Time in seconds: {x} <br>Load average: {y}",
showInLegend: true,
dataPoints: data1
},
{
type: "line",
name: "Cached",
markerSize: 0,
toolTipContent: "Time in seconds: {x} <br>Load average: {y}",
showInLegend: true,
dataPoints: data2
},
]
});
chart.render();
function toggleDataSeries(e){
if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
}
else{
e.dataSeries.visible = true;
}
chart.render();
}
}
</script>
</body>
</html>
@SitesByYogi
Copy link
Author

https://w3tc.weapon-x.dev/servLoad.php

Command:
watch -n 1 "uptime | awk '{print $1, $10}' | sed 's/,//' >> uptime.log"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment