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

feature(viewer): read csi from viewer params #2033

Merged
merged 1 commit into from
Feb 19, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion examples/viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@
paddingTop: this.header./*OK*/offsetHeight,
visibilityState: this.visibilityState_,
prerenderSize: this.prerenderSize_,
viewerorigin: parseUrl(window.location.href).origin
viewerorigin: parseUrl(window.location.href).origin,
csi: 0
Copy link
Member

Choose a reason for hiding this comment

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

lets call this property performanceTracking. At least on our side.

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

added some additional code in performance.js to ignore all ticks and flush calls when performanceTracking is off.

};
log('Params:' + JSON.stringify(params));

Expand Down
10 changes: 8 additions & 2 deletions src/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class Performance {
opt_from = opt_from == undefined ? null : opt_from;
opt_value = opt_value == undefined ? timer.now() : opt_value;

if (this.viewer_) {
if (this.viewer_ && this.viewer_.isPerformanceTrackingOn()) {
this.viewer_.tick({
'label': label,
'from': opt_from,
Expand Down Expand Up @@ -242,7 +242,7 @@ export class Performance {
* Calls the "flushTicks" function on the viewer.
*/
flush() {
if (this.viewer_) {
if (this.viewer_ && this.viewer_.isPerformanceTrackingOn()) {
this.viewer_.flushTicks();
}
}
Expand Down Expand Up @@ -282,6 +282,12 @@ export class Performance {
return;
}

if (!this.viewer_.isPerformanceTrackingOn()) {
// drop all queued ticks to not leak
this.events_.length = 0;
return;
}

this.events_.forEach(tickEvent => {
this.viewer_.tick(tickEvent);
});
Expand Down
18 changes: 14 additions & 4 deletions src/service/framerate-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ export class Framerate {
*/
this.loadedAd_ = false;

const viewer = viewerFor(this.win);
/** @private @const {!Viewer} */
this.viewer_ = viewerFor(this.win);

/**
* We do not make measurements when the window is hidden, because
* animation frames not not fire in that case.
* @private {boolean}
*/
this.isActive_ = viewer.isVisible();
this.isActive_ = this.isActive();

viewer.onVisibilityChanged(() => {
this.isActive_ = viewer.isVisible();
this.viewer_.onVisibilityChanged(() => {
this.isActive_ = this.isActive();
this.reset_();
if (this.isActive_) {
this.collect();
Expand All @@ -78,6 +79,15 @@ export class Framerate {
this.collect();
}

/**
* Framerate instrumentation should only be on if viewer is visible
* and csi is actually on.
* @return {boolean}
*/
isActive() {
return this.viewer_.isPerformanceTrackingOn() && this.viewer_.isVisible();
}

/**
* Call this when something interesting is about to happen on screen.
* This class will then measure the framerate for the next few seconds.
Expand Down
12 changes: 12 additions & 0 deletions src/service/viewer-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ export class Viewer {
this.paddingTop_;
log.fine(TAG_, '- padding-top:', this.paddingTop_);

/** @private @const {boolean} */
this.performanceTracking_ = this.params_['csi'] === '1';
log.fine(TAG_, '- performanceTracking:', this.performanceTracking_);

/** @private {boolean} */
this.hasBeenVisible_ = this.isVisible();

Expand Down Expand Up @@ -393,6 +397,14 @@ export class Viewer {
return this.isRuntimeOn_;
}

/**
* Identifies if the viewer is recording instrumentation.
* @return {boolean}
*/
isPerformanceTrackingOn() {
return this.performanceTracking_;
}

/**
*/
toggleRuntime() {
Expand Down
30 changes: 30 additions & 0 deletions test/functional/test-framerate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('the framerate service', () => {
let lastRafCallback;
let call = 0;
let visible;
let csiOn;
let performance;
let viewer;
let sandbox;
Expand All @@ -34,6 +35,7 @@ describe('the framerate service', () => {
clock = sandbox.useFakeTimers();
lastRafCallback = null;
visible = true;
csiOn = true;
performance = {
tickDelta: sinon.spy(),
flush: sinon.spy()
Expand All @@ -42,6 +44,9 @@ describe('the framerate service', () => {
isVisible: () => {
return visible;
},
isPerformanceTrackingOn() {
return csiOn;
},
onVisibilityChanged: sinon.spy()
};
win = {
Expand Down Expand Up @@ -164,4 +169,29 @@ describe('the framerate service', () => {
expect(performance.tickDelta.args[2][0]).to.equal('fps');
expect(performance.tickDelta.args[3][0]).to.equal('fal');
});

it('respects viewer csi flag', () => {
csiOn = false;

fr = installFramerateService(win);
expect(viewer.onVisibilityChanged.callCount).to.equal(1);
expect(fr.isActive_).to.be.false;
fr.collect();
expect(fr.requestedFrame_).to.be.null;
visible = true;
viewer.onVisibilityChanged.args[0][0]();
expect(fr.isActive_).to.be.false;
fr.collect();
expect(fr.requestedFrame_).to.be.null;
viewer.onVisibilityChanged.args[0][0](false);
expect(fr.isActive_).to.be.false;
expect(fr.requestedFrame_).to.be.null;
fr.collect();
expect(fr.requestedFrame_).to.be.null;

csiOn = true;
fr.collect();
viewer.onVisibilityChanged.args[0][0](false);
expect(fr.requestedFrame_).to.not.be.null;
});
});
137 changes: 93 additions & 44 deletions test/functional/test-performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,67 +142,116 @@ describe('performance', () => {
flushTicksSpy = sandbox.stub(viewer, 'flushTicks');
});

it('should forward all queued tick events', () => {
perf.tick('start0');
clock.tick(1);
perf.tick('start1', 'start0');
describe('and performanceTracking is off', () => {

expect(perf.events_.length).to.equal(2);
beforeEach(() => {
sandbox.stub(viewer, 'isPerformanceTrackingOn')
.returns(false);
});

perf.coreServicesAvailable();
it('should not forward queued ticks', () => {
perf.tick('start0');
clock.tick(1);
perf.tick('start1', 'start0');

expect(tickSpy.firstCall.args[0]).to.be.jsonEqual({
label: 'start0',
from: null,
value: 0,
expect(perf.events_.length).to.equal(2);

perf.coreServicesAvailable();
perf.flushQueuedTicks_();
perf.flush();

expect(perf.events_.length).to.equal(0);

expect(tickSpy.callCount).to.equal(0);
expect(flushTicksSpy.callCount).to.equal(0);
});
expect(tickSpy.secondCall.args[0]).to.be.jsonEqual({
label: 'start1',
from: 'start0',
value: 1,

it('should ignore all calls to tick', () => {
perf.coreServicesAvailable();

perf.tick('start0');
expect(tickSpy.callCount).to.equal(0);
});

it('should ignore all calls to flush', () => {
perf.coreServicesAvailable();

perf.tick('start0');
perf.flush();
expect(flushTicksSpy.callCount).to.equal(0);
});
});

it('should have no more queued tick events after flush', () => {
perf.tick('start0');
perf.tick('start1');
describe('and performanceTracking is on', () => {

expect(perf.events_.length).to.equal(2);
beforeEach(() => {
sandbox.stub(viewer, 'isPerformanceTrackingOn')
.returns(true);
});

perf.coreServicesAvailable();
it('should forward all queued tick events', () => {
perf.tick('start0');
clock.tick(1);
perf.tick('start1', 'start0');

expect(perf.events_.length).to.equal(0);
});
expect(perf.events_.length).to.equal(2);

it('should forward tick events', () => {
perf.coreServicesAvailable();
perf.coreServicesAvailable();

clock.tick(100);
perf.tick('start0');
perf.tick('start1', 'start0', 300);
expect(tickSpy.firstCall.args[0]).to.be.jsonEqual({
label: 'start0',
from: null,
value: 0,
});
expect(tickSpy.secondCall.args[0]).to.be.jsonEqual({
label: 'start1',
from: 'start0',
value: 1,
});
});

expect(tickSpy.firstCall.args[0]).to.be.jsonEqual({
label: 'start0',
from: null,
value: 100,
it('should have no more queued tick events after flush', () => {
perf.tick('start0');
perf.tick('start1');

expect(perf.events_.length).to.equal(2);

perf.coreServicesAvailable();

expect(perf.events_.length).to.equal(0);
});
expect(tickSpy.secondCall.args[0]).to.be.jsonEqual({
label: 'start1',
from: 'start0',
value: 300,

it('should forward tick events', () => {
perf.coreServicesAvailable();

clock.tick(100);
perf.tick('start0');
perf.tick('start1', 'start0', 300);

expect(tickSpy.firstCall.args[0]).to.be.jsonEqual({
label: 'start0',
from: null,
value: 100,
});
expect(tickSpy.secondCall.args[0]).to.be.jsonEqual({
label: 'start1',
from: 'start0',
value: 300,
});
});
});

it('should call the flush callback', () => {
expect(flushTicksSpy.callCount).to.equal(0);
// coreServicesAvailable calls flush once.
perf.coreServicesAvailable();
expect(flushTicksSpy.callCount).to.equal(1);
perf.flush();
expect(flushTicksSpy.callCount).to.equal(2);
perf.flush();
expect(flushTicksSpy.callCount).to.equal(3);
it('should call the flush callback', () => {
expect(flushTicksSpy.callCount).to.equal(0);
// coreServicesAvailable calls flush once.
perf.coreServicesAvailable();
expect(flushTicksSpy.callCount).to.equal(1);
perf.flush();
expect(flushTicksSpy.callCount).to.equal(2);
perf.flush();
expect(flushTicksSpy.callCount).to.equal(3);
});
});

});

describe('coreServicesAvailable', () => {
Expand Down
14 changes: 14 additions & 0 deletions test/functional/test-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ describe('Viewer', () => {
expect(viewer.getPrerenderSize()).to.equal(3);
});

it('should configure performance tracking', () => {
windowApi.location.hash = '';
let viewer = new Viewer(windowApi);
expect(viewer.isPerformanceTrackingOn()).to.be.false;

windowApi.location.hash = '#csi=1';
viewer = new Viewer(windowApi);
expect(viewer.isPerformanceTrackingOn()).to.be.true;

windowApi.location.hash = '#csi=0';
viewer = new Viewer(windowApi);
expect(viewer.isPerformanceTrackingOn()).to.be.false;
});

it('should configure correctly for iOS embedding', () => {
windowApi.name = '__AMP__viewportType=natural';
windowApi.parent = {};
Expand Down