Skip to content

Commit

Permalink
Better API.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Nov 10, 2015
1 parent f94f270 commit a2d1f6f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ See [example.js][example].

* <a href="#constructor"><code><b>loopbench()</b></code></a>
* <a href="#delay"><code>instance.<b>delay</b></code></a>
* <a href="#maxDelay"><code>instance.<b>maxDelay</b></code></a>
* <a href="#limit"><code>instance.<b>limit</b></code></a>
* <a href="#overLimit"><code>instance.<b>overLimit</b></code></a>
* <a href="#stop"><code>instance.<b>stop()</b></code></a>

Expand All @@ -43,14 +43,14 @@ Creates a new instance of loopbench.
Options:

* `sampleInterval`: the interval at which the eventLoop should be
sampled
* `maxDelay`: the maximum amount of delay that is tollerated before
[`overLimit`](#overlimit) becomes true, and the `load` event is
emitted
sampled, defaults to `5`.
* `limit`: the maximum amount of delay that is tollerated before
[`overLimit`](#overLimit) becomes true, and the `load` event is
emitted, defaults to `42`.

Events:

* `load`, emitted when `instance.delay > instance.maxDelay`
* `load`, emitted when `instance.delay > instance.limit`

-------------------------------------------------------
<a name="delay"></a>
Expand All @@ -60,8 +60,8 @@ The delay in milliseconds (and fractions) from the expected run.
It might be negative (in older nodes).

-------------------------------------------------------
<a name="maxDelay"></a>
### instance.maxDelay
<a name="limit"></a>
### instance.limit

The maximum amount of delay that is tollerated before
[`overLimit`](#overlimit) becomes true, and the `load` event is
Expand All @@ -71,7 +71,7 @@ emitted.
<a name="overLimit"></a>
### instance.overLimit

Is `true` if the `instance.delay > instance.maxDelay`.
Is `true` if the `instance.delay > instance.limit`.

-------------------------------------------------------
<a name="stop"></a>
Expand Down
6 changes: 3 additions & 3 deletions loopbench.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var xtend = require('xtend')
var EE = require('events').EventEmitter

var defaults = {
maxDelay: 42,
limit: 42,
sampleInterval: 5
}

Expand All @@ -17,7 +17,7 @@ function loopbench (opts) {

result.delay = 0
result.sampleInterval = opts.sampleInterval
result.maxDelay = opts.maxDelay
result.limit = opts.limit
result.stop = clearInterval.bind(null, timer)

var last = now()
Expand All @@ -29,7 +29,7 @@ function loopbench (opts) {
result.delay = toCheck - last - result.sampleInterval
last = toCheck

if (result.delay > result.maxDelay) {
if (result.delay > result.limit) {
result.overLimit = true
result.emit('load')
} else {
Expand Down
10 changes: 5 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function now () {
test('bench the event loop', function (t) {
var instance = loopbench()

t.equal(instance.maxDelay, 42, 'default maxDelay matches')
t.equal(instance.limit, 42, 'default limit matches')
t.equal(instance.sampleInterval, 5, 'default sampleInterval matches')
t.equal(instance.delay, 0, 'delay starts at zero')

Expand All @@ -25,23 +25,23 @@ test('bench the event loop', function (t) {
sleep(4)
setImmediate(function () {
console.log('delay', instance.delay)
t.ok(instance.delay < 6, 'delay must be less than 6 ms')
t.ok(instance.delay < 7, 'delay must be less than 7 ms')
t.ok(instance.delay > -1, 'delay must be greater than -1 ms')
t.notOk(instance.overLimit, 'must not be overLimit')
instance.stop()
t.end()
})
})

test('emits a "load" event when the maxEventLoopDelay is reached', function (t) {
test('emits a "load" event when the limit is reached', function (t) {
t.plan(7)

var instance = loopbench({
sampleInterval: 1, // ms
maxDelay: 10 // ms
limit: 10 // ms
})

t.equal(instance.maxDelay, 10, 'maxDelay matches')
t.equal(instance.limit, 10, 'limit matches')
t.equal(instance.sampleInterval, 1, 'sampleInterval matches')

t.equal(instance.delay, 0, 'delay starts at zero')
Expand Down

0 comments on commit a2d1f6f

Please sign in to comment.