Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface IEntry {
- type: string;
- name: string;
- startTime: number | null;
- }
- class Entry implements IEntry {
- type: string = 'mark';
- name: string;
- startTime: number | null = null;
- constructor(name: string, startTime: number) {
- this.name = name;
- this.startTime = startTime;
- }
- }
- interface IMeasurement extends IEntry {
- duration: number | null;
- }
- class Measurement extends Entry implements IMeasurement {
- type: string = 'measurement';
- duration: number = null;
- constructor(name: string, startTime: number, duration: number) {
- super(name, startTime);
- this.duration = duration;
- }
- }
- export class PerformanceMeasurement {
- initTiming: number|null = null;
- private entries: Array<IEntry|IMeasurement> = [];
- private now: () => number = require("performance-now");
- constructor() {
- this.initTiming = this.now();
- }
- public mark(name: string): void {
- let entry = new Entry(name, this.now());
- this.entries.push(entry);
- }
- public mesure(name: string, startMark: string, endMark: string): void {
- let startTime:number = this.getEntry(startMark).startTime;
- let endTime:number = this.getEntry(endMark).startTime;
- let duration = endTime - startTime;
- let measurement = new Measurement(name, this.now(), duration);
- this.entries.push(measurement);
- }
- public getEntry(name: string): IEntry {
- let filteredEntries = this.entries.filter(entry => entry.name === name);
- return filteredEntries[0];
- }
- public getEntries(): Array<IEntry|IMeasurement> {
- return this.entries;
- }
- }
- let PM = new PerformanceMeasurement();
- PM.mark('asd');
- PM.mark('dsa');
- PM.mesure('zxc', 'asd', 'dsa');
- console.log(PM.getEntry('asd'));
- console.log(PM.getEntry('dsa'));
- console.log(PM.getEntry('zxc'));
- console.log(PM.getEntries());
- console.log(PM);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement