-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adding in stats to keyv * adding in stats option * getting errors to work * moving to counts as properties * adding in stats
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 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
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,55 @@ | ||
import EventManager from './event-manager'; | ||
|
||
class StatsManager extends EventManager { | ||
public enabled = true; | ||
|
||
public hits = 0; | ||
public misses = 0; | ||
public sets = 0; | ||
public deletes = 0; | ||
public errors = 0; | ||
|
||
constructor(enabled?: boolean) { | ||
super(); | ||
if (enabled !== undefined) { | ||
this.enabled = enabled; | ||
} | ||
|
||
this.reset(); | ||
} | ||
|
||
hit() { | ||
if (this.enabled) { | ||
this.hits++; | ||
} | ||
} | ||
|
||
miss() { | ||
if (this.enabled) { | ||
this.misses++; | ||
} | ||
} | ||
|
||
set() { | ||
if (this.enabled) { | ||
this.sets++; | ||
} | ||
} | ||
|
||
delete() { | ||
if (this.enabled) { | ||
this.deletes++; | ||
} | ||
} | ||
|
||
reset() { | ||
this.hits = 0; | ||
this.misses = 0; | ||
this.sets = 0; | ||
this.deletes = 0; | ||
this.errors = 0; | ||
} | ||
} | ||
|
||
export default StatsManager; | ||
module.exports = StatsManager; |
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,54 @@ | ||
import test from 'ava'; | ||
import KeyvStatsManager from '../src/stats-manager'; | ||
|
||
test('will initialize with correct stats at zero', t => { | ||
const stats = new KeyvStatsManager(); | ||
t.is(stats.hits, 0); | ||
}); | ||
|
||
test('will increment hits', t => { | ||
const stats = new KeyvStatsManager(); | ||
stats.hit(); | ||
t.is(stats.hits, 1); | ||
}); | ||
|
||
test('will increment misses', t => { | ||
const stats = new KeyvStatsManager(); | ||
stats.miss(); | ||
t.is(stats.misses, 1); | ||
}); | ||
|
||
test('will increment sets', t => { | ||
const stats = new KeyvStatsManager(); | ||
stats.set(); | ||
t.is(stats.sets, 1); | ||
}); | ||
|
||
test('will increment deletes', t => { | ||
const stats = new KeyvStatsManager(); | ||
stats.delete(); | ||
t.is(stats.deletes, 1); | ||
}); | ||
|
||
test('will reset stats', t => { | ||
const stats = new KeyvStatsManager(); | ||
stats.hit(); | ||
stats.miss(); | ||
stats.set(); | ||
stats.delete(); | ||
t.is(stats.hits, 1); | ||
t.is(stats.misses, 1); | ||
t.is(stats.sets, 1); | ||
t.is(stats.deletes, 1); | ||
stats.reset(); | ||
t.is(stats.hits, 0); | ||
t.is(stats.misses, 0); | ||
t.is(stats.sets, 0); | ||
t.is(stats.deletes, 0); | ||
}); | ||
|
||
test('will not increment hits if disabled', t => { | ||
const stats = new KeyvStatsManager(false); | ||
stats.hit(); | ||
t.is(stats.hits, 0); | ||
}); |
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