Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor opportunities & experiments includes #2641

Merged
merged 4 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions www/constants.inc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ define('TESTS_PATH', realpath(APP_ROOT . '/tests/'));
define('INCLUDES_PATH', WWW_PATH); // same as www for now
define('SETTINGS_PATH', realpath(WWW_PATH . '/settings/'));
define('TEMP_DIR', realpath(WWW_PATH . '/tmp/'));
define('OE_PATH', realpath(WWW_PATH . '/experiments/'));

define('VER_WEBPAGETEST', '21.07'); // webpagetest version
define('VER_TYPOGRAPHY_CSS', @md5_file(ASSETS_PATH . '/css/typography.css')); // version of the typography css file
Expand Down
64 changes: 64 additions & 0 deletions www/experiments/AssesmentRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

class AssesmentRegistry {
private static $instance = null;

const Quick = 'Quick';
const Usable = 'Usable';
const Resilient = 'Resilient';
const Custom = 'Custom';

private $assessments = [
self::Quick => [
'grade' => '',
'sentiment' => '',
'summary' => '',
'opportunities' => [],
'num_recommended' => 0,
'num_experiments' => 0
],
self::Usable => [
'grade' => '',
'sentiment' => '',
'summary' => '',
'opportunities' => [],
'num_recommended' => 0,
'num_experiments' => 0
],
self::Resilient => [
'grade' => '',
'sentiment' => '',
'summary' => '',
'opportunities' => [],
'num_recommended' => 0,
'num_experiments' => 0
],
self::Custom => [
'grade' => '',
'sentiment' => 'Advanced!',
'summary' => '',
'opportunities' => [],
'num_recommended' => 0,
'num_experiments' => 0
],
];

public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new AssesmentRegistry();
}

return self::$instance;
}

public function register($category, $opportunity) {
$this->assessments[$category]['opportunities'][] = $opportunity;
}

public function getAll() {
return $this->assessments;
}

}

