All files / engine/Source/Scene PerformanceDisplay.js

69.49% Statements 41/59
38.88% Branches 7/18
80% Functions 4/5
69.49% Lines 41/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 60 61 62 63 64 65 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                      8x   8x   8x         8x   8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x   8x 8x 8x 8x   8x 8x 8x 8x 8x 8x     1x                       1x 1x                                       1x 2x 2x   2x 2x 2x                     2x 2x 2x                             1x 1x      
import Frozen from "../Core/Frozen.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import DeveloperError from "../Core/DeveloperError.js";
import getTimestamp from "../Core/getTimestamp.js";
import getElement from "../DataSources/getElement.js";
 
/**
 * @private
 */
function PerformanceDisplay(options) {
  options = options ?? Frozen.EMPTY_OBJECT;
 
  const container = getElement(options.container);
  //>>includeStart('debug', pragmas.debug);
  Iif (!defined(container)) {
    throw new DeveloperError("container is required");
  }
  //>>includeEnd('debug');
 
  this._container = container;
 
  const display = document.createElement("div");
  display.className = "cesium-performanceDisplay";
  const fpsElement = document.createElement("div");
  fpsElement.className = "cesium-performanceDisplay-fps";
  this._fpsText = document.createTextNode("");
  fpsElement.appendChild(this._fpsText);
  const msElement = document.createElement("div");
  msElement.className = "cesium-performanceDisplay-ms";
  this._msText = document.createTextNode("");
  msElement.appendChild(this._msText);
  display.appendChild(msElement);
  display.appendChild(fpsElement);
  this._container.appendChild(display);
 
  this._lastFpsSampleTime = getTimestamp();
  this._lastMsSampleTime = getTimestamp();
  this._fpsFrameCount = 0;
  this._msFrameCount = 0;
 
  this._throttled = false;
  const throttledElement = document.createElement("div");
  throttledElement.className = "cesium-performanceDisplay-throttled";
  this._throttledText = document.createTextNode("");
  throttledElement.appendChild(this._throttledText);
  display.appendChild(throttledElement);
}
 
Object.defineProperties(PerformanceDisplay.prototype, {
  /**
   * The display should indicate the FPS is being throttled.
   * @memberof PerformanceDisplay.prototype
   *
   * @type {boolean}
   */
  throttled: {
    get: function () {
      return this._throttled;
    },
    set: function (value) {
      Eif (this._throttled === value) {
        return;
      }
 
      if (value) {
        this._throttledText.nodeValue = "(throttled)";
      } else {
        this._throttledText.nodeValue = "";
      }
 
      this._throttled = value;
    },
  },
});
 
/**
 * Update the display.  This function should only be called once per frame, because
 * each call records a frame in the internal buffer and redraws the display.
 *
 * @param {boolean} [renderedThisFrame=true] If provided, the FPS count will only update and display if true.
 */
PerformanceDisplay.prototype.update = function (renderedThisFrame) {
  const time = getTimestamp();
  const updateDisplay = renderedThisFrame ?? true;
 
  this._fpsFrameCount++;
  const fpsElapsedTime = time - this._lastFpsSampleTime;
  Iif (fpsElapsedTime > 1000) {
    let fps = "N/A";
    if (updateDisplay) {
      fps = ((this._fpsFrameCount * 1000) / fpsElapsedTime) | 0;
    }
 
    this._fpsText.nodeValue = `${fps} FPS`;
    this._lastFpsSampleTime = time;
    this._fpsFrameCount = 0;
  }
 
  this._msFrameCount++;
  const msElapsedTime = time - this._lastMsSampleTime;
  Iif (msElapsedTime > 200) {
    let ms = "N/A";
    if (updateDisplay) {
      ms = (msElapsedTime / this._msFrameCount).toFixed(2);
    }
 
    this._msText.nodeValue = `${ms} MS`;
    this._lastMsSampleTime = time;
    this._msFrameCount = 0;
  }
};
 
/**
 * Destroys the WebGL resources held by this object.
 */
PerformanceDisplay.prototype.destroy = function () {
  return destroyObject(this);
};
export default PerformanceDisplay;