Skip to content

Commit b3ce01a

Browse files
authored
Merge pull request #2 from gokatz/patch_v0.1.0_release
v0.1.0 release
2 parents cf09e77 + 50e3caa commit b3ce01a

File tree

5 files changed

+151
-11
lines changed

5 files changed

+151
-11
lines changed

README.md

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,64 @@
11
# ember-appmetrics
22

3-
This README outlines the details of collaborating on this Ember addon.
3+
[![Build Status](https://travis-ci.org/gokatz/ember-appmetrics.svg?branch=master)](https://travis-ci.org/gokatz/ember-appmetrics)
4+
5+
The ember version of [appmetrics.js](https://github.com/ebidel/appmetrics.js). Used to measure various things in your Ember app with ever simple APIs.
6+
7+
## Installation
8+
For Ember CLI >= `0.2.3`:
9+
```shell
10+
ember install ember-appmetrics
11+
```
12+
For Ember CLI < `0.2.3`:
13+
```shell
14+
ember install:addon ember-appmetrics
15+
```
16+
17+
## Compatibility
18+
This addon is tested against the latest stable Ember version.
19+
20+
## Usage
21+
22+
Inject the metrics service like `'metrics: Ember.inject.service()'` into the class where you want to measure the performance or use initializers if you are going with one-time injection.
23+
24+
Addon provides three API to measure the performace of a given period.
25+
- `start` : need to call this api with an event name as argument to mark the starting point
26+
- `end` : need to call this api with an event name as argument to mark the ending point
27+
- `measure` : will return the calculated time for the given event
28+
29+
```js
30+
this.get('metric').start('accounts_page');
31+
Ember.run.scheduleOnce('afterRender', () => {
32+
this.get('metric').end('accounts_page');
33+
let accountsPageRenderDuration = this.get('metric').measure('accounts_page');
34+
console.log(accountsPageRenderDuration); // will return the duration to for this render performance in milliseconds.
35+
});
36+
```
37+
38+
## Browser support
39+
40+
Since fall back machanism of all level has been handled in addon itself, the only thing addon needs is that the browser must have Date API, which is supported in all major and minor browsers.
41+
42+
PS: In Safari, the User Timing API (performance.mark()) is not available, so the DevTools timeline will not be annotated with marks.
443

544
## Installation
645

7-
* `git clone <repository-url>` this repository
8-
* `cd ember-appmetrics`
46+
* `git clone` this repository
947
* `npm install`
1048
* `bower install`
1149

1250
## Running
1351

14-
* `ember serve`
15-
* Visit your app at [http://localhost:4200](http://localhost:4200).
52+
* `ember server`
53+
* Visit your app at http://localhost:4200.
1654

1755
## Running Tests
1856

19-
* `npm test` (Runs `ember try:each` to test your addon against multiple Ember versions)
2057
* `ember test`
2158
* `ember test --server`
2259

2360
## Building
2461

2562
* `ember build`
2663

27-
For more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/).
64+
For more information on using ember-cli, visit [http://www.ember-cli.com/](http://www.ember-cli.com/).

addon/services/metrics.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import Ember from 'ember';
2+
3+
const { typeOf } = Ember;
4+
5+
export default Ember.Service.extend({
6+
isMarkSupportedBrowser: true,
7+
isNowSupportedBrowser: true,
8+
9+
secondaryPerformanceObj: {},
10+
11+
init() {
12+
if (!(window.performance && window.performance.mark)) {
13+
console.log('Performance.mark is not supported in this browser. Hence falling back to performance.now()');
14+
this.set('isMarkSupportedBrowser', false);
15+
16+
if (!(window.performance && window.performance.now)) {
17+
console.log('Performance.now is also not supported. Hence falling back to javascript Date API');
18+
this.set('isNowSupportedBrowser', false);
19+
20+
}
21+
}
22+
},
23+
24+
start(eventName) {
25+
let secondaryPerformanceObj = this.get('secondaryPerformanceObj');
26+
secondaryPerformanceObj[eventName] = secondaryPerformanceObj[eventName] || {};
27+
let eventObj = secondaryPerformanceObj[eventName];
28+
29+
if (typeOf(eventName) !== 'string') {
30+
throw 'Expected type String for invoking `start`';
31+
}
32+
33+
if (this.get('isNowSupportedBrowser')) {
34+
if (this.get('isMarkSupportedBrowser')) {
35+
performance.mark(`mark_${eventName}_start`);
36+
} else {
37+
eventObj.start = window.performance.now();
38+
}
39+
} else {
40+
eventObj.start = new Date().valueOf();
41+
}
42+
43+
return;
44+
},
45+
46+
end(eventName) {
47+
let secondaryPerformanceObj = this.get('secondaryPerformanceObj');
48+
49+
secondaryPerformanceObj[eventName] = secondaryPerformanceObj[eventName] || {};
50+
let eventObj = secondaryPerformanceObj[eventName];
51+
52+
if (typeOf(eventName) !== 'string') {
53+
throw 'Expected type String for invoking `end`';
54+
}
55+
56+
if (this.get('isNowSupportedBrowser')) {
57+
let startMark = `mark_${eventName}_start`;
58+
let endMark = `mark_${eventName}_end`;
59+
60+
if (this.get('isMarkSupportedBrowser')) {
61+
performance.mark(endMark);
62+
performance.measure(eventName, startMark, endMark);
63+
} else {
64+
eventObj.end = window.performance.now();
65+
}
66+
} else {
67+
eventObj.end = new Date().valueOf();
68+
}
69+
return;
70+
},
71+
72+
measure(eventName) {
73+
let secondaryPerformanceObj = this.get('secondaryPerformanceObj');
74+
let eventObj = secondaryPerformanceObj[eventName] || {};
75+
let duration;
76+
77+
if (this.get('isMarkSupportedBrowser')) {
78+
let perfEntries = performance.getEntriesByName(eventName);
79+
// poping up the last entry pushed into teh array
80+
let entry = perfEntries[perfEntries.length - 1];
81+
if (entry) {
82+
duration = entry.duration;
83+
}
84+
} else {
85+
duration = eventObj.end - eventObj.start;
86+
}
87+
88+
return duration || -1;
89+
}
90+
91+
});

app/services/metrics.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from 'ember-appmetrics/services/metrics';

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"name": "ember-appmetrics",
3-
"version": "0.0.0",
3+
"version": "0.1.0",
44
"description": "The default blueprint for ember-cli addons.",
55
"keywords": [
66
"ember-addon"
77
],
88
"license": "MIT",
9-
"author": "",
9+
"author": "Gokul Kathirvel (@_gokatz)",
1010
"directories": {
1111
"doc": "doc",
1212
"test": "tests"
1313
},
14-
"repository": "",
14+
"repository": "https://github.com/gokatz/ember-appmetrics",
1515
"scripts": {
1616
"build": "ember build",
1717
"start": "ember server",
@@ -40,7 +40,6 @@
4040
"ember-export-application-global": "^1.0.5",
4141
"ember-load-initializers": "^0.5.1",
4242
"ember-resolver": "^2.0.3",
43-
"ember-welcome-page": "^1.0.3",
4443
"loader.js": "^4.0.10"
4544
},
4645
"engines": {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { moduleFor, test } from 'ember-qunit';
2+
3+
moduleFor('service:metrics', 'Unit | Service | metrics', {
4+
// Specify the other units that are required for this test.
5+
// needs: ['service:foo']
6+
});
7+
8+
// Replace this with your real tests.
9+
test('it exists', function(assert) {
10+
let service = this.subject();
11+
assert.ok(service);
12+
});

0 commit comments

Comments
 (0)