136 changes: 69 additions & 67 deletions www/experiments/axe-warnings.inc
Original file line number Diff line number Diff line change
@@ -1,80 +1,82 @@
<?php
(function () {

$category = "Usable";
if (isset($axe)) {
if (count($axe['violations'])) {
$violations = $axe['violations'];
$num_violations = count($violations);
$num_critical = 0;
$num_serious = 0;
$num_moderate = 0;
$num_minor = 0;
global $axe;

foreach ($violations as $v) {
if ($v['impact'] === "critical") {
$num_critical++;
}
if ($v['impact'] === "serious") {
$num_serious++;
}
if ($v['impact'] === "moderate") {
$num_moderate++;
}
if ($v['impact'] === "minor") {
$num_minor++;
}
}

$warningsArr = array();
foreach ($violations as $v) {
array_push($warningsArr, $v['impact'] . ": " . $v['help']);
}
if (isset($axe)) {
if (count($axe['violations'])) {
$violations = $axe['violations'];
$num_violations = count($violations);
$num_critical = 0;
$num_serious = 0;
$num_moderate = 0;
$num_minor = 0;

$recs = array();
foreach ($violations as $v) {
$thisRec = '<h6 class="recommendation_level-' . $v['impact'] . '"><span>' . $v['impact'] . '</span> ' . htmlentities($v['help']) . ' <a href="' . $v['helpUrl'] . '">More info</a></h6>';
if ($v["nodes"] && count($v["nodes"])) {
//print_r($v["nodes"])
if (count($v["nodes"]) > 6) {
$thisRec .= '<ul class="util_overflow_more">';
} else {
$thisRec .= '<ul>';
foreach ($violations as $v) {
if ($v['impact'] === "critical") {
$num_critical++;
}
foreach ($v["nodes"] as $vnode) {
$thisRec .= '<li>' . htmlentities($vnode["failureSummary"]) . '<code>' . htmlentities($vnode["html"]) . '</code></li>';
if ($v['impact'] === "serious") {
$num_serious++;
}
if ($v['impact'] === "moderate") {
$num_moderate++;
}
if ($v['impact'] === "minor") {
$num_minor++;
}
$thisRec .= '</ul>';
}

array_push($recs, $thisRec);
}
$warningsArr = array();
foreach ($violations as $v) {
array_push($warningsArr, $v['impact'] . ": " . $v['help']);
}

$recs = array();
foreach ($violations as $v) {
$thisRec = '<h6 class="recommendation_level-' . $v['impact'] . '"><span>' . $v['impact'] . '</span> ' . htmlentities($v['help']) . ' <a href="' . $v['helpUrl'] . '">More info</a></h6>';
if ($v["nodes"] && count($v["nodes"])) {
//print_r($v["nodes"])
if (count($v["nodes"]) > 6) {
$thisRec .= '<ul class="util_overflow_more">';
} else {
$thisRec .= '<ul>';
}
foreach ($v["nodes"] as $vnode) {
$thisRec .= '<li>' . htmlentities($vnode["failureSummary"]) . '<code>' . htmlentities($vnode["html"]) . '</code></li>';
}
$thisRec .= '</ul>';
}

array_push($recs, $thisRec);
}

$opp = [
"title" => "Accessibility Issues were Detected",
"desc" => "Axe found $num_violations accessibility issues: " .
($num_critical > 0 ? "$num_critical critical, " : '') .
($num_serious > 0 ? "$num_serious serious, " : '') .
($num_moderate > 0 ? "$num_moderate moderate, " : '') .
($num_minor > 0 ? "$num_minor minor " : ''),
"examples" => array(),
"experiments" => array(
(object) [
'title' => 'Make the following changes to improve accessibility:',
"desc" => implode($recs)
]
),
"good" => false
];
} else {
$opp = [
"title" => "Zero Accessibility Issues were Detected",
"desc" => "Axe found no accessibility issues. ",
"examples" => array(),
"experiments" => array(),
"good" => true
];
}

$assessment[$category]["opportunities"][] = array(
"title" => "Accessibility Issues were Detected",
"desc" => "Axe found $num_violations accessibility issues: " .
($num_critical > 0 ? "$num_critical critical, " : '') .
($num_serious > 0 ? "$num_serious serious, " : '') .
($num_moderate > 0 ? "$num_moderate moderate, " : '') .
($num_minor > 0 ? "$num_minor minor " : ''),
"examples" => array(),
"experiments" => array(
(object) [
'title' => 'Make the following changes to improve accessibility:',
"desc" => implode($recs)
]
),
"good" => false
);
} else {
$assessment[$category]["opportunities"][] = array(
"title" => "Zero Accessibility Issues were Detected",
"desc" => "Axe found no accessibility issues. ",
"examples" => array(),
"experiments" => array(),
"good" => true
);
AssesmentRegistry::getInstance()->register(AssesmentRegistry::Usable, $opp);
}
}
})();
98 changes: 52 additions & 46 deletions www/experiments/cache.inc
Original file line number Diff line number Diff line change
@@ -1,57 +1,63 @@
<?php
(function () {

$category = "Quick";
$rawResults = $testStepResult->getRawResults();
$needCache = array();
if (isset($rawResults['score_cache'])) {
foreach ($requests as $index => &$request) {
if (isset($request['score_cache']) && $request['score_cache'] >= 0 && $request['score_cache'] < 100) {
$key = $request['cache_time'] . ' ' . $request['host'] . ' ' . $index;
global $testStepResult;
global $requests;

if ($request['score_cache'] < 50) {
$value = 'FAILED ';
} else {
$value = 'WARNING ';
}
$rawResults = $testStepResult->getRawResults();
$needCache = array();
if (isset($rawResults['score_cache'])) {
foreach ($requests as $index => &$request) {
if (isset($request['score_cache']) && $request['score_cache'] >= 0 && $request['score_cache'] < 100) {
$key = $request['cache_time'] . ' ' . $request['host'] . ' ' . $index;

$time = 'No max-age or expires';
if ($request['cache_time'] > 0) {
if ($request['cache_time'] > 86400) {
$time = number_format($request['cache_time'] / 86400.0, 1) . ' days';
} elseif ($request['cache_time'] > 3600) {
$time = number_format($request['cache_time'] / 3600.0, 1) . ' hours';
} elseif ($request['cache_time'] > 60) {
$time = number_format($request['cache_time'] / 60.0, 1) . ' minutes';
if ($request['score_cache'] < 50) {
$value = 'FAILED ';
} else {
$time = $request['cache_time'] . ' seconds';
$value = 'WARNING ';
}

$time = 'No max-age or expires';
if ($request['cache_time'] > 0) {
if ($request['cache_time'] > 86400) {
$time = number_format($request['cache_time'] / 86400.0, 1) . ' days';
} elseif ($request['cache_time'] > 3600) {
$time = number_format($request['cache_time'] / 3600.0, 1) . ' hours';
} elseif ($request['cache_time'] > 60) {
$time = number_format($request['cache_time'] / 60.0, 1) . ' minutes';
} else {
$time = $request['cache_time'] . ' seconds';
}
}
}

$proto = 'http://';
if ($request['is_secure']) {
$proto = 'https://';
$proto = 'http://';
if ($request['is_secure']) {
$proto = 'https://';
}
$value .= "($time): " . $proto . $request['host'] . $request['url'];
$needCache[$key] = $value;
}
$value .= "($time): " . $proto . $request['host'] . $request['url'];
$needCache[$key] = $value;
}
$needCache = array_unique($needCache);
ksort($needCache);
}

$cacheOppDesc = "Cache settings can instruct browsers and intermediaries to store recent versions of a site's static files (JavaScript, CSS, Images, fonts...) for reuse, reducing page weight and latency.";
if (count($needCache) > 0) {
$opp = [
"title" => count($needCache) . " static file" . (count($needCache) > 1 ? "s have" : " has") . " inadequate cache settings.",
"desc" => $cacheOppDesc,
"examples" => $needCache,
"good" => false
];
} else {
$opp = [
"title" => 'This site\'s static files are configured for optimal caching.',
"desc" => $cacheOppDesc,
"examples" => array(),
"good" => true
];
}
$needCache = array_unique($needCache);
ksort($needCache);
}

$cacheOppDesc = "Cache settings can instruct browsers and intermediaries to store recent versions of a site's static files (JavaScript, CSS, Images, fonts...) for reuse, reducing page weight and latency.";
if (count($needCache) > 0) {
$assessment[$category]["opportunities"][] = array(
"title" => count($needCache) . " static file" . (count($needCache) > 1 ? "s have" : " has") . " inadequate cache settings.",
"desc" => $cacheOppDesc,
"examples" => $needCache,
"good" => false
);
} else {
$assessment[$category]["opportunities"][] = array(
"title" => 'This site\'s static files are configured for optimal caching.',
"desc" => $cacheOppDesc,
"examples" => array(),
"good" => true
);
}
AssesmentRegistry::getInstance()->register(AssesmentRegistry::Quick, $opp);
})();
Loading