Skip to content

Commit

Permalink
split StopwatchEvent into a separate file, updated and added addition…
Browse files Browse the repository at this point in the history
…al tests
  • Loading branch information
patinthehat authored and Patrick committed Feb 9, 2021
1 parent dbe0a8b commit 1d8068b
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/Payloads/MeasurePayload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { end } from '../lib/utils';
import { StopwatchEvent } from '../Stopwatch/Stopwatch';
import { StopwatchEvent } from '../Stopwatch/StopwatchEvent';
import { Payload } from '../Payloads/Payload';

export class MeasurePayload extends Payload {
Expand Down
36 changes: 1 addition & 35 deletions src/Stopwatch/Stopwatch.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,7 @@
/* eslint-disable @typescript-eslint/no-inferrable-types */

import { end } from '../lib/utils';
import { StopWatch as StopwatchNode } from 'stopwatch-node';

export class StopwatchEvent {
protected name: string | undefined;
protected laps: number[] = [];
protected startedAt: number = 0;
protected endedAt: number = 0;
protected lapTime: number = 0;

constructor(sw: Stopwatch, lapTime: number | null = null) {
this.name = sw.name?.slice(0);
this.laps = sw.laps.slice(0);
this.startedAt = sw.startedAt;
this.endedAt = sw.endedAt;
this.lapTime = lapTime ?? new Date().getTime();
}

public getDuration(): number {
return this.laps.reduce((prev, cur) => {
return cur + prev;
}, 0);
}

public getMemory() {
return 0;
}

public getPeriods(): number[] {
return this.laps.slice();
}

public getPreviousDuration(): number {
return this.lapTime - end(this.laps);
}
}
import { StopwatchEvent } from './StopwatchEvent';

export class Stopwatch {
protected sw: StopwatchNode;
Expand Down
34 changes: 34 additions & 0 deletions src/Stopwatch/StopwatchEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { end } from '../lib/utils';
import { Stopwatch } from './Stopwatch';

export class StopwatchEvent {
protected name: string | undefined;
protected laps: number[] = [];
protected startedAt: number;
protected endedAt: number;
protected lapTime: number;

constructor(sw: Stopwatch, lapTime: number | null = null) {
this.name = sw.name?.slice(0);
this.laps = sw.laps.slice(0);
this.startedAt = sw.startedAt;
this.endedAt = sw.endedAt;
this.lapTime = lapTime ?? new Date().getTime();
}

public getDuration(): number {
return this.laps.reduce((prev, cur) => cur + prev, 0);
}

public getMemory() {
return 0;
}

public getPeriods(): number[] {
return this.laps.slice();
}

public getPreviousDuration(): number {
return end(this.laps) - (new Date().getTime() - this.lapTime);
}
}
56 changes: 55 additions & 1 deletion tests/Ray.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ray, Ray } from './../src/RayNode';
import { Ray as BaseRay } from './../src/Ray';
import { NullPayload } from './../src/Payloads/NullPayload';
import { Request } from './../src/Request';
import { end } from '../src/lib/utils';
import { end, usleep } from '../src/lib/utils';

let client: FakeClient, myRay: Ray, myBaseRay: BaseRay;

Expand Down Expand Up @@ -378,3 +378,57 @@ it('can transform a request into JSON', () => {

expect(req.toJson()).toMatchSnapshot();
});

it('measures the execution time of a closure', async () => {
myRay.measure(() => {
usleep(200);
});

expect(client.sentPayloads()).toMatchSnapshot();
});

it('measures the execution time of repeated and unnamed calls to measure', async () => {
myRay.measure();
usleep(200);
myRay.measure();
usleep(200);
myRay.measure();

expect(client.sentPayloads()).toMatchSnapshot();
});

it('measures the execution time of named stopwatches', async () => {
myRay.measure('first');
usleep(200);
myRay.measure('first');

expect(client.sentPayloads()).toMatchSnapshot();
});

it('removes named stopwatches', () => {
myRay.measure('mytimer');
expect(Ray.stopWatches.mytimer).not.toBeUndefined();
myRay.stopTime('mytimer');
expect(Ray.stopWatches.mytimer).toBeUndefined();
});

it('removes all stopwatches', () => {
myRay.measure('mytimer1');
myRay.measure('mytimer2');
expect(Ray.stopWatches.mytimer1).not.toBeUndefined();
expect(Ray.stopWatches.mytimer2).not.toBeUndefined();
myRay.stopTime();
expect(JSON.stringify(Ray.stopWatches)).toBe('{}');
});

it('pauses code execution', async () => {
await myRay.pause();

expect(client.sentPayloads()).toMatchSnapshot();
});

it('sends an html payload when calling ray() with an object argument', () => {
myRay.send({ A: 123, B: [4, 5, 6] });

expect(client.sentPayloads()).toMatchSnapshot();
});
59 changes: 59 additions & 0 deletions tests/Stopwatch/Stopwatch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable no-undef */

import { usleep } from '../../src/lib/utils';
import Stopwatch from './../../src/Stopwatch/Stopwatch';

let stopwatch: Stopwatch;

beforeEach(() => {
stopwatch = new Stopwatch('one');
});

describe('Stopwatch', () => {
it('can created an unnamed stopwatch', () => {
const sw = new Stopwatch();
expect(sw.name).toBeUndefined();
});

it('can be reset', () => {
stopwatch.startedAt = 100;
stopwatch.endedAt = 200;

stopwatch.start('one');
stopwatch.lap();
stopwatch.stop();

stopwatch.reset();

expect(stopwatch.startedAt).toBe(0);
expect(stopwatch.endedAt).toBe(0);
expect(stopwatch.getLaps().length).toBe(0);
});

it('can get the total duration', async () => {
stopwatch.start('one');
usleep(100);
stopwatch.stop();

expect(Math.floor(stopwatch.totalDuration() / 10)).toBe(10);
});

it('can get the previous duration', async () => {
stopwatch.start('one');
usleep(100);
const ev1 = stopwatch.lap();
usleep(100);
const ev2 = stopwatch.lap();
expect(Math.floor(ev2.getPreviousDuration() / 10)).toBe(10);
stopwatch.stop();
});

it('can get the laps', async () => {
stopwatch.start('one');
usleep(5);
stopwatch.stop();

expect(stopwatch.getLaps().length).toBe(1);
});
});
22 changes: 21 additions & 1 deletion tests/TestClasses/FakeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,35 @@ export class FakeClient extends Client {
payload.data.origin.function_name = 'xxxx';
payload.data.origin.file = fn.replace(this.baseDirectory(), '');
//payload.data.origin.file = this.convertToUnixPath(payload.data.origin.file);

payload.data.origin.line_number = 999;

if (payload.getType() === 'measure') {
payload.data.content.total_time = Math.floor(
payload.data.content.total_time / 10
);
payload.data.content.time_since_last_call = Math.floor(
payload.data.content.time_since_last_call / 10
);
}

if (payload.getType() === 'create_lock') {
payload.data.content.name = 'xxxxx';
}
});

requestProperties.meta = [];

this.sentRequests.push(requestProperties);
}

// eslint-disable-next-line no-unused-vars
public async lockExists(lockName: string) {
// eslint-disable-next-line no-unused-vars
return new Promise((resolve, reject) => {
resolve({ active: false, stop_exectution: true });
});
}

public sentPayloads(): any[] {
return this.sentRequests;
}
Expand Down
Loading

0 comments on commit 1d8068b

Please sign in to comment.