-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from gokatz/patch_v0.1.0_release
v0.1.0 release
- Loading branch information
Showing
5 changed files
with
151 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,64 @@ | ||
# ember-appmetrics | ||
|
||
This README outlines the details of collaborating on this Ember addon. | ||
[![Build Status](https://travis-ci.org/gokatz/ember-appmetrics.svg?branch=master)](https://travis-ci.org/gokatz/ember-appmetrics) | ||
|
||
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. | ||
|
||
## Installation | ||
For Ember CLI >= `0.2.3`: | ||
```shell | ||
ember install ember-appmetrics | ||
``` | ||
For Ember CLI < `0.2.3`: | ||
```shell | ||
ember install:addon ember-appmetrics | ||
``` | ||
|
||
## Compatibility | ||
This addon is tested against the latest stable Ember version. | ||
|
||
## Usage | ||
|
||
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. | ||
|
||
Addon provides three API to measure the performace of a given period. | ||
- `start` : need to call this api with an event name as argument to mark the starting point | ||
- `end` : need to call this api with an event name as argument to mark the ending point | ||
- `measure` : will return the calculated time for the given event | ||
|
||
```js | ||
this.get('metric').start('accounts_page'); | ||
Ember.run.scheduleOnce('afterRender', () => { | ||
this.get('metric').end('accounts_page'); | ||
let accountsPageRenderDuration = this.get('metric').measure('accounts_page'); | ||
console.log(accountsPageRenderDuration); // will return the duration to for this render performance in milliseconds. | ||
}); | ||
``` | ||
|
||
## Browser support | ||
|
||
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. | ||
|
||
PS: In Safari, the User Timing API (performance.mark()) is not available, so the DevTools timeline will not be annotated with marks. | ||
|
||
## Installation | ||
|
||
* `git clone <repository-url>` this repository | ||
* `cd ember-appmetrics` | ||
* `git clone` this repository | ||
* `npm install` | ||
* `bower install` | ||
|
||
## Running | ||
|
||
* `ember serve` | ||
* Visit your app at [http://localhost:4200](http://localhost:4200). | ||
* `ember server` | ||
* Visit your app at http://localhost:4200. | ||
|
||
## Running Tests | ||
|
||
* `npm test` (Runs `ember try:each` to test your addon against multiple Ember versions) | ||
* `ember test` | ||
* `ember test --server` | ||
|
||
## Building | ||
|
||
* `ember build` | ||
|
||
For more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/). | ||
For more information on using ember-cli, visit [http://www.ember-cli.com/](http://www.ember-cli.com/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import Ember from 'ember'; | ||
|
||
const { typeOf } = Ember; | ||
|
||
export default Ember.Service.extend({ | ||
isMarkSupportedBrowser: true, | ||
isNowSupportedBrowser: true, | ||
|
||
secondaryPerformanceObj: {}, | ||
|
||
init() { | ||
if (!(window.performance && window.performance.mark)) { | ||
console.log('Performance.mark is not supported in this browser. Hence falling back to performance.now()'); | ||
this.set('isMarkSupportedBrowser', false); | ||
|
||
if (!(window.performance && window.performance.now)) { | ||
console.log('Performance.now is also not supported. Hence falling back to javascript Date API'); | ||
this.set('isNowSupportedBrowser', false); | ||
|
||
} | ||
} | ||
}, | ||
|
||
start(eventName) { | ||
let secondaryPerformanceObj = this.get('secondaryPerformanceObj'); | ||
secondaryPerformanceObj[eventName] = secondaryPerformanceObj[eventName] || {}; | ||
let eventObj = secondaryPerformanceObj[eventName]; | ||
|
||
if (typeOf(eventName) !== 'string') { | ||
throw 'Expected type String for invoking `start`'; | ||
} | ||
|
||
if (this.get('isNowSupportedBrowser')) { | ||
if (this.get('isMarkSupportedBrowser')) { | ||
performance.mark(`mark_${eventName}_start`); | ||
} else { | ||
eventObj.start = window.performance.now(); | ||
} | ||
} else { | ||
eventObj.start = new Date().valueOf(); | ||
} | ||
|
||
return; | ||
}, | ||
|
||
end(eventName) { | ||
let secondaryPerformanceObj = this.get('secondaryPerformanceObj'); | ||
|
||
secondaryPerformanceObj[eventName] = secondaryPerformanceObj[eventName] || {}; | ||
let eventObj = secondaryPerformanceObj[eventName]; | ||
|
||
if (typeOf(eventName) !== 'string') { | ||
throw 'Expected type String for invoking `end`'; | ||
} | ||
|
||
if (this.get('isNowSupportedBrowser')) { | ||
let startMark = `mark_${eventName}_start`; | ||
let endMark = `mark_${eventName}_end`; | ||
|
||
if (this.get('isMarkSupportedBrowser')) { | ||
performance.mark(endMark); | ||
performance.measure(eventName, startMark, endMark); | ||
} else { | ||
eventObj.end = window.performance.now(); | ||
} | ||
} else { | ||
eventObj.end = new Date().valueOf(); | ||
} | ||
return; | ||
}, | ||
|
||
measure(eventName) { | ||
let secondaryPerformanceObj = this.get('secondaryPerformanceObj'); | ||
let eventObj = secondaryPerformanceObj[eventName] || {}; | ||
let duration; | ||
|
||
if (this.get('isMarkSupportedBrowser')) { | ||
let perfEntries = performance.getEntriesByName(eventName); | ||
// poping up the last entry pushed into teh array | ||
let entry = perfEntries[perfEntries.length - 1]; | ||
if (entry) { | ||
duration = entry.duration; | ||
} | ||
} else { | ||
duration = eventObj.end - eventObj.start; | ||
} | ||
|
||
return duration || -1; | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'ember-appmetrics/services/metrics'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { moduleFor, test } from 'ember-qunit'; | ||
|
||
moduleFor('service:metrics', 'Unit | Service | metrics', { | ||
// Specify the other units that are required for this test. | ||
// needs: ['service:foo'] | ||
}); | ||
|
||
// Replace this with your real tests. | ||
test('it exists', function(assert) { | ||
let service = this.subject(); | ||
assert.ok(service); | ||
}); |