v.js 12.6 KB
Newer Older
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
// Copyright 2011 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


/**
 * This function provides requestAnimationFrame in a cross browser way.
 * http://paulirish.com/2011/requestanimationframe-for-smart-animating/
 */
if ( !window.requestAnimationFrame ) {
  window.requestAnimationFrame = ( function() {
    return window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback, element) {
          window.setTimeout( callback, 1000 / 60 );
        };
  } )();
}

var kNPoints = 8000;
var kNModifications = 20;
var kNVisiblePoints = 200;
var kDecaySpeed = 20;

var kPointRadius = 4;
var kInitialLifeForce = 100;

var livePoints = void 0;
var dyingPoints = void 0;
var scene = void 0;
var renderingStartTime = void 0;
var scene = void 0;
var pausePlot = void 0;
var splayTree = void 0;
60 61 62 63
var numberOfFrames = 0;
var sumOfSquaredPauses = 0;
var benchmarkStartTime = void 0;
var benchmarkTimeLimit = void 0;
64
var autoScale = void 0;
65
var pauseDistribution = [];
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196


function Point(x, y, z, payload) {
  this.x = x;
  this.y = y;
  this.z = z;

  this.next = null;
  this.prev = null;
  this.payload = payload;
  this.lifeForce = kInitialLifeForce;
}


Point.prototype.color = function () {
  return "rgba(0, 0, 0, " + (this.lifeForce / kInitialLifeForce) + ")";
};


Point.prototype.decay = function () {
  this.lifeForce -= kDecaySpeed;
  return this.lifeForce <= 0;
};


function PointsList() {
  this.head = null;
  this.count = 0;
}


PointsList.prototype.add = function (point) {
  if (this.head !== null) this.head.prev = point;
  point.next = this.head;
  this.head = point;
  this.count++;
}


PointsList.prototype.remove = function (point) {
  if (point.next !== null) {
    point.next.prev = point.prev;
  }
  if (point.prev !== null) {
    point.prev.next = point.next;
  } else {
    this.head = point.next;
  }
  this.count--;
}


function GeneratePayloadTree(depth, tag) {
  if (depth == 0) {
    return {
      array  : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
      string : 'String for key ' + tag + ' in leaf node'
    };
  } else {
    return {
      left:  GeneratePayloadTree(depth - 1, tag),
      right: GeneratePayloadTree(depth - 1, tag)
    };
  }
}


// To make the benchmark results predictable, we replace Math.random
// with a 100% deterministic alternative.
Math.random = (function() {
  var seed = 49734321;
  return function() {
    // Robert Jenkins' 32 bit integer hash function.
    seed = ((seed + 0x7ed55d16) + (seed << 12))  & 0xffffffff;
    seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff;
    seed = ((seed + 0x165667b1) + (seed << 5))   & 0xffffffff;
    seed = ((seed + 0xd3a2646c) ^ (seed << 9))   & 0xffffffff;
    seed = ((seed + 0xfd7046c5) + (seed << 3))   & 0xffffffff;
    seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff;
    return (seed & 0xfffffff) / 0x10000000;
  };
})();


function GenerateKey() {
  // The benchmark framework guarantees that Math.random is
  // deterministic; see base.js.
  return Math.random();
}

function CreateNewPoint() {
  // Insert new node with a unique key.
  var key;
  do { key = GenerateKey(); } while (splayTree.find(key) != null);

  var point = new Point(Math.random() * 40 - 20,
                        Math.random() * 40 - 20,
                        Math.random() * 40 - 20,
                        GeneratePayloadTree(5, "" + key));

  livePoints.add(point);

  splayTree.insert(key, point);
  return key;
}

function ModifyPointsSet() {
  if (livePoints.count < kNPoints) {
    for (var i = 0; i < kNModifications; i++) {
      CreateNewPoint();
    }
  } else if (kNModifications === 20) {
    kNModifications = 80;
    kDecay = 30;
  }

  for (var i = 0; i < kNModifications; i++) {
    var key = CreateNewPoint();
    var greatest = splayTree.findGreatestLessThan(key);
    if (greatest == null) {
      var point = splayTree.remove(key).value;
    } else {
      var point = splayTree.remove(greatest.key).value;
    }
    livePoints.remove(point);
    point.payload = null;
    dyingPoints.add(point);
  }
}


