Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 48 additions & 21 deletions platformPhpAnalyzer.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env php
<?php

define('DATE_FORMAT', 'Y-m-d\TH:i:sP');
define('DATE_FORMAT', 'Y-m-d G:i:s');

define('DATE_FORMAT_HOUR', 'Y-m-d\TH');
define('DATE_FORMAT_HOUR', 'Y-m-d G');

ini_set("memory_limit","500M");

Expand All @@ -17,29 +17,29 @@

if (!isset($argv[1])) {
exec('platform projects --format=csv --no-header', $output);

$platformProjects = [];
foreach ($output as $platformProject) {
$platformProjects[] = str_getcsv($platformProject);
}

echo 'Available Platform.sh projects:' . "\n";

for ($i = 0; $i < count($platformProjects); $i++) {
echo ' [' . ($i+1) . '] ' . $platformProjects[$i][1] . ' (' . $platformProjects[$i][0] . ')' . "\n";
}

do {
$selectedProjectNumber = (int) readline('Enter a project number > ');

if (isset($platformProjects[$selectedProjectNumber-1])) {
$selectedProject = $platformProjects[$selectedProjectNumber-1];
break;
} else {
echo 'ERROR: Invalid project number. Please try again.' . "\n";
}
} while (true);

echo "\n";
echo $selectedProject[1] . ' was selected.' . "\n";
} else {
Expand Down Expand Up @@ -84,10 +84,10 @@
echo ' [3] 5000' . "\n";
echo ' [4] 20000' . "\n";
echo ' [5] Max' . "\n";

do {
$lineSelection = (string) readline('Please select (default: [4]) > ');

if (in_array($lineSelection, ['1', '2', '3', '4', '5', ''], true)) {
switch ($lineSelection) {
case '1':
Expand Down Expand Up @@ -117,7 +117,7 @@
}

echo 'Getting the log... ' . "\n";
exec('platform log --lines=' . escapeshellarg($numberOfLines) . ' --project=' . escapeshellarg($selectedProject[0]) . ' --environment=master php.access', $logData);
exec('platform log --lines=' . escapeshellarg($numberOfLines) . ' --project=' . escapeshellarg($selectedProject[0]) . ' --environment=' . escapeshellarg($selectedEnvironment[0]) . ' php.access', $logData);
echo 'Done' . "\n";
echo "\n";

Expand All @@ -134,25 +134,29 @@
echo 'Processing ' . count($logData) . ' lines... ';

$lineData = [];
$lineCount = 0;
for ($i = 0; $i < count($logData); $i++) {
$line = str_getcsv($logData[$i],' ');
$line = explode(' ', $logData[$i]);
$lineCount++;
//progressBar($lineCount, count($logData));

$dateTime = DateTimeImmutable::createFromFormat(DATE_FORMAT, $line[0]);
if($dateTime === false || ($excludeTypo3 && substr($line[8], 0, 7 ) === "/typo3/")) {
// 0 1 2 3 4 5 6 7 8 9
// 2021-11-06 02:20:33 GET 301 11.095 ms 2048 kB 90.13% /request-uri

$dateTime = DateTimeImmutable::createFromFormat(DATE_FORMAT, $line[0] . ' ' . $line[1]);
if($dateTime === false || ($excludeTypo3 && substr($line[9], 0, 7 ) === "/typo3/")) {
continue;
}

$lineData[] = [
'dateTime' => $dateTime,
'requestMethod' => $line[1],
'responseCode' => (int) $line[2],
'executionTime' => (float) $line[3],
'peakMemory' => (int) $line[5],
'cpuPercentage' => (float) substr($line[7], 0, -1),
'requestUri' => $line[8],
'parsedUri' => parse_url($line[8]),
'requestMethod' => $line[2],
'responseCode' => (int) $line[3],
'executionTime' => (float) $line[4],
'peakMemory' => (int) $line[6],
'cpuPercentage' => (float) substr($line[8], 0, -1),
'requestUri' => $line[9],
'parsedUri' => parse_url($line[9]),
];
}

Expand Down Expand Up @@ -291,10 +295,16 @@
for ($lineNumber = 0; $lineNumber < count($lineData); $lineNumber++) {
$line = $lineData[$lineNumber];
$key = round($line['peakMemory']/1024) . 'M';
if (!array_key_exists($key, $memoryUsage)) {
$memoryUsage[$key] = 0;
}
$memoryUsage[$key]++;
$executionTimes[$key][] = $line['executionTime'];
$cpus[$key][] = $line['cpuPercentage'];
if ($line['responseCode'] >= 400) {
if (!array_key_exists($key, $errorResponses)) {
$errorResponses[$key] = 0;
}
$errorResponses[$key]++;
}
}
Expand All @@ -321,6 +331,9 @@
foreach ($memoryUsage as $key=>$value) {
$runningSum -= $value;
$memoryUsagePercentOfTotal[$key] = round(($runningSum/$memoryUsageSum)*100, 1);
if (!array_key_exists($key, $errorResponses)) {
$errorResponses[$key] = 0;
}
$errorResponses[$key] = round(($errorResponses[$key]/$value)*100, 1);
}
?>
Expand Down Expand Up @@ -438,6 +451,9 @@

foreach ($lineData as $line) {
$responseCode = $line['responseCode'];
if (!array_key_exists($responseCode, $topResponseCodes)) {
$topResponseCodes[$responseCode] = 0;
}
$topResponseCodes[$responseCode]++;
$responseCodeMemory[$responseCode][] = $line['peakMemory'];
$responseCodeCpu[$responseCode][] = $line['cpuPercentage'];
Expand Down Expand Up @@ -577,7 +593,16 @@

foreach ($lineData as $line) {
$timeSlot = $line['dateTime']->format(DATE_FORMAT_HOUR);
if (!array_key_exists($line['responseCode'], $responseCodesByTime)) {
$responseCodesByTime[$line['responseCode']] = [];
}
if (!array_key_exists($timeSlot, $responseCodesByTime[$line['responseCode']])) {
$responseCodesByTime[$line['responseCode']][$timeSlot] = 0;
}
$responseCodesByTime[$line['responseCode']][$timeSlot]++;
if (!array_key_exists($timeSlot, $totalRequestsPerTimeSlot)) {
$totalRequestsPerTimeSlot[$timeSlot] = 0;
}
$totalRequestsPerTimeSlot[$timeSlot]++;
$cpuPerTimeSlot[$timeSlot][] = $line['cpuPercentage'];
$memoryPerTimeSlot[$timeSlot][] = $line['peakMemory'];
Expand Down Expand Up @@ -904,6 +929,7 @@
<tbody>
<?php
foreach ($topRequestsByMemory as $request) {
if (!empty($request)) {
?>
<tr>
<td class="table-info"><?php echo $request['peakMemory'] ?></td>
Expand All @@ -913,6 +939,7 @@
<td><?php echo htmlspecialchars($request['requestUri']) ?></td>
</tr>
<?php
}
}
?>
</tbody>
Expand Down