stats.js使用性能监控源码分析

前端开发   发布日期:2025年04月03日   浏览次数:145

这篇文章主要介绍“stats.js使用性能监控源码分析”,在日常操作中,相信很多人在stats.js使用性能监控源码分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”stats.js使用性能监控源码分析”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

1.性能监控

github 地址:

  1. https://github.com/mrdoob/stats.js/blob/master/src/Stats.js
  • FPS 在最近一秒渲染的帧数量。数值越高,性能越好.

  • MS 渲染帧所需的毫秒数。数值越低,性能越好.

  • MB 占用的内存大小. (Chrome 浏览器快捷键后面添加--enable-precise-memory-info 命令)

2.fps 计算

  1. var fps = 0;
  2. var prevTime = (performance || Date).now(),
  3. frames = 0;
  4. function aaa() {
  5. frames++;
  6. var time = (performance || Date).now();
  7. //每秒计算一次渲染帧数量
  8. if (time >= prevTime + 1000) {
  9. fps = (frames * 1000) / (time - prevTime);
  10. console.log(fps);
  11. prevTime = time;
  12. frames = 0;
  13. }
  14. window.requestAnimationFrame(aaa);
  15. }
  16. aaa();

3.ms 每个渲染帧运行需要多少毫秒

  1. let ms = 0;
  2. var beginTime = (performance || Date).now();
  3. function bbb() {
  4. //当前时间减去开始时间
  5. ms = (performance || Date).now() - beginTime;
  6. console.log(ms);
  7. window.requestAnimationFrame(bbb);
  8. beginTime = (performance || Date).now();
  9. }
  10. bbb();

4.memory 内存占用

  1. usedJSHeapSize
已经使用的内存

  1. jsHeapSizeLimit
内存大小限制
  1. let mb = 0,
  2. mbPercent = 0;
  3. let prevTime = (performance || Date).now();
  4. function ccc() {
  5. var time = (performance || Date).now();
  6. //每秒获取一次
  7. if (time >= prevTime + 1000) {
  8. //获取性能里的内存相关参数,前提是performance.memory存在
  9. var memory = performance.memory;
  10. //1M =1048576=2^20
  11. //使用了多少内存
  12. mb = memory.usedJSHeapSize / 1048576;
  13. //内存占用百分比
  14. mbPercent = memory.usedJSHeapSize / memory.jsHeapSizeLimit;
  15. console.log(mb, mbPercent);
  16. }
  17. window.requestAnimationFrame(ccc);
  18. }
  19. ccc();

5.画 Canvas 的板面

创建 canvas

  1. //name性能名称, fg颜色, bg背景
  2. Stats.Panel = function (name, fg, bg) {
  3. var min = Infinity,
  4. max = 0,
  5. round = Math.round;
  6. var PR = round(window.devicePixelRatio || 1);
  7. var WIDTH = 80 * PR, //canvas板面宽度
  8. HEIGHT = 48 * PR, //canvas板面高度
  9. TEXT_X = 3 * PR, //文本x坐标
  10. TEXT_Y = 2 * PR, //文本y坐标
  11. GRAPH_X = 3 * PR, //图表x坐标
  12. GRAPH_Y = 15 * PR, //图表y坐标
  13. GRAPH_WIDTH = 74 * PR, //图表宽度
  14. GRAPH_HEIGHT = 30 * PR; //图表高度
  15. //创建canvas
  16. var canvas = document.createElement('canvas');
  17. canvas.width = WIDTH;
  18. canvas.height = HEIGHT;
  19. canvas.style.cssText = 'width:80px;height:48px';
  20. var context = canvas.getContext('2d');
  21. //设置字体样式
  22. context.font = 'bold ' + 9 * PR + 'px Helvetica,Arial,sans-serif';
  23. context.textBaseline = 'top';
  24. };