197
function PausePlot(width, height, size, scale) {
198 199 200 201 202 203 204
  var canvas = document.createElement("canvas");
  canvas.width = this.width = width;
  canvas.height = this.height = height;
  document.body.appendChild(canvas);

  this.ctx = canvas.getContext('2d');

205 206 207 208 209 210 211 212
  if (typeof scale !== "number") {
    this.autoScale = true;
    this.maxPause = 0;
  } else {
    this.autoScale = false;
    this.maxPause = scale;
  }

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
  this.size = size;

  // Initialize cyclic buffer for pauses.
  this.pauses = new Array(this.size);
  this.start = this.size;
  this.idx = 0;
}


PausePlot.prototype.addPause = function (p) {
  if (this.idx === this.size) {
    this.idx = 0;
  }

  if (this.idx === this.start) {
    this.start++;
  }

  if (this.start === this.size) {
    this.start = 0;
  }

  this.pauses[this.idx++] = p;
};


PausePlot.prototype.iteratePauses = function (f) {
  if (this.start < this.idx) {
    for (var i = this.start; i < this.idx; i++) {
      f.call(this, i - this.start, this.pauses[i]);
    }
  } else {
    for (var i = this.start; i < this.size; i++) {
      f.call(this, i - this.start, this.pauses[i]);
    }

    var offs = this.size - this.start;
    for (var i = 0; i < this.idx; i++) {
      f.call(this, i + offs, this.pauses[i]);
    }
  }
};


PausePlot.prototype.draw = function () {
  var first = null;
259 260 261 262 263 264 265 266 267

  if (this.autoScale) {
    this.iteratePauses(function (i, v) {
      if (first === null) {
        first = v;
      }
      this.maxPause = Math.max(v, this.maxPause);
    });
  }
268 269 270 271 272

  var dx = this.width / this.size;
  var dy = this.height / this.maxPause;

  this.ctx.save();
273
  this.ctx.clearRect(0, 0, this.width, this.height);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
  this.ctx.beginPath();
  this.ctx.moveTo(1, dy * this.pauses[this.start]);
  var p = first;
  this.iteratePauses(function (i, v) {
    var delta = v - p;
    var x = 1 + dx * i;
    var y = dy * v;
    this.ctx.lineTo(x, y);
    if (delta > 2 * (p / 3)) {
      this.ctx.font = "bold 12px sans-serif";
      this.ctx.textBaseline = "bottom";
      this.ctx.fillText(v + "ms", x + 2, y);
    }
    p = v;
  });
  this.ctx.strokeStyle = "black";
  this.ctx.stroke();
  this.ctx.restore();
}


function Scene(width, height) {
  var canvas = document.createElement("canvas");
  canvas.width = width;
  canvas.height = height;
  document.body.appendChild(canvas);

  this.ctx = canvas.getContext('2d');
  this.width = canvas.width;
  this.height = canvas.height;

  // Projection configuration.
  this.x0 = canvas.width / 2;
  this.y0 = canvas.height / 2;
  this.z0 = 100;
  this.f  = 1000;  // Focal length.

  // Camera is rotating around y-axis.
  this.angle = 0;
}


Scene.prototype.drawPoint = function (x, y, z, color) {
  // Rotate the camera around y-axis.
  var rx = x * Math.cos(this.angle) - z * Math.sin(this.angle);
  var ry = y;
  var rz = x * Math.sin(this.angle) + z * Math.cos(this.angle);

  // Perform perspective projection.
  var px = (this.f * rx) / (rz - this.z0) + this.x0;
  var py = (this.f * ry) / (rz - this.z0) + this.y0;

  this.ctx.save();
  this.ctx.fillStyle = color
  this.ctx.beginPath();
  this.ctx.arc(px, py, kPointRadius, 0, 2 * Math.PI, true);
  this.ctx.fill();
  this.ctx.restore();
};


Scene.prototype.drawDyingPoints = function () {
  var point_next = null;
  for (var point = dyingPoints.head; point !== null; point = point_next) {
    // Rotate the scene around y-axis.
    scene.drawPoint(point.x, point.y, point.z, point.color());

    point_next = point.next;

    // Decay the current point and remove it from the list
    // if it's life-force ran out.
    if (point.decay()) {
      dyingPoints.remove(point);
    }
  }
};


