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