<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[192691] trunk/Websites/perf.webkit.org</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/192691">192691</a></dd>
<dt>Author</dt> <dd>rniwa@webkit.org</dd>
<dt>Date</dt> <dd>2015-11-20 12:39:34 -0800 (Fri, 20 Nov 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Perf dashboard's runs API uses more than 128MB of memory
https://bugs.webkit.org/show_bug.cgi?id=151478

Reviewed by Andreas Kling.

Don't fetch all query results at once to avoid using twice as much memory as needed.
Use iterative API to format each result at a time.

This change is also a 5% runtime performance gain.

* public/api/runs.php:
(RunsGenerator::__construct): Takes a Database instance instead of a list of configurations. The latter is
no longer needed as we pass in each configuration type explicitly to fetch_runs.
(RunsGenerator::fetch_runs): Renamed from add_runs since it now executes the database query via execute_query.
Also moved the logic to compute the last modified time here.
(RunsGenerator::execute_query): Moved from fetch_runs_for_config. Use Database::query instead of query_and_fetch_all.
(RunsGeneratorForTestGroup):
(RunsGeneratorForTestGroup::__construct):
(RunsGeneratorForTestGroup::execute_query): Moved from fetch_runs_for_config_and_test_group.

* public/include/db.php:
(generate_data_file): Lock the file to avoid corruption.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkWebsitesperfwebkitorgChangeLog">trunk/Websites/perf.webkit.org/ChangeLog</a></li>
<li><a href="#trunkWebsitesperfwebkitorgpublicapirunsphp">trunk/Websites/perf.webkit.org/public/api/runs.php</a></li>
<li><a href="#trunkWebsitesperfwebkitorgpublicincludedbphp">trunk/Websites/perf.webkit.org/public/include/db.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkWebsitesperfwebkitorgChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Websites/perf.webkit.org/ChangeLog (192690 => 192691)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Websites/perf.webkit.org/ChangeLog        2015-11-20 20:35:18 UTC (rev 192690)
+++ trunk/Websites/perf.webkit.org/ChangeLog        2015-11-20 20:39:34 UTC (rev 192691)
</span><span class="lines">@@ -1,3 +1,28 @@
</span><ins>+2015-11-20  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
+
+        Perf dashboard's runs API uses more than 128MB of memory
+        https://bugs.webkit.org/show_bug.cgi?id=151478
+
+        Reviewed by Andreas Kling.
+
+        Don't fetch all query results at once to avoid using twice as much memory as needed.
+        Use iterative API to format each result at a time.
+
+        This change is also a 5% runtime performance gain.
+
+        * public/api/runs.php:
+        (RunsGenerator::__construct): Takes a Database instance instead of a list of configurations. The latter is
+        no longer needed as we pass in each configuration type explicitly to fetch_runs.
+        (RunsGenerator::fetch_runs): Renamed from add_runs since it now executes the database query via execute_query.
+        Also moved the logic to compute the last modified time here.
+        (RunsGenerator::execute_query): Moved from fetch_runs_for_config. Use Database::query instead of query_and_fetch_all.
+        (RunsGeneratorForTestGroup):
+        (RunsGeneratorForTestGroup::__construct):
+        (RunsGeneratorForTestGroup::execute_query): Moved from fetch_runs_for_config_and_test_group.
+
+        * public/include/db.php:
+        (generate_data_file): Lock the file to avoid corruption.
+
</ins><span class="cx"> 2015-11-19  Ryosuke Niwa  &lt;rniwa@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Perf dashboard always fetches charts JSON twice
</span></span></pre></div>
<a id="trunkWebsitesperfwebkitorgpublicapirunsphp"></a>
<div class="modfile"><h4>Modified: trunk/Websites/perf.webkit.org/public/api/runs.php (192690 => 192691)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Websites/perf.webkit.org/public/api/runs.php        2015-11-20 20:35:18 UTC (rev 192690)
+++ trunk/Websites/perf.webkit.org/public/api/runs.php        2015-11-20 20:39:34 UTC (rev 192691)
</span><span class="lines">@@ -2,27 +2,6 @@
</span><span class="cx"> 
</span><span class="cx"> require('../include/json-header.php');
</span><span class="cx"> 
</span><del>-function fetch_runs_for_config_and_test_group($db, $config, $test_group_id) {
-    return $db-&gt;query_and_fetch_all('
-        SELECT test_runs.*, builds.*, array_agg((commit_repository, commit_revision, commit_time)) AS revisions
-            FROM builds
-                LEFT OUTER JOIN build_commits ON commit_build = build_id
-                LEFT OUTER JOIN commits ON build_commit = commit_id,
-                test_runs, build_requests, analysis_test_groups
-            WHERE run_build = build_id AND run_config = $1 AND request_build = build_id AND request_group = $2
-            GROUP BY build_id, run_id', array($config['config_id'], $test_group_id));
-}
-
-function fetch_runs_for_config($db, $config) {
-    return $db-&gt;query_and_fetch_all('
-        SELECT test_runs.*, builds.*, array_agg((commit_repository, commit_revision, commit_time)) AS revisions
-            FROM builds
-                LEFT OUTER JOIN build_commits ON commit_build = build_id
-                LEFT OUTER JOIN commits ON build_commit = commit_id, test_runs
-            WHERE run_build = build_id AND run_config = $1 AND NOT EXISTS (SELECT * FROM build_requests WHERE request_build = build_id)
-            GROUP BY build_id, run_id', array($config['config_id']));
-}
-
</del><span class="cx"> function main($path) {
</span><span class="cx">     if (count($path) != 1)
</span><span class="cx">         exit_with_error('InvalidRequest');
</span><span class="lines">@@ -52,15 +31,13 @@
</span><span class="cx">         header(&quot;Cache-Control: maxage=$maxage&quot;);
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    $generator = new RunsGenerator($config_rows);
</del><ins>+    if ($test_group_id)
+        $generator = new RunsGeneratorForTestGroup($db, $test_group_id);
+    else
+        $generator = new RunsGenerator($db);
</ins><span class="cx"> 
</span><del>-    foreach ($config_rows as $config) {
-        if ($test_group_id)
-            $raw_runs = fetch_runs_for_config_and_test_group($db, $config, $test_group_id);
-        else
-            $raw_runs = fetch_runs_for_config($db, $config);
-        $generator-&gt;add_runs($config['config_type'], $raw_runs);
-    }
</del><ins>+    foreach ($config_rows as $config)
+        $generator-&gt;fetch_runs($config['config_type'], $config['config_id'], $config['config_runs_last_modified']);
</ins><span class="cx"> 
</span><span class="cx">     $content = success_json($generator-&gt;results());
</span><span class="cx">     if (!$test_group_id)
</span><span class="lines">@@ -69,12 +46,10 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> class RunsGenerator {
</span><del>-    function __construct($config_rows) {
</del><ins>+    function __construct($db) {
+        $this-&gt;db = $db;
</ins><span class="cx">         $this-&gt;results = array();
</span><del>-        $last_modified_times = array();
-        foreach ($config_rows as $row)
-            array_push($last_modified_times, Database::to_js_time($row['config_runs_last_modified']));
-        $this-&gt;last_modified = max($last_modified_times);
</del><ins>+        $this-&gt;last_modified = 0;
</ins><span class="cx">         $this-&gt;start_time = microtime(true);
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="lines">@@ -85,16 +60,28 @@
</span><span class="cx">             'elapsedTime' =&gt; (microtime(true) - $this-&gt;start_time) * 1000);
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    function add_runs($name, $raw_runs) {
</del><ins>+    function fetch_runs($name, $config_id, $last_modified) {
+        $this-&gt;last_modified = max($this-&gt;last_modified, Database::to_js_time($last_modified));
+
+        $results = $this-&gt;execute_query($config_id);
+
</ins><span class="cx">         $formatted_runs = array();
</span><del>-        if ($raw_runs) {
-            foreach ($raw_runs as $run)
-                array_push($formatted_runs, self::format_run($run));
-        }
</del><ins>+        while ($row = $this-&gt;db-&gt;fetch_next_row($results))
+            array_push($formatted_runs, self::format_run($row));
+
</ins><span class="cx">         $this-&gt;results[$name] = $formatted_runs;
</span><del>-        return $formatted_runs;
</del><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    function execute_query($config_id) {
+        return $this-&gt;db-&gt;query('
+            SELECT test_runs.*, builds.*, array_agg((commit_repository, commit_revision, commit_time)) AS revisions
+                FROM builds
+                    LEFT OUTER JOIN build_commits ON commit_build = build_id
+                    LEFT OUTER JOIN commits ON build_commit = commit_id, test_runs
+                WHERE run_build = build_id AND run_config = $1 AND NOT EXISTS (SELECT * FROM build_requests WHERE request_build = build_id)
+                GROUP BY build_id, run_id', array($config_id));
+    }
+
</ins><span class="cx">     private static function format_run($run) {
</span><span class="cx">         return array(
</span><span class="cx">             'id' =&gt; intval($run['run_id']),
</span><span class="lines">@@ -125,6 +112,24 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+class RunsGeneratorForTestGroup extends RunsGenerator {
+    function __construct($db, $test_group_id) {
+        parent::__construct($db);
+        $this-&gt;test_group_id = $test_group_id;
+    }
+
+    function execute_query($config_id) {
+        return $this-&gt;db-&gt;query('
+            SELECT test_runs.*, builds.*, array_agg((commit_repository, commit_revision, commit_time)) AS revisions
+                FROM builds
+                    LEFT OUTER JOIN build_commits ON commit_build = build_id
+                    LEFT OUTER JOIN commits ON build_commit = commit_id,
+                    test_runs, build_requests, analysis_test_groups
+                WHERE run_build = build_id AND run_config = $1 AND request_build = build_id AND request_group = $2
+                GROUP BY build_id, run_id', array($config_id, $this-&gt;test_group_id));
+    }
+}
+
</ins><span class="cx"> main(array_key_exists('PATH_INFO', $_SERVER) ? explode('/', trim($_SERVER['PATH_INFO'], '/')) : array());
</span><span class="cx"> 
</span><span class="cx"> ?&gt;
</span></span></pre></div>
<a id="trunkWebsitesperfwebkitorgpublicincludedbphp"></a>
<div class="modfile"><h4>Modified: trunk/Websites/perf.webkit.org/public/include/db.php (192690 => 192691)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Websites/perf.webkit.org/public/include/db.php        2015-11-20 20:35:18 UTC (rev 192690)
+++ trunk/Websites/perf.webkit.org/public/include/db.php        2015-11-20 20:39:34 UTC (rev 192691)
</span><span class="lines">@@ -43,7 +43,7 @@
</span><span class="cx"> function generate_data_file($filename, $content) {
</span><span class="cx">     if (!assert(ctype_alnum(str_replace(array('-', '_', '.'), '', $filename))))
</span><span class="cx">         return FALSE;
</span><del>-    return file_put_contents(config_path('dataDirectory', $filename), $content);
</del><ins>+    return file_put_contents(config_path('dataDirectory', $filename), $content, LOCK_EX);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> if (config('debug')) {
</span></span></pre>
</div>
</div>

</body>
</html>