-
Notifications
You must be signed in to change notification settings - Fork 715
/
Copy pathvalidate.php
177 lines (157 loc) · 5.13 KB
/
validate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
include './settings.inc';
$results = array();
$urls = array();
$counts = array('total' => 0, 'complete' => 0, 'similar' => 0, 'different' => 0);
$testCount = 0;
// Group the results by URL
if (LoadResults($results)) {
foreach ($results as $result) {
if (isset($result['id']) && isset($result['url'])) {
if (!isset($urls[$result['url']])) {
$urls[$result['url']] = array();
}
$urls[$result['url']][$result['id']] = $result;
}
}
}
$testCount = count($urls);
echo "Validating the results for $testCount comparisons...\r\n";
// Loop through the URLs and make sure all of the tests are comprable
// (result, TTFB, requests and last video frames are all similar)
foreach ($urls as &$url) {
$counts['total']++;
echo "\rValidating the results of comparison {$counts['total']} of $testCount... ";
if (AllTestsComplete($url)) {
$counts['complete']++;
if (MetricsSimilar($url) && ImagesSimilar($url)) {
$counts['similar']++;
} else {
$counts['different']++;
// mark all of the configurations to be resubmitted
foreach ($url as $url_result) {
if (isset($url_result['id'])) {
foreach ($results as &$original_result) {
if (isset($original_result['id']) && $original_result['id'] == $url_result['id']) {
$original_result['resubmit'] = true;
break;
}
}
}
}
}
// update the similarity values for the raw results
foreach ($url as $url_result) {
if (isset($url_result['id']) && isset($url_result['sim'])) {
foreach ($results as &$original_result) {
if (isset($original_result['id']) && $original_result['id'] == $url_result['id']) {
$original_result['sim'] = $url_result['sim'];
break;
}
}
}
}
}
}
// clear the progress text
echo "\r \r";
StoreResults($results);
foreach ($counts as $label => $count) {
echo "$label: $count\r\n";
}
function AllTestsComplete(&$url)
{
$all_complete = true;
foreach ($url as $result) {
if (!isset($result['result']) || (isset($result['resubmit']) && $result['resubmit'])) {
$all_complete = false;
break;
}
}
return $all_complete;
}
function MetricsSimilar(&$url)
{
$similar = true;
// make sure the result codes are the same
$baseline = null;
foreach ($url as $result) {
if (isset($baseline)) {
if ($result['result'] !== $baseline) {
$similar = false;
break;
}
} else {
$baseline = $result['result'];
}
}
// make sure the TTFB is within 200ms
if ($similar) {
$baseline = null;
foreach ($url as $result) {
if (isset($baseline)) {
if (abs($result['TTFB'] - $baseline) > 200) {
$similar = false;
break;
}
} else {
$baseline = $result['TTFB'];
}
}
}
// make sure the request count is within 5
if ($similar) {
$baseline = null;
foreach ($url as $result) {
if (isset($baseline)) {
if (abs($result['requestsDoc'] - $baseline) > 5) {
$similar = false;
break;
}
} else {
$baseline = $result['requestsDoc'];
}
}
}
return $similar;
}
function ImagesSimilar(&$url)
{
global $video;
global $server;
$similar = true;
if ($video) {
// ask the server to visually compare the last frames (if we don't already have similarity numbers)
$needSimilarity = false;
foreach ($url as $result) {
if (!isset($result['sim']) || $result['sim'] < 0) {
$needSimilarity = true;
break;
}
}
if ($needSimilarity) {
// build up the similarity request
$api = "{$server}video/compareFrames.php?tests=";
foreach ($url as $result) {
if (isset($result['id']) && isset($result['run'])) {
$api .= "{$result['id']}-r:{$result['run']},";
}
}
$raw = file_get_contents($api);
$compare = json_decode($raw, true);
if (isset($compare['data']) && is_array($compare['data'])) {
foreach ($compare['data'] as $test => $similarity) {
$url[$test]['sim'] = $similarity;
}
}
}
// if the similarity is < 85% for any of them it fails
foreach ($url as $result) {
if (!isset($result['sim']) || $result['sim'] < 85) {
$similar = false;
break;
}
}
}
return $similar;
}