Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Allow load metrics emission without file info #828

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1525,15 +1525,8 @@ class Preview extends EventEmitter {
const downloadTag = Timer.createTag(this.file.id, LOAD_METRIC.downloadResponseTime);
const fullLoadTag = Timer.createTag(this.file.id, LOAD_METRIC.fullDocumentLoadTime);

// Do nothing if there is nothing worth logging.
const infoTime = Timer.get(infoTag) || {};
if (!infoTime.elapsed) {
Timer.reset([infoTag, convertTag, downloadTag, fullLoadTag]);
return;
}

const timerList = [
infoTime,
Timer.get(infoTag) || {},
Timer.get(convertTag) || {},
Timer.get(downloadTag) || {},
Timer.get(fullLoadTag) || {}
Expand Down
39 changes: 17 additions & 22 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2282,7 +2282,7 @@ describe('lib/Preview', () => {
Timer.reset();
});

it('should do nothingescape early if no file or file id', () => {
it('should do nothing and escape early if no file or file id', () => {
sandbox.stub(Timer, 'reset');
sandbox.stub(preview, 'emit');
preview.file = undefined;
Expand All @@ -2291,23 +2291,22 @@ describe('lib/Preview', () => {
expect(preview.emit).to.not.be.called;
});

it('should reset the timer and escape early if the first load milestone is not hit', () => {
Timer.reset(); // Clear out all entries in the Timer
sandbox.stub(Timer, 'reset');
sandbox.stub(preview, 'emit');
it('should emit a preview_metric event', (done) => {
preview.once(PREVIEW_METRIC, () => {
done();
});
preview.emitLoadMetrics();
expect(Timer.reset).to.be.called;
expect(preview.emit).to.not.be.called;
});

it('should emit a preview_metric event', (done) => {
preview.on(PREVIEW_METRIC, () => {
it('should emit a preview_metric event with event_name "load"', (done) => {
preview.once(PREVIEW_METRIC, (metric) => {
expect(metric.event_name).to.equal(LOAD_METRIC.previewLoadEvent);
done();
});
preview.emitLoadMetrics();
});

it('should emit a preview_metric event with event_name "preview_load"', () => {
it('should emit a preview_metric event where the value property equals the sum of all load events', (done) => {
const tag = Timer.createTag(preview.file.id, LOAD_METRIC.fullDocumentLoadTime);
Timer.start(tag);
Timer.stop(tag);
Expand All @@ -2317,25 +2316,20 @@ describe('lib/Preview', () => {

const expectedTime = 30; // 10ms + 20ms

preview.on(PREVIEW_METRIC, (metric) => {
preview.once(PREVIEW_METRIC, (metric) => {
expect(metric.value).to.equal(expectedTime);
done();
});
preview.emitLoadMetrics();
});

it('should emit a preview_metric event where the value property equals the sum of all load events', () => {
preview.on(PREVIEW_METRIC, (metric) => {
expect(metric.event_name).to.equal(LOAD_METRIC.previewLoadEvent);
});
preview.emitLoadMetrics();
});

it('should emit a preview_metric event with an object, with all of the proper load properties', () => {
preview.on(PREVIEW_METRIC, (metric) => {
it('should emit a preview_metric event with an object, with all of the proper load properties', (done) => {
preview.once(PREVIEW_METRIC, (metric) => {
expect(metric[LOAD_METRIC.fileInfoTime]).to.exist;
expect(metric[LOAD_METRIC.convertTime]).to.exist;
expect(metric[LOAD_METRIC.downloadResponseTime]).to.exist;
expect(metric[LOAD_METRIC.fullDocumentLoadTime]).to.exist;
done();
});
preview.emitLoadMetrics();
});
Expand All @@ -2348,13 +2342,14 @@ describe('lib/Preview', () => {
expect(preview.emit).to.be.called;
});

it('should default all un-hit milestones, after the first, to 0, and cast float values to ints', () => {
it('should default all un-hit milestones, after the first, to 0, and cast float values to ints', (done) => {
Timer.get(fileInfoTag).elapsed = 1.00001236712394687;
preview.on(PREVIEW_METRIC, (metric) => {
preview.once(PREVIEW_METRIC, (metric) => {
expect(metric[LOAD_METRIC.fileInfoTime]).to.equal(1); // Converted to int
expect(metric[LOAD_METRIC.convertTime]).to.equal(0);
expect(metric[LOAD_METRIC.downloadResponseTime]).to.equal(0);
expect(metric[LOAD_METRIC.fullDocumentLoadTime]).to.equal(0);
done();
});
preview.emitLoadMetrics();
});
Expand Down
1 change: 0 additions & 1 deletion src/lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export const ERROR_CODE = {
FLASH_NOT_ENABLED: 'error_flash_not_enabled'
};

export const PREVIEW_LOAD_EVENT = '';
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we removing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not used anywhere AND it's an empty string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We now use the below LOAD_METRIC.previewLoadEvent

// Event fired from Preview with error details
export const PREVIEW_ERROR = 'preview_error';
// Event fired from Preview with performance metrics
Expand Down