Perf dashboard always re-generate measurement set JSON
[WebKit.git] / Websites / perf.webkit.org / public / api / measurement-set.php
1 <?php
2
3 require('../include/json-header.php');
4
5 function main() {
6     $program_start_time = microtime(true);
7
8     $arguments = validate_arguments($_GET, array(
9         'platform' => 'int?',
10         'metric' => 'int?',
11         'analysisTask' => 'int?'));
12
13     $platform_id = $arguments['platform'];
14     $metric_id = $arguments['metric'];
15     $task_id = $arguments['analysisTask'];
16     if (!(($platform_id && $metric_id && !$task_id) || ($task_id && !$platform_id && !$metric_id)))
17         exit_with_error('AmbiguousRequest');
18
19     $db = new Database;
20     if (!$db->connect())
21         exit_with_error('DatabaseConnectionFailure');
22
23     if ($task_id) {
24         $fetcher = new AnalysisResultsFetcher($db, $task_id);
25         exit_with_success($fetcher->fetch());
26     }
27
28     $fetcher = new MeasurementSetFetcher($db);
29     if (!$fetcher->fetch_config_list($platform_id, $metric_id)) {
30         exit_with_error('ConfigurationNotFound',
31             array('platform' => $platform_id, 'metric' => $metric_id));
32     }
33
34     if ($fetcher->at_end()) {
35         header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
36         exit(404);
37     }
38
39     $cluster_count = 0;
40     while (!$fetcher->at_end()) {
41         $content = $fetcher->fetch_next_cluster();
42         $cluster_count++;
43         if ($fetcher->at_end()) {
44             $cache_filename = "measurement-set-$platform_id-$metric_id.json";
45             $content['clusterCount'] = $cluster_count;
46             $content['elapsedTime'] = (microtime(true) - $program_start_time) * 1000;
47         } else
48             $cache_filename = "measurement-set-$platform_id-$metric_id-{$content['endTime']}.json";
49
50         $json = success_json($content);
51         generate_data_file($cache_filename, $json);
52     }
53
54     echo $json;
55 }
56
57 define('DAY', 24 * 3600 * 1000);
58 define('YEAR', 365.24 * DAY);
59 define('MONTH', 30 * DAY);
60
61 class MeasurementSetFetcher {
62     function __construct($db) {
63         $this->db = $db;
64         $this->queries = NULL;
65
66         // Each cluster contains data points between two commit time
67         // as well as a point immediately before and a point immediately after these points.
68         // Clusters are fetched in chronological order.
69         $start_time = config('clusterStart');
70         $size = config('clusterSize');
71         $this->cluster_start = mktime($start_time[3], $start_time[4], 0, $start_time[1], $start_time[2], $start_time[0]) * 1000;
72         $this->next_cluster_start = $this->cluster_start;
73         $this->next_cluster_results = NULL;
74         $this->cluster_size = $size[0] * YEAR + $size[1] * MONTH + $size[2] * DAY;
75         $this->last_modified = 0;
76
77         $this->start_time = microtime(TRUE);
78     }
79
80     function fetch_config_list($platform_id, $metric_id) {
81         $config_rows = $this->db->query_and_fetch_all('SELECT *
82             FROM test_configurations WHERE config_metric = $1 AND config_platform = $2',
83             array($metric_id, $platform_id));
84         $this->config_rows = $config_rows;
85         if (!$config_rows)
86             return FALSE;
87
88         $this->queries = array();
89         $this->next_cluster_results = array();
90         $min_commit_time = microtime(TRUE) * 1000;
91         foreach ($config_rows as &$config_row) {
92             $query = $this->execute_query($config_row['config_id']);
93
94             $this->last_modified = max($this->last_modified, Database::to_js_time($config_row['config_runs_last_modified']));
95
96             $measurement_row = $this->db->fetch_next_row($query);
97             if ($measurement_row) {
98                 $commit_time = 0;
99                 $formatted_row = self::format_run($measurement_row, $commit_time);
100                 $this->next_cluster_results[$config_row['config_type']] = array($formatted_row);
101                 $min_commit_time = min($min_commit_time, $commit_time);
102             } else
103                 $query = NULL;
104
105             $this->queries[$config_row['config_type']] = $query;
106         }
107
108         while ($this->next_cluster_start + $this->cluster_size < $min_commit_time)
109             $this->next_cluster_start += $this->cluster_size;
110
111         return TRUE;
112     }
113
114     function at_end() {
115         if ($this->queries === NULL)
116             return FALSE;
117         foreach ($this->queries as $name => &$query) {
118             if ($query)
119                 return FALSE;
120         }
121         return TRUE;
122     }
123     
124     function fetch_next_cluster() {
125         assert($this->queries);
126
127         $results_by_config = array();
128         $current_cluster_start = $this->next_cluster_start;
129         $this->next_cluster_start += $this->cluster_size;
130
131         foreach ($this->queries as $name => &$query) {
132             assert($this->next_cluster_start);
133
134             $carry_over = array_get($this->next_cluster_results, $name);
135             if ($carry_over)
136                 $results_by_config[$name] = $carry_over;
137             else
138                 $results_by_config[$name] = array();
139
140             if (!$query)
141                 continue;
142
143             while ($row = $this->db->fetch_next_row($query)) {
144                 $commit_time = NULL;
145                 $formatted_row = self::format_run($row, $commit_time);
146                 array_push($results_by_config[$name], $formatted_row);
147                 $row_belongs_to_next_cluster = $commit_time > $this->next_cluster_start;
148                 if ($row_belongs_to_next_cluster)
149                     break;
150             }
151
152             $reached_end = !$row;
153             if ($reached_end)
154                 $this->queries[$name] = NULL;
155             else {
156                 $this->next_cluster_results[$name] = array_slice($results_by_config[$name], -2);
157             }
158         }
159
160         return array(
161             'clusterStart' => $this->cluster_start,
162             'clusterSize' => $this->cluster_size,
163             'configurations' => &$results_by_config,
164             'formatMap' => self::format_map(),
165             'startTime' => $current_cluster_start,
166             'endTime' => $this->next_cluster_start,
167             'lastModified' => $this->last_modified);
168     }
169
170     function execute_query($config_id) {
171         return $this->db->query('
172             SELECT test_runs.*, build_id, build_number, build_builder, build_time,
173             array_agg((commit_id, commit_repository, commit_revision, extract(epoch from commit_time) * 1000)) AS revisions,
174             extract(epoch from max(commit_time)) * 1000 AS revision_time, max(commit_order) AS revision_order
175                 FROM builds
176                     LEFT OUTER JOIN build_commits ON commit_build = build_id
177                     LEFT OUTER JOIN commits ON build_commit = commit_id, test_runs
178                 WHERE run_build = build_id AND run_config = $1 AND NOT EXISTS (SELECT * FROM build_requests WHERE request_build = build_id)
179                 GROUP BY build_id, run_id ORDER BY revision_time, revision_order, build_time', array($config_id));
180     }
181
182     static function format_map()
183     {
184         return array('id', 'mean', 'iterationCount', 'sum', 'squareSum', 'markedOutlier', 'revisions',
185             'commitTime', 'build', 'buildTime', 'buildNumber', 'builder');
186     }
187
188     private static function format_run(&$run, &$commit_time) {
189         $commit_time = intval($run['revision_time']);
190         $build_time = Database::to_js_time($run['build_time']);
191         if (!$commit_time)
192             $commit_time = $build_time;
193         return array(
194             intval($run['run_id']),
195             floatval($run['run_mean_cache']),
196             intval($run['run_iteration_count_cache']),
197             floatval($run['run_sum_cache']),
198             floatval($run['run_square_sum_cache']),
199             Database::is_true($run['run_marked_outlier']),
200             self::parse_revisions_array($run['revisions']),
201             $commit_time,
202             intval($run['build_id']),
203             $build_time,
204             $run['build_number'],
205             intval($run['build_builder']));
206     }
207
208     private static function parse_revisions_array(&$postgres_array) {
209         // e.g. {"(<commit-id>,<repository-id>,<revision>,\"2012-10-16 14:53:00\")","(<commit-id>,<repository-id>,<revision>,)"}
210         $outer_array = json_decode('[' . trim($postgres_array, '{}') . ']');
211         $revisions = array();
212         foreach ($outer_array as $item) {
213             $name_and_revision = explode(',', trim($item, '()'));
214             if (!$name_and_revision[0])
215                 continue;
216             $commit_id = intval(trim($name_and_revision[0], '"'));
217             $repository_id = intval(trim($name_and_revision[1], '"'));
218             $revision = trim($name_and_revision[2], '"');
219             $time = intval(trim($name_and_revision[3], '"'));
220             array_push($revisions, array($commit_id, $repository_id, $revision, $time));
221         }
222         return $revisions;
223     }
224 }
225
226 class AnalysisResultsFetcher {
227
228     function __construct($db, $task_id) {
229         $this->db = $db;
230         $this->task_id = $task_id;
231         $this->build_to_commits = array();
232     }
233
234     function fetch()
235     {
236         $start_time = microtime(TRUE);
237
238         // Fetch commmits separately from test_runs since number of builds is much smaller than number of runs here.
239         $this->fetch_commits();
240
241         $query = $this->db->query('SELECT test_runs.*, builds.*, test_configurations.*
242             FROM builds,
243                 test_runs JOIN test_configurations ON run_config = config_id,
244                 build_requests JOIN analysis_test_groups ON request_group = testgroup_id
245             WHERE run_build = build_id AND build_id = request_build
246                 AND testgroup_task = $1 AND run_config = config_id', array($this->task_id));
247
248         $results = array();
249         while ($row = $this->db->fetch_next_row($query))
250             array_push($results, $this->format_measurement($row));
251
252         return array(
253             'formatMap' => self::format_map(),
254             'measurements' => $results,
255             'elapsedTime' => (microtime(TRUE) - $start_time) * 1000);
256     }
257
258     function fetch_commits()
259     {
260         $query = $this->db->query('SELECT commit_id, commit_build, commit_repository, commit_revision, commit_time
261             FROM commits, build_commits, build_requests, analysis_test_groups
262             WHERE commit_id = build_commit AND commit_build = request_build
263                 AND request_group = testgroup_id AND testgroup_task = $1', array($this->task_id));
264         while ($row = $this->db->fetch_next_row($query)) {
265             $commit_time = Database::to_js_time($row['commit_time']);
266             array_push(array_ensure_item_has_array($this->build_to_commits, $row['commit_build']),
267                 array($row['commit_id'], $row['commit_repository'], $row['commit_revision'], $commit_time));
268         }
269     }
270
271     function format_measurement($row)
272     {
273         $build_id = $row['build_id'];
274         return array(
275             intval($row['run_id']),
276             floatval($row['run_mean_cache']),
277             intval($row['run_iteration_count_cache']),
278             floatval($row['run_sum_cache']),
279             floatval($row['run_square_sum_cache']),
280             $this->build_to_commits[$build_id],
281             intval($build_id),
282             Database::to_js_time($row['build_time']),
283             $row['build_number'],
284             intval($row['build_builder']),
285             intval($row['config_metric']),
286             $row['config_type']);
287     }
288
289     static function format_map()
290     {
291         return array('id', 'mean', 'iterationCount', 'sum', 'squareSum', 'revisions',
292             'build', 'buildTime', 'buildNumber', 'builder', 'metric', 'configType');
293     }
294 }
295
296 main();
297
298 ?>