Scene.prototype.draw = function () {
  this.ctx.save();
  this.ctx.clearRect(0, 0, this.width, this.height);
  this.drawDyingPoints();
  this.ctx.restore();

  this.angle += Math.PI / 90.0;
};


362 363 364 365 366
function updateStats(pause) {
  numberOfFrames++;
  if (pause > 20) {
    sumOfSquaredPauses += (pause - 20) * (pause - 20);
  }
367 368
  pauseDistribution[Math.floor(pause / 10)] |= 0;
  pauseDistribution[Math.floor(pause / 10)]++;
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
}


function renderStats() {
  var msg = document.createElement("p");
  msg.innerHTML = "Score " +
    Math.round(numberOfFrames * 1000 / sumOfSquaredPauses);
  var table = document.createElement("table");
  table.align = "center";
  for (var i = 0; i < pauseDistribution.length; i++) {
    if (pauseDistribution[i] > 0) {
      var row = document.createElement("tr");
      var time = document.createElement("td");
      var count = document.createElement("td");
      time.innerHTML = i*10 + "-" + (i+1)*10 + "ms";
      count.innerHTML = " => " + pauseDistribution[i];
      row.appendChild(time);
      row.appendChild(count);
      table.appendChild(row);
    }
  }
  div.appendChild(msg);
  div.appendChild(table);
}


395 396 397
function render() {
  if (typeof renderingStartTime === 'undefined') {
    renderingStartTime = Date.now();
398
    benchmarkStartTime = renderingStartTime;
399 400 401 402 403 404 405
  }

  ModifyPointsSet();

  scene.draw();

  var renderingEndTime = Date.now();
406 407
  var pause = renderingEndTime - renderingStartTime;
  pausePlot.addPause(pause);
408 409 410 411
  renderingStartTime = renderingEndTime;

  pausePlot.draw();

412 413
  updateStats(pause);

414 415
  div.innerHTML =
      livePoints.count + "/" + dyingPoints.count + " " +
416 417 418 419 420 421 422 423 424 425
      pause + "(max = " + pausePlot.maxPause + ") ms " +
      numberOfFrames + " frames";

  if (renderingEndTime < benchmarkStartTime + benchmarkTimeLimit) {
    // Schedule next frame.
    requestAnimationFrame(render);
  } else {
    renderStats();
  }
}
426

427

428 429 430
function Form() {
  function create(tag) { return document.createElement(tag); }
  function text(value) { return document.createTextNode(value); }
431

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
  this.form = create("form");
  this.form.setAttribute("action", "javascript:start()");

  var table = create("table");
  table.setAttribute("style", "margin-left: auto; margin-right: auto;");

  function col(a) {
    var td = create("td");
    td.appendChild(a);
    return td;
  }

  function row(a, b) {
    var tr = create("tr");
    tr.appendChild(col(a));
    tr.appendChild(col(b));
448
    return tr;
449 450 451 452 453 454 455 456 457 458 459 460 461
  }

  this.timelimit = create("input");
  this.timelimit.setAttribute("value", "60");

  table.appendChild(row(text("Time limit in seconds"), this.timelimit));

  this.autoscale = create("input");
  this.autoscale.setAttribute("type", "checkbox");
  this.autoscale.setAttribute("checked", "true");
  table.appendChild(row(text("Autoscale pauses plot"), this.autoscale));

  var button = create("input");
462 463
  button.setAttribute("type", "submit");
  button.setAttribute("value", "Start");
464 465 466 467
  this.form.appendChild(table);
  this.form.appendChild(button);

  document.body.appendChild(this.form);
468 469 470
}


471 472 473 474 475
Form.prototype.remove = function () {
  document.body.removeChild(this.form);
};


476 477 478 479 480 481 482 483 484 485 486
function init() {
  livePoints = new PointsList;
  dyingPoints = new PointsList;

  splayTree = new SplayTree();

  scene = new Scene(640, 480);

  div = document.createElement("div");
  document.body.appendChild(div);

487
  pausePlot = new PausePlot(480, autoScale ? 240 : 500, 160, autoScale ? void 0 : 500);
488 489
}

490
function start() {
491 492 493
  benchmarkTimeLimit = form.timelimit.value * 1000;
  autoScale = form.autoscale.checked;
  form.remove();
494 495 496
  init();
  render();
}
497

498
var form = new Form();