板面更新数值

  1. update:function (value, maxValue) {
  2. //监控过程中,最小最大值范围
  3. min = Math.min(min, value);
  4. max = Math.max(max, value);
  5. context.fillStyle = bg;
  6. context.globalAlpha = 1;
  7. //清空内容重绘
  8. context.fillRect(0, 0, WIDTH, GRAPH_Y);
  9. context.fillStyle = fg;
  10. //画文本,当前数值,name,最小最大值
  11. context.fillText(
  12. round(value) + ' ' + name + ' (' + round(min) + '-' + round(max) + ')',
  13. TEXT_X,
  14. TEXT_Y
  15. );
  16. //截取canvas之前的内容范围,往前移动,覆盖内容
  17. context.drawImage(
  18. canvas,
  19. GRAPH_X + PR,
  20. GRAPH_Y,
  21. GRAPH_WIDTH - PR,
  22. GRAPH_HEIGHT,
  23. GRAPH_X,
  24. GRAPH_Y,
  25. GRAPH_WIDTH - PR,
  26. GRAPH_HEIGHT
  27. );
  28. //清空最后的那部分
  29. context.fillRect(GRAPH_X + GRAPH_WIDTH - PR, GRAPH_Y, PR, GRAPH_HEIGHT);
  30. context.fillStyle = bg;
  31. context.globalAlpha = 0.9;
  32. //画出最新的数值矩形
  33. context.fillRect(
  34. GRAPH_X + GRAPH_WIDTH - PR,
  35. GRAPH_Y,
  36. PR,
  37. round((1 - value / maxValue) * GRAPH_HEIGHT)
  38. );
  39. }

6.创建 Stats 板面

  1. var mode = 0;
  2. var container = document.createElement('div');
  3. container.style.cssText = 'position:fixed;top:0;left:0;cursor:pointer;opacity:0.9;z-index:10000';
  4. //点击切换板面模式
  5. container.addEventListener(
  6. 'click',
  7. function (event) {
  8. event.preventDefault();
  9. showPanel(++mode % container.children.length);
  10. },
  11. false
  12. );
  13. //添加canvas板面
  14. function addPanel(panel) {
  15. container.appendChild(panel.dom);
  16. return panel;
  17. }
  18. //显示对应的板面模式
  19. function showPanel(id) {
  20. for (var i = 0; i < container.children.length; i++) {
  21. container.children[i].style.display = i === id ? 'block' : 'none';
  22. }
  23. mode = id;

添加三种 canvas 板面

  1. //添加索引为0的fps板面
  2. var fpsPanel = addPanel(new Stats.Panel('FPS', '#0ff', '#002'));
  3. //添加索引为1的ms板面
  4. var msPanel = addPanel(new Stats.Panel('MS', '#0f0', '#020'));
  5. //如果performance.memory存在,添加索引为2的内存板面
  6. if (self.performance && self.performance.memory) {
  7. var memPanel = addPanel(new Stats.Panel('MB', '#f08', '#201'));
  8. }
  9. //默认显示fps
  10. showPanel(0);

每个板面数值的更新

  1. var beginTime = (performance || Date).now(),
  2. prevTime = beginTime,
  3. frames = 0;
  4. //开始时间
  5. begin: function () {
  6. beginTime = (performance || Date).now();
  7. },
  8. //计算
  9. end: function () {
  10. frames++;
  11. var time = (performance || Date).now();
  12. //ms板面的数值
  13. msPanel.update(time - beginTime, 200);
  14. if (time >= prevTime + 1000) {
  15. //fps板面数值
  16. fpsPanel.update((frames * 1000) / (time - prevTime), 100);
  17. prevTime = time;
  18. frames = 0;
  19. //内存板面数值更新
  20. if (memPanel) {
  21. var memory = performance.memory;
  22. //1M =1048576=2^20
  23. memPanel.update(memory.usedJSHeapSize / 1048576, memory.jsHeapSizeLimit / 1048576);
  24. }
  25. }
  26. return time;
  27. },
  28. //更新
  29. update: function () {
  30. beginTime = this.end();
  31. },

7.使用 Stats

  1. var stats = new Stats();
  2. document.body.appendChild(stats.dom);
  3. function animate() {
  4. stats.update();
  5. window.requestAnimationFrame(animate);
  6. }
  7. animate();

以上就是stats.js使用性能监控源码分析的详细内容,更多关于stats.js使用性能监控源码分析的资料请关注九品源码其它相关文章!