Permalink
Browse files

Add output matrix display.

  • Loading branch information...
ShaneMcC committed Dec 3, 2018
1 parent 6508f60 commit eaf0d48e751add2d27aee6b9b5c65f1e069c88dc
Showing with 326 additions and 5 deletions.
  1. +2 −0 .gitignore
  2. +3 −3 bench.php
  3. +2 −1 config.php
  4. +191 −0 inputMatrix.php
  5. +8 −0 www/functions.php
  6. +5 −1 www/index.php
  7. +85 −0 www/matrix.php
  8. +30 −0 www/rawMatrix.php
@@ -4,3 +4,5 @@ results.*.json
config.local.php
config.local.*.php
.benchlock
outputresults.json
outputresults.*.json
@@ -120,10 +120,10 @@ function getInput($day) {
if (!isset($data['results'][$person])) {
$data['results'][$person] = [];
$data['results'][$person]['name'] = $participant->getName();
$data['results'][$person]['repo'] = $participant->getRepo();
$data['results'][$person]['days'] = [];
}
$data['results'][$person]['name'] = $participant->getName();
$data['results'][$person]['repo'] = $participant->getRepo();
// Run day.
for ($day = 1; $day <= 25; $day++) {
@@ -246,7 +246,7 @@ function secondsToDuration($seconds) {
if ($seconds > 60) {
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
$result .= $minutes . ' minute' . ($minute != 1 ? 's' : '');
$result .= $minutes . ' minute' . ($minutes != 1 ? 's' : '');
}
$result .= $seconds . ' second' . ($seconds != 1 ? 's' : '');
@@ -2,6 +2,7 @@
$lockfile = __DIR__ . '/.benchlock';
$resultsFile = __DIR__ . '/results.json';
$outputResultsFile = __DIR__ . '/outputresults.json';
$participantsDir = __DIR__ . '/participants/';
$repeatCount = 20;
@@ -30,7 +31,7 @@ public function prepare() {
public function hasDay($day) { return $this->getVersion($day) !== NULL; }
public function getInput($day) {
return file_get_contents($this->getInputFilename($day));
return file_exists($this->getInputFilename($day)) ? file_get_contents($this->getInputFilename($day)) : '';
}
public function setInput($day, $input) {
@@ -0,0 +1,191 @@
#!/usr/bin/php
<?php
require_once(__DIR__ . '/config.php');
if (!file_exists($lockfile)) { file_put_contents($lockfile, ''); }
$fp = fopen($lockfile, 'r+');
if (!flock($fp, LOCK_EX | LOCK_NB)) {
echo 'Unable to get lock on ', $lockfile, "\n";
exit(1);
}
$startTime = time();
echo 'inputMatrix starting at: ', date('r'), "\n";
// Load old data file.
$data = ['results' => []];
if (file_exists($outputResultsFile)) {
$data = json_decode(file_get_contents($outputResultsFile), true);
}
// Save Data.
function saveData() {
global $data, $outputResultsFile, $hasRun;
if ($hasRun || !isset($data['time'])) { $data['time'] = time(); }
// Output results to disk.
file_put_contents($outputResultsFile, json_encode($data));
}
// Get CLI Options.
$__CLIOPTS = getopt('fp:d:h', ['force', 'participant:', 'day:', 'help', 'no-update']);
function getOptionValue($short = NULL, $long = NULL, $default = '') {
global $__CLIOPTS;
if ($short !== NULL && array_key_exists($short, $__CLIOPTS)) { $val = $__CLIOPTS[$short]; }
else if ($long !== NULL && array_key_exists($long, $__CLIOPTS)) { $val = $__CLIOPTS[$long]; }
else { $val = $default; }
if (is_array($val)) { $val = array_pop($val); }
return $val;
}
$noUpdate = getOptionValue(NULL, 'no-update', NULL) !== NULL;
$wantedParticipant = getOptionValue('p', 'participant', '.*');
$wantedDay = getOptionValue('d', 'day', '.*');
$force = getOptionValue('f', 'force', NULL) !== NULL;
if (getOptionValue('h', 'help', NULL) !== NULL) {
echo 'AoCBench input matrix generator.', "\n";
echo "\n";
echo 'Usage: ', $_SERVER['argv'][0], ' [options]', "\n";
echo '', "\n";
echo 'Valid options:', "\n";
echo ' -h, --help Show this help output', "\n";
echo ' -f, --force Force run matching participants/days that would', "\n";
echo ' otherwise be ignored due to lack of changes.', "\n";
echo ' -p, --participant <regex> Only look at participants matching <regex> (This', "\n";
echo ' is automatically anchored start/end)', "\n";
echo ' -d, --day <regex> Only look at days matching <regex> (This is', "\n";
echo ' automatically anchored start/end)', "\n";
echo ' --no-update Do not update repos.', "\n";
echo '', "\n";
echo 'If not specified, day and participant both default to ".*" to match all', "\n";
echo 'participants/days.', "\n";
die();
}
// Get all inputs.
$inputs = [];
$cwd = getcwd();
echo 'Getting inputs.', "\n";
foreach ($participants as $participant) {
$person = preg_replace('#[^A-Z0-9-_]#i', '', $participant->getName());
echo $participant->getName() , ': ', "\n";
$dir = $participantsDir . '/' . $person;
if (!$noUpdate) {
chdir($cwd);
$participant->updateRepo($dir);
}
chdir($dir);
for ($day = 1; $day <= 25; $day++) {
$input = $participant->getInput($day);
$inputs[$day][$person]['input'] = $input;
$inputs[$day][$person]['version'] = $participant->getVersion($day);
}
echo 'Done.', "\n";
}
chdir($cwd);
// Remove days with no inputs.
for ($day = 1; $day <= 25; $day++) {
$hasInputs = false;
foreach ($inputs[$day] as $person => $input) {
if (!empty($input['input'])) { $hasInputs = true; continue; }
}
if (!$hasInputs) { unset($inputs[$day]); }
}
echo "\n", 'Running.', "\n";
foreach ($participants as $participant) {
$person = preg_replace('#[^A-Z0-9-_]#i', '', $participant->getName());
if (!preg_match('#^' . $wantedParticipant. '$#', $person)) { continue; }
echo "\n", $participant->getName() , ': ', "\n";
$dir = $participantsDir . '/' . $person;
chdir($dir);
// Prepare.
echo 'Preparing.', "\n";
$participant->prepare();
if (!isset($data['results'][$person])) {
$data['results'][$person] = [];
$data['results'][$person]['days'] = [];
}
$data['results'][$person]['name'] = $participant->getName();
$data['results'][$person]['repo'] = $participant->getRepo();
// Run day.
for ($day = 1; $day <= 25; $day++) {
if (!$participant->hasDay($day)) { continue; }
if (!preg_match('#^' . $wantedDay. '$#', $day)) { continue; }
$thisDay = isset($data['results'][$person]['days'][$day]) ? $data['results'][$person]['days'][$day] : ['outputs' => []];
echo 'Day ', $day, ':', "\n";
$currentVersion = isset($thisDay['version']) ? $thisDay['version'] : 'Unknown';
// Should we skip this?
$skip = !empty($thisDay['outputs']);
$skip &= ($currentVersion == $participant->getVersion($day));
if ($force) {
echo ' [Forced]', "\n";
$skip = false;
}
foreach ($inputs[$day] as $inputPerson => $input) {
echo ' ', $inputPerson, ': ';
$thisInputVersion = isset($thisDay['outputs'][$inputPerson]['version']) ? $thisDay['outputs'][$inputPerson]['version'] : 'Unknown';
if ($skip && $thisInputVersion == $input['version']) { echo 'Up to date.', "\n"; continue; }
$participant->setInput($day, $input['input']);
list($ret, $result) = $participant->run($day);
$thisDay['outputs'][$inputPerson] = ['version' => $input['version'], 'return' => $ret, 'output' => $result];
echo 'Done!', "\n";
}
echo "\n";
// Update data
$thisDay['version'] = $participant->getVersion($day);
$data['results'][$person]['days'][$day] = $thisDay;
// Save the data.
saveData();
}
$participant->cleanup();
}
function secondsToDuration($seconds) {
$result = '';
if ($seconds > 60 * 60) {
$hours = floor($seconds / (60 * 60));
$seconds -= $hours * 60 * 60;
$result .= $hours . ' hour' . ($hours != 1 ? 's' : '');
}
if ($seconds > 60) {
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
$result .= $minutes . ' minute' . ($minutes != 1 ? 's' : '');
}
$result .= $seconds . ' second' . ($seconds != 1 ? 's' : '');
return $result;
}
$endTime = time();
echo "\n";
echo 'inputMatrix ended at: ', date('r'), "\n";
echo 'Took: ', secondsToDuration($endTime - $startTime), "\n";
@@ -9,6 +9,14 @@
}
}
$hasMatrix = false;
if (file_exists($outputResultsFile)) {
$matrix = json_decode(file_get_contents($outputResultsFile), true);
if (isset($matrix['results'])) {
$hasMatrix = true;
}
}
function getDayBestTime($day, $method) {
global $data;
@@ -28,7 +28,11 @@
echo '<th class="day">&nbsp;</th>';
$p = 1;
foreach ($data['results'] as $participant => $pdata) {
$link = '<a href="' . $pdata['repo'] . '"><img height="16px" width="16px" src="https://github.com/favicon.ico" alt="github"></a>';
if (isset($pdata['repo'])) {
$link = '<a href="' . $pdata['repo'] . '"><img height="16px" width="16px" src="https://github.com/favicon.ico" alt="github"></a>';
} else {
$link = '';
}
if (isset($_REQUEST['anon'])) { $participant = 'Participant ' . $p++; }
echo '<th class="participant">', $participant, ' ', $link, '</th>';
@@ -0,0 +1,85 @@
<?php
require_once(__DIR__ . '/functions.php');
$pageid = 'matrix';
require_once(__DIR__ . '/header.php');
if ($hasMatrix) {
for ($day = 1; $day <= 25; $day++) {
// Build day matrix.
$dayMatrix = [];
$dayParticipants = array_keys($matrix['results']);
$hasDay = false;
foreach ($dayParticipants as $p1) {
foreach ($dayParticipants as $p2) {
if (isset($matrix['results'][$p1]['days'][$day]['outputs'][$p2])) {
$hasDay = true;
$dayMatrix[$p1][$p2] = $matrix['results'][$p1]['days'][$day]['outputs'][$p2];
}
}
}
if (!$hasDay) { continue; }
echo '<h2>Day ', $day, '</h2>', "\n";
echo '<table class="table table-striped table-bordered">';
// Participants
echo '<thead>';
echo '<tr>';
echo '<th class="who">&nbsp;</th>';
$p = 1;
foreach ($dayParticipants as $participant) {
$pdata = $matrix['results'][$participant];
if (isset($pdata['repo'])) {
$link = '<a href="' . $pdata['repo'] . '"><img height="16px" width="16px" src="https://github.com/favicon.ico" alt="github"></a>';
} else {
$link = '';
}
if (isset($_REQUEST['anon'])) { $participant = 'Participant ' . $p++; }
echo '<th class="output">', $participant, ' ', $link, '</th>';
}
echo '</tr>', "\n";
foreach ($dayParticipants as $p2) {
echo '<tr>';
echo '<th class="who">', $p2, '</th>';
foreach ($dayParticipants as $p1) {
$classes = ['output'];
if ($dayMatrix[$p1][$p2]['return'] != '0') {
$classes[] = 'table-danger';
} else if ($p1 == $p2) {
$classes[] = 'table-primary';
}
echo '<td class="', implode(' ', $classes),'"><pre>', htmlspecialchars(implode("\n", $dayMatrix[$p1][$p2]['output'])), '</pre></td>';
}
echo '</tr>';
}
echo '</thead>';
echo '<tbody>';
echo '</tbody>';
echo '</table>';
}
echo '<p class="text-muted text-right">';
if (isset($data['time'])) {
echo '<small>Last updated: ', date('r', $data['time']), '</small>';
}
echo '</p>';
echo '<script src="./index.js"></script>';
} else {
echo 'No results yet.';
}
require_once(__DIR__ . '/footer.php');
@@ -0,0 +1,30 @@
<?php
require_once(__DIR__ . '/functions.php');
$pageid = 'rawMatrix';
if (isset($_REQUEST['json'])) {
header('Content-Type: application/json');
} else {
require_once(__DIR__ . '/header.php');
}
if ($hasMatrix) {
if (isset($_REQUEST['json'])) {
echo json_encode($matrix, JSON_PRETTY_PRINT);
} else {
echo '<h2>Raw Data</h2>', "\n";
echo '<small><a href="rawMatrix.json">json</a></small>', "\n";
echo '<br><br>';
echo '<pre>';
echo htmlspecialchars(json_encode($matrix, JSON_PRETTY_PRINT));
echo '</pre>';
}
} else {
if (isset($_REQUEST['json'])) {
echo '[]';
} else {
echo 'No results yet.';
}
}
if (!isset($_REQUEST['json'])) { require_once(__DIR__ . '/footer.php'); }

0 comments on commit eaf0d48

Please sign in to comment.