Skip to content
This repository
Browse code

Results are now persisted and can be viewed

  • Loading branch information...
commit 94a1b8a8f428a92eae638b57ac4aea857445a2dd 1 parent 721d111
nincode authored June 24, 2011
12  engine/core/browser.js
@@ -35,8 +35,13 @@ var Browser = (function() {
35 35
     }
36 36
 
37 37
     function detect() {
38  
-      var user_agent = navigator.userAgent.toLowerCase();
  38
+      var user_agent = navigator.userAgent;
  39
+      Browser.browser_version = detectFromUA(user_agent);
  40
+      console.log('browser_version ' + Browser.browser_version);
  41
+    }
39 42
 
  43
+    function detectFromUA(user_agent) {
  44
+      user_agent = user_agent.toLowerCase();
40 45
       if (/chrome/.test(user_agent)) {
41 46
         Browser.browser = Browser.CHROME;
42 47
         Browser.threeD = true;
@@ -147,8 +152,8 @@ var Browser = (function() {
147 152
           }
148 153
           break;
149 154
       }
150  
-      Browser.browser_version = Browser.os + ' ' + Browser.browser_version;
151  
-      console.log('browser_version ' + Browser.browser_version);
  155
+      var retVal = Browser.os + ' ' + Browser.browser_version;
  156
+      return retVal;
152 157
     }
153 158
 
154 159
     function getWindowSize() {
@@ -176,5 +181,6 @@ var Browser = (function() {
176 181
 
177 182
     Browser.getWindowSize = getWindowSize;
178 183
     Browser.detect = detect;
  184
+    Browser.detectFromUA = detectFromUA;
179 185
     return Browser;
180 186
   })();
107  lib/benchmark.js
... ...
@@ -0,0 +1,107 @@
  1
+// Copyright 2004-present Facebook. All Rights Reserved.
  2
+
  3
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
  4
+// not use this file except in compliance with the License. You may obtain
  5
+// a copy of the License at
  6
+
  7
+//     http://www.apache.org/licenses/LICENSE-2.0
  8
+
  9
+// Unless required by applicable law or agreed to in writing, software
  10
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12
+// License for the specific language governing permissions and limitations
  13
+// under the License.
  14
+
  15
+
  16
+var benchmark_results = new Array();
  17
+var benchmark_file_name = 'benchmark_results.json';
  18
+var results_to_keep = 10000;
  19
+var results_to_return = 20;
  20
+
  21
+function benchmarkResult(time, result, browser, testData) {
  22
+  this.time = time;
  23
+  this.result = result;
  24
+  this.browser = browser;
  25
+  this.testData = testData;
  26
+}
  27
+
  28
+function resultSorterByTopScore(a, b) {
  29
+  return b.result - a.result;
  30
+}
  31
+
  32
+function resultSorterByNewest(a, b) {
  33
+  return b.time - a.time;
  34
+}
  35
+
  36
+function processResult(result, browser, testData) {
  37
+  var time = new Date();
  38
+  var br = new benchmarkResult(time.valueOf(), result, browser, testData);
  39
+  benchmark_results.push(br);
  40
+  benchmark_results.sort(resultSorterByNewest);
  41
+  if (benchmark_results.length > results_to_keep) {
  42
+    benchmark_results.splice(results_to_keep, benchmark_results.length);
  43
+  }
  44
+}
  45
+
  46
+function saveResults() {
  47
+  fs.writeFileSync(benchmark_file_name, JSON.stringify(benchmark_results));
  48
+  console.log('BenchmarkResult - saved '+benchmark_results.length+' results');
  49
+}
  50
+
  51
+function loadResults() {
  52
+  var data;
  53
+  try {
  54
+    data = fs.readFileSync(benchmark_file_name);
  55
+  } catch(err) {
  56
+    return;
  57
+  }
  58
+  if (data.length > 0) {
  59
+    benchmark_results = JSON.parse(data);
  60
+  }
  61
+  console.log('BenchmarkResult - loaded '+benchmark_results.length+' results');
  62
+}
  63
+
  64
+function init() {
  65
+  if (benchmark_results.length == 0)
  66
+    loadResults();
  67
+}
  68
+
  69
+function getTopResults()
  70
+{
  71
+  var ret = benchmark_results;
  72
+  ret.sort(resultSorterByTopScore);
  73
+  //ret.splice(results_to_return, ret.length);
  74
+
  75
+  // Convert results into something the client can use - strip out extra
  76
+  // headers bits, etc.
  77
+  var results = new Array();
  78
+  var total_results = Math.min(results_to_return, ret.length);
  79
+  for (var i=0; i<total_results; i++) {
  80
+    results[i] = {};
  81
+    if ("user-agent" in ret[i].browser) {
  82
+      results[i].browser = ret[i].browser["user-agent"];
  83
+    } else {
  84
+      results[i].browser = ret[i].browser;
  85
+    }
  86
+    results[i].time = ret[i].time;
  87
+    results[i].score = ret[i].result;
  88
+  }
  89
+  return results;
  90
+}
  91
+
  92
+function testResults() {
  93
+  return;
  94
+  if (benchmark_results.length>5)
  95
+    return;
  96
+  processResult(Math.floor(Math.random()*50), "Chrome", "relja");
  97
+  processResult(Math.floor(Math.random()*50), "Chrome", "relja");
  98
+  processResult(Math.floor(Math.random()*50), "Chrome <script>foo!<script>i &bla=noway", "relja");
  99
+  processResult(Math.floor(Math.random()*50), "Chrome &bla=bla", "relja");
  100
+  processResult(Math.floor(Math.random()*50), "IE10", "relja");
  101
+}
  102
+
  103
+exports.init = init;
  104
+exports.test = testResults;
  105
+exports.save = saveResults;
  106
+exports.getTopResults = getTopResults;
  107
+exports.processResult = processResult;
31  lib/server.js
@@ -12,6 +12,8 @@
12 12
 // License for the specific language governing permissions and limitations
13 13
 // under the License.
14 14
 
  15
+BenchmarkResults = require('../lib/benchmark.js');
  16
+
15 17
 var tick_count = 0;
16 18
 var tick_fps = 50;
17 19
 var grid_send_rate = 5; // needs to be integer multiple of tick_fps
@@ -52,6 +54,7 @@ function init() {
52 54
     }
53 55
   });
54 56
   setInterval(function() { tick(); }, 1000 / tick_fps);
  57
+  BenchmarkResults.init();
55 58
 }
