Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

feat: add timing API #149

Merged
merged 9 commits into from
Dec 18, 2019
Merged

feat: add timing API #149

merged 9 commits into from
Dec 18, 2019

Conversation

imurchie
Copy link
Contributor

We have a number of different mechanisms for getting the duration of operations. This is a first pass at making an extensible API for our use. Once the Performance API is out of super-experimental in Node, this could encapsulate that too, without needing application code to be messed with.

lib/timing.js Outdated
*/
start () {
// once Node 10 is no longer supported, this check can be removed
this.startTime = _.isFunction(process.hrtime.bigint)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

startTime could be memoized during module load so it doesn't need to happen every time this class is used. Probably a very small optimization, but presumably when timing things we want to have as little overhead as possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and by startTime, i mean, the function used to get the start time

lib/timing.js Outdated Show resolved Hide resolved
lib/timing.js Outdated Show resolved Hide resolved
lib/timing.js Outdated Show resolved Hide resolved
@imurchie
Copy link
Contributor Author

Added the ability to just print the Timer itself, without calling getDuration(). E.g.,

const timer = new timing.Timer().start();
// ...
log.debug(`The operation took ${timer}`);

outputs

The operation took 42ms

lib/timing.js Outdated
/**
* Creates a timer
*
* @param {?string} units - the default units for conversion
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still cannot understand why units are needed. We could simply keep the absolute number of seconds/nanoseconds and have asSeconds, asNanos, etc. methods like TimeSpan in C# or Duration in Java have

@imurchie
Copy link
Contributor Author

Alright. Not sure why it's preferable, but moved the conversions in the Duration class, so now it is used something like

const timer = new Timer().start();

// ...

log.debug(`This took ${timer.getDuration().asSeconds.toFixed(3)}s`)

lib/timing.js Outdated
* Class representing a duration, encapsulating the number and units.
*/
class Duration {
constructor (duration) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I's rather rename the property to nanos

lib/timing.js Outdated
this._duration = duration;
}

get duration () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this getter redundant now?

@imurchie
Copy link
Contributor Author

Basically. But mirroring the internal, read-only variable.

lib/timing.js Outdated
/**
* Get the duration since the timer was started
*
* @return {Duration} the duration, in the specified units
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the param description should be updated

if (_.isArray(this.startTime)) {
// startTime was created using process.hrtime()
const [seconds, nanos] = process.hrtime(this.startTime);
nanoDuration = seconds * NS_PER_S + nanos;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can return immediately here. There is no need for the nanoDuration intermediate variable

/**
* Class representing a duration, encapsulating the number and units.
*/
class Duration {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future we could make the Duration class more advanced (e.g. add +/- operations, factory methods to create duration objects from different units (aka Duration.fromSeconds(x), etc)) and use it in other scenarios.
Or use the one, from moment.js lib.

Although, the current implementation is pretty enough for the task it is supposed to solve

@imurchie imurchie merged commit 1837308 into master Dec 18, 2019
@imurchie imurchie deleted the isaac-timings branch December 18, 2019 21:15
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants