monitor.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var Monitoring = Monitoring || (function() {
  2. var stats = new MemoryStats();
  3. stats.domElement.style.position = 'fixed';
  4. stats.domElement.style.right = '0px';
  5. stats.domElement.style.bottom = '0px';
  6. document.body.appendChild( stats.domElement );
  7. requestAnimationFrame(function rAFloop(){
  8. stats.update();
  9. requestAnimationFrame(rAFloop);
  10. });
  11. var RenderRate = function () {
  12. var container = document.createElement( 'div' );
  13. container.id = 'stats';
  14. container.style.cssText = 'width:150px;opacity:0.9;cursor:pointer;position:fixed;right:80px;bottom:0px;';
  15. var msDiv = document.createElement( 'div' );
  16. msDiv.id = 'ms';
  17. msDiv.style.cssText = 'padding:0 0 3px 3px;text-align:left;background-color:#020;';
  18. container.appendChild( msDiv );
  19. var msText = document.createElement( 'div' );
  20. msText.id = 'msText';
  21. msText.style.cssText = 'color:#0f0;font-family:Helvetica,Arial,sans-serif;font-size:9px;font-weight:bold;line-height:15px';
  22. msText.innerHTML= 'Repaint rate: 0/sec';
  23. msDiv.appendChild( msText );
  24. var bucketSize = 20;
  25. var bucket = [];
  26. var lastTime = Date.now();
  27. return {
  28. domElement: container,
  29. ping: function () {
  30. var start = lastTime;
  31. var stop = Date.now();
  32. var rate = 1000 / (stop - start);
  33. bucket.push(rate);
  34. if (bucket.length > bucketSize) {
  35. bucket.shift();
  36. }
  37. var sum = 0;
  38. for (var i = 0; i < bucket.length; i++) {
  39. sum = sum + bucket[i];
  40. }
  41. msText.textContent = "Repaint rate: " + (sum / bucket.length).toFixed(2) + "/sec";
  42. lastTime = stop;
  43. }
  44. }
  45. };
  46. var renderRate = new RenderRate();
  47. document.body.appendChild( renderRate.domElement );
  48. return {
  49. memoryStats: stats,
  50. renderRate: renderRate
  51. };
  52. })();