56 59
 
57 60
 function listen(port,options,cb) {
@@ -171,23 +174,37 @@ function postHandler(request, callback) {
171 174
 };
172 175
 
173 176
 function serverCallback(req, res) {
  177
+  var parse = url.parse(req.url);
  178
+  var pathname = parse.pathname || '/';
  179
+  var split = pathname.split('/');
174 180
   if (req.method == 'POST') {
175  
-    //console.log(req.method + ': ' + req.url);
176  
-    //console.log('headers: ' + JSON.stringify(req.headers));
177  
-    //console.log('body: ' + JSON.stringify(req.body));
  181
+    console.log(req.method + ': ' + req.url);
  182
+    console.log('headers: ' + JSON.stringify(req.headers));
  183
+    console.log('body: ' + JSON.stringify(req.body));
178 184
     postHandler(req, function(data) {
179 185
       if (data.indexOf('cmd:') == 0) {
180 186
         var fb_info = Graph.fbinfo_from_cookie(req.headers.cookie);
181 187
         console.log(JSON.stringify(fb_info));
182 188
         Graph.handler(fb_info,res,data.substring(5));
183 189
       } else {
184  
-        console.log(data);
  190
+        var result = JSON.parse(data);
  191
+        BenchmarkResults.processResult(result.score, req.headers, result.testName);
  192
+        res.writeHead(200, {'Content-Type': 'text/html'});
  193
+        res.end();
  194
+        BenchmarkResults.save();
  195
+        return;
185 196
       }
186 197
     });
  198
+  } else {
  199
+    if (split[1] === 'benchmark_results') {
  200
+      res.writeHead(200, {'Content-Type': 'text/html'});
  201
+      var returnData = JSON.stringify(BenchmarkResults.getTopResults());
  202
+      res.write(returnData);
  203
+      res.end();
  204
+      return;
  205
+    }
187 206
   }
188  
-  var parse = url.parse(req.url);
189  
-  var pathname = parse.pathname || '/';
190  
-  var split = pathname.split('/');
  207
+
191 208
   if (0 && req.method === 'POST') {
192 209
     console.log('headers: ' + JSON.stringify(req.headers));
193 210
     switch(split[1]) {
21  perf/css/perf.css
@@ -763,3 +763,24 @@ to { -webkit-transform: translate3d(-50px,-50px,200px) rotateZ(360deg); }
763 763
 #details {
764 764
   overflow: auto;
765 765
 }
  766
+
  767
+.bar {
  768
+  background-color: #98F5FF;
  769
+  position: relative;
  770
+  height: 16px;
  771
+  margin-top: 1px;
  772
+  margin-bottom: 1px;
  773
+  -moz-box-shadow: 5px 5px 5px #008;
  774
+  -webkit-box-shadow: 5px 5px 5px #008;
  775
+  box-shadow: 5px 5px 5px #008;
  776
+}
  777
+
  778
+table.hpadding td {
  779
+  padding-right: 10px;
  780
+  text-align: left;
  781
+}
  782
+
  783
+table.hpadding th {
  784
+  padding-right: 10px;
  785
+  text-align: left;
  786
+}
1  perf/index.shtml
@@ -5,6 +5,7 @@
5 5
     <script type="text/javascript" src="/socket.io/socket.io.js"></script>
6 6
     #include '/engine/include/core.include'
7 7
     <link rel="stylesheet" type="text/css" href="/perf/css/perf.css" />
  8
+    <script type="text/javascript" src="/engine/core/xhr.js"> </script>
8 9
     <script type="text/javascript" src="/perf/benchmark.js"> </script>
9 10
     <script type="text/javascript" src="/perf/perftest.js"> </script>
10 11
     <script type="text/javascript" src="/perf/fbmark.js"> </script>
77  perf/perf.js
@@ -182,6 +182,76 @@ var Perf = (function() {
182 182
       }
183 183
     }
184 184
 
  185
+    // Score reporting
  186
+    function getBrowser() {
  187
+      var b = data.browser;
  188
+      var browser = b.match(/(\w+) \d+/);
  189
+      if (browser) {
  190
+        browser = browser[1];
  191
+      } else {
  192
+        browser = b;
  193
+      }
  194
+    }
  195
+
  196
+    function getScores() {
  197
+      var req = new XMLHttpRequest();
  198
+      req.open('GET', 'benchmark_results', true);
  199
+      req.onreadystatechange = function() {
  200
+        var result;
  201
+        var done = 4, ok = 200;
  202
+        if (req.readyState == done && req.status == ok) {
  203
+          result = JSON.parse(req.responseText);
  204
+          outputScoresAsync(result);
  205
+        }
  206
+      }
  207
+      req.send(null);
  208
+      return "Loading result history...";
  209
+    }
  210
+
  211
+    // Super hacky and likely unsafe sanitization function
  212
+    // Still looking for the best way to sanitize strings in node.js
  213
+    function sanitize(string) {
  214
+      return string.replace(/[&<>]/g, '');
  215
+    }
  216
+
  217
+    function outputScoresAsync(results) {
  218
+      var scoreTable;
  219
+      var maxResult;
  220
+
  221
+      scoreTable='<table class="hpadding">';
  222
+      scoreTable+='<tr><th style="width:200px"></th><th>Score</th><th>Browser</th><th>Date</th></tr>';
  223
+
  224
+      if (results.length>0) {
  225
+        maxResult = parseInt(results[0].score);
  226
+      }
  227
+
  228
+      for (var i=0; i<results.length; i++) {
  229
+        var date = new Date(results[i].time).toDateString();
  230
+        var score = parseInt(results[i].score);
  231
+        var barWidth = score*100/maxResult;
  232
+        scoreTable+='<tr>';
  233
+        scoreTable+='<td><div class="bar" style="width:'+barWidth+'%;"></div></td>';
  234
+        scoreTable+='<td>'+score+'</td>';
  235
+        scoreTable+='<td>'+sanitize(Browser.detectFromUA(results[i].browser))+'</td>';
  236
+        scoreTable+='<td>'+sanitize(date)+'</td>';
  237
+        scoreTable+='</tr>';
  238
+      }
  239
+      scoreTable+='</table>';
  240
+
  241
+      UI.addHTML('details', 'detailinfo', {pos: [5, 105], uiclass: 'renderdetails', markup: scoreTable});
  242
+    }
  243
+
  244
+    function showHiscores() {
  245
+      UI.del('details');
  246
+      UI.addCollection('perf', 'details', {uiclass: 'perfblock', pos: [265, 0], width: 600, height: 1000});
  247
+
  248
+      var score = 'All time high scores:';
  249
+      UI.addHTML('details', 'dperfscore', {pos: [3, 4], uiclass: 'perfscore', markup: score});
  250
+
  251
+      getScores();
  252
+    }
  253
+
  254
+
185 255
     function canvasDemo() {
186 256
       UI.del('buttons');
187 257
       UI.del('perf');
@@ -358,6 +428,9 @@ var Perf = (function() {
358 428
 //      UI.addButton('buttons', 'idemo', {pos: [530, 5], width: 95, height: 40, text: 'iPhone Demo', command: {cmd: 'idemo', args: []}});
359 429
 //      UI.addButton('buttons', 'rotdemo', {pos: [635, 5], width: 95, height: 40, text: 'Rotate Demo', command: {cmd: 'rotdemo', args: []}});
360 430
       UI.addCollection(null, 'perf', {pos: [100, 60], width: 260});
  431
+      UI.addButton('buttons', 'hiscore', {pos: [540, 5], width: 95, height: 40, text: 'Hi-Scores', command: {cmd: 'showHiscores', args: []}});
  432
+
  433
+
361 434
       if (data) {
362 435
         for (var i = 0, len = data.length; i < len; i++) {
363 436
           UI.addCollection('perf', 'perfblock' + i, {uiclass: 'perfblock', pos: [0, 82 * i], height: 78, width: 260, command: {cmd:'showdetails', args:[data[i]]}});
@@ -372,6 +445,9 @@ var Perf = (function() {
372 445
           var score = parseInt(data[i].peak);
373 446
           UI.addHTML('perfblock' + i, 'perfscore' + i, {pos: [5, 24], uiclass: 'perfscore', markup: score});
374 447
         }
  448
+
  449
+        // Display result browser
  450
+        showHiscores();
375 451
       }
376 452
     }
377 453
 
@@ -390,6 +466,7 @@ var Perf = (function() {
390 466
       ClientCmd.install('idemo', iDemo);
391 467
       ClientCmd.install('rotdemo', rotDemo);
392 468
       ClientCmd.install('scrolldemo', scrollDemo);
  469
+      ClientCmd.install('showHiscores', showHiscores);
393 470
       ClientCmd.install('playgame', playGame);
394 471
       ClientCmd.install('playgamehtml', playGameHTML);
395 472
       ClientCmd.install('showdetails', showDetails);
10  perf/perftest.js
@@ -368,6 +368,15 @@ var PerfTest = (function() {
368 368
 
369 369
     var current = 0;
370 370
 
  371
+    PerfTest.postToServer = function(score, testName) {
  372
+      var req = new XMLHttpRequest();
  373
+      req.open('POST', 'benchmark_results', false);
  374
+      var result={};
  375
+      result.score=score;
  376
+      result.testName=testName;
  377
+      req.send(JSON.stringify(result));
  378
+    }
  379
+
371 380
     PerfTest.init = function() {
372 381
       UI.del('buttons');
373 382
       UI.del('perf');
@@ -441,6 +450,7 @@ var PerfTest = (function() {
441 450
         temp.peak = result.score;
442 451
         FBmark.reset();
443 452
         PerfTest.stop();
  453
+        PerfTest.postToServer(result.score, [temp]);
444 454
         ClientCmd.perfdisplay([temp]);
445 455
       }
446 456
     }

0 notes on commit 94a1b8a

Please sign in to comment.
Something went wrong with that request. Please try again.