Skip to main content

Stat

A tracker for a single statistic.

Usage

Create a Stat instance using Stats.get. There are two basic usage patterns, timer and counter. Timer usage involves the methods timeStart, timeEnd, addTime, getHz, and getAverageTime:

import {Stats} from '@probe.gl/stats';

const stats = new Stats({id: 'my-stats'});
const executionTime = stats.get('Time');
executionTime.timeStart();
executionTime.timeEnd();
executionTime.addTime(16);

// getHz and getAverageTime based on the
// number of individual timings that were taken
executionTime.getHz();
executionTime.getAverageTime();

Counter usage involves the methods incrementCount, decrementCount, addCount, and subtractCount:

const stats = new Stats({id: 'my-stats'});
const memoryUsage = stats.get('Mem');
memoryUsage.incrementCount();
memoryUsage.decrementCount();
memoryUsage.addCount(1024);
memoryUsage.subtractCount(512);

For time statistics, the Stat object can also define a sample window, to only update count or time after a given number of samples are taken:

const stats = new Stats({id: 'my-stats'});
const executionTime = stats.get('Time').setSampleSize(3);
executionTime.addTime(1);
executionTime.addTime(2);
// `time` is still 0 at this point
executionTime.getHz(); // => 0
executionTime.getAverageTime(); // => 0

executionTime.addTime(3);
// Now `time` = 6
executionTime.getAverageTime(); // => 2
executionTime.addTime(1);
executionTime.addTime(1);
executionTime.addTime(1);
executionTime.getAverageTime(); // => 1.5
executionTime.getSampleAverageTime(); // => 1 (only from the last sample set)

Properties

name : String

Name of the stat.

count : Number

Accumulated count or number of timings.

time : Number

Accumulated time from all timings.

lastTiming : Number

Last timing taken.

lastSampleTime : Number

Timing of the last completed set of samples.

Methods

constructor

new Stat(name, type)

  • name (String) - the name of the stat.
  • type (String) - the type of the stat.

Supported options:

  • count
  • averageTime
  • totalTime
  • fps
  • memory

incrementCount

Increase count by 1.

stat.incrementCount()

decrementCount

Decrease count by 1.

stat.decrementCount()

addCount

Increase count by value.

stat.addCount(value)

  • value (Number, required) - the amount to add to count.

subtractCount

Decrease count by value.

stat.subtractCount(value)

  • value (Number, required) - the amount to subtract from count.

timeStart

Start a timer.

stat.timeStart()

timeEnd

End a timer. Time elapsed since the last timeStart is added to time and count is incremented by 1.

stat.timeEnd()

addTime

Increase time by value and increment count by 1.

stat.addTime(value)

  • value (Number, required) - time in millisecons to add to time.

getHz

Calculate the average number of timing events per second (i.e. samples / (time * 1000).

stat.getHz()

getAverageTime

Calculate the average amount of time take per timing event in milliseconds (i.e. time / samples).

stat.getAverageTime()

getAverageCount

Calculate the average count per sampling (i.e. count / samples).

stat.getAverageCount()

getSampleHz

Calculate the average number of timing events per second (i.e. samples / (time * 1000) for the last completed set of samples.

stat.getHz()

getSampleAverageTime

Calculate the average amount of time take per timing event in milliseconds (i.e. time / samples) for the last completed set of samples.

stat.getAverageTime()

getSampleAverageCount

Calculate the average count per sampling (i.e. count / samples) for the last completed set of samples.

stat.getAverageTime()