Skip to content

Commit

Permalink
Merge pull request #2 from gokatz/patch_v0.1.0_release
Browse files Browse the repository at this point in the history
v0.1.0 release
  • Loading branch information
gokatz authored Mar 23, 2017
2 parents cf09e77 + 50e3caa commit b3ce01a
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 11 deletions.
51 changes: 44 additions & 7 deletions README.md
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/).
91 changes: 91 additions & 0 deletions addon/services/metrics.js
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;
}

});
1 change: 1 addition & 0 deletions app/services/metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-appmetrics/services/metrics';
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "ember-appmetrics",
"version": "0.0.0",
"version": "0.1.0",
"description": "The default blueprint for ember-cli addons.",
"keywords": [
"ember-addon"
],
"license": "MIT",
"author": "",
"author": "Gokul Kathirvel (@_gokatz)",
"directories": {
"doc": "doc",
"test": "tests"
},
"repository": "",
"repository": "https://github.com/gokatz/ember-appmetrics",
"scripts": {
"build": "ember build",
"start": "ember server",
Expand Down Expand Up @@ -40,7 +40,6 @@
"ember-export-application-global": "^1.0.5",
"ember-load-initializers": "^0.5.1",
"ember-resolver": "^2.0.3",
"ember-welcome-page": "^1.0.3",
"loader.js": "^4.0.10"
},
"engines": {
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/services/metrics-test.js
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);
});

0 comments on commit b3ce01a

Please sign in to comment.