deno.land / x / hooks@v0.6.5 / test / benchmark.test.ts

benchmark.test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as assert from 'assert';import { hooks, HookContext, NextFunction, middleware } from '../src/';
const CYCLES = 100000;const getRuntime = async (callback: () => Promise<any>) => { const start = Date.now();
for (let i = 0; i < CYCLES; i++) { await callback(); }
return Date.now() - start;}
describe('hook benchmark', () => { const hello = async (name: string, _params: any = {}) => { return `Hello ${name}`; }; let baseline: number; let threshold: number;
before(async () => { baseline = await getRuntime(() => hello('Dave')); threshold = baseline * 25; // TODO might be improved further });
it('empty hook', async () => { const hookHello1 = hooks(hello, middleware([])); const runtime = await getRuntime(() => hookHello1('Dave'));
assert.ok(runtime < threshold, `Runtime is ${runtime}ms, threshold is ${threshold}ms`); });
it('single simple hook', async () => { const hookHello = hooks(hello, middleware([ async (_ctx: HookContext, next: NextFunction) => { await next(); } ])); const runtime = await getRuntime(() => hookHello('Dave'));
assert.ok(runtime < threshold, `Runtime is ${runtime}ms, threshold is ${threshold}ms`); });
it('single hook, withParams and props', async () => { const hookHello = hooks(hello, middleware([ async (_ctx: HookContext, next: NextFunction) => { await next(); } ]).params('name').props({ dave: true }));
const runtime = await getRuntime(() => hookHello('Dave'));
assert.ok(runtime < threshold, `Runtime is ${runtime}ms, threshold is ${threshold}ms`); });});
hooks

Version Info

Tagged at
3 years ago