VCF Automation Blog

from Stefan Schnell

To measure execution times, e.g. to determine the performance of methods or functions, it is useful to have a corresponding class that can measure the elapsed times, similar to a stopwatch. Here an approach to handle this requirement.

Stopwatch Class


/**
 * @module de.stschnell.stopWatch
 *
 * @version 0.1.0
 *
 * @outputType Any
 *
 * @description Stopwatch class for time measurement
 */

/**
 * Stopwatch class for VMware Aria Automation for time measurement.
 *
 * @author Stefan Schnell <mail@stefan-schnell.de>
 * @license MIT
 * @version 0.1.0
 *
 * Checked with Rhino engines version 1.7R4 and 1.7.14,
 * and VMware Aria Automation 8.14.0 and 8.18.0.
 */

function stopWatch() {

  var _stopWatchNS = function() {

    this.pointsInTime = [];
    this._elapsedTime = 0;
    this._startTime = 0;
    this._stopTime = 0;

  };

  _stopWatchNS.prototype = {

    /**
     * Delivers the elapsed time between start and stop.
     *
     * @function elapsedTime
     * @returns {number}
     *
     * @example
     * var oStopWatch = stopWatch();
     * oStopWatch.start();
     * System.sleep(250);
     * oStopWatch.stop();
     * System.log(oStopWatch.elapsedTime());
     */
    elapsedTime : function() {
      return this._elapsedTime;
    },

    /**
     * Sets a point in time in an array.
     * This allows you to set intermediate points that can be used in
     * a time measurement. They are stored in an array, so they can be
     * accessed using its methods.
     *
     * @function pointInTime
     *
     * @example
     * var oStopWatch = stopWatch();
     * oStopWatch.start();
     * System.sleep(250);
     * oStopWatch.pointInTime();
     * System.sleep(250);
     * oStopWatch.stop();
     * oStopWatch.pointsInTime.forEach( function(pointInTime) {
     *   System.log(pointInTime);
     * });
     */
    pointInTime : function() {
      this.pointsInTime.push(Date.now());
    },

    /**
     * Starts the time measurement.
     *
     * @function start
     *
     * @example
     * var oStopWatch = stopWatch();
     * oStopWatch.start();
     * System.sleep(250);
     * oStopWatch.stop();
     * System.log(oStopWatch.elapsedTime());
     */
    start : function() {
      this._startTime = Date.now();
      this.pointsInTime.push(this._startTime);
    },

    /**
     * Stops the time measurement.
     *
     * @function stop
     *
     * @example
     * var oStopWatch = stopWatch();
     * oStopWatch.start();
     * System.sleep(250);
     * oStopWatch.stop();
     * System.log(oStopWatch.elapsedTime());
     */
    stop : function() {
      this._stopTime = Date.now();
      this.pointsInTime.push(this._stopTime);
      this._elapsedTime = this._stopTime - this._startTime;
    }

  };

  return new _stopWatchNS();

}