measure-fn 3.8.1 → 3.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.test.ts +40 -0
- package/index.ts +15 -12
- package/package.json +33 -33
package/index.test.ts
CHANGED
|
@@ -385,6 +385,46 @@ describe("createMeasure (scoped)", () => {
|
|
|
385
385
|
expect(out.logs[2]).toBe("[b:a] ... y");
|
|
386
386
|
expect(out.logs[4]).toBe("[a:b] ... z");
|
|
387
387
|
});
|
|
388
|
+
|
|
389
|
+
test("scoped maxResultLength truncates results", () => {
|
|
390
|
+
const s = createMeasure("s", { maxResultLength: 15 });
|
|
391
|
+
s.resetCounter();
|
|
392
|
+
const out = captureConsole();
|
|
393
|
+
s.measureSync("op", () => ({ data: "x".repeat(50) }));
|
|
394
|
+
out.restore();
|
|
395
|
+
expect(out.logs[0]).toContain("…");
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
test("scoped maxResultLength inherits to children", () => {
|
|
399
|
+
const s = createMeasure("s", { maxResultLength: 15 });
|
|
400
|
+
s.resetCounter();
|
|
401
|
+
const out = captureConsole();
|
|
402
|
+
s.measureSync("parent", (m) => {
|
|
403
|
+
m("child", () => ({ data: "x".repeat(50) }));
|
|
404
|
+
return 1;
|
|
405
|
+
});
|
|
406
|
+
out.restore();
|
|
407
|
+
const childLine = out.logs[1];
|
|
408
|
+
expect(childLine).toContain("…");
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
test("per-label maxResultLength overrides scoped", () => {
|
|
412
|
+
const s = createMeasure("s", { maxResultLength: 15 });
|
|
413
|
+
s.resetCounter();
|
|
414
|
+
const out = captureConsole();
|
|
415
|
+
s.measureSync({ label: "op", maxResultLength: 500 }, () => ({ data: "x".repeat(50) }));
|
|
416
|
+
out.restore();
|
|
417
|
+
expect(out.logs[0]).not.toContain("…");
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
test("scoped maxResultLength works with async measure", async () => {
|
|
421
|
+
const s = createMeasure("s", { maxResultLength: 15 });
|
|
422
|
+
s.resetCounter();
|
|
423
|
+
const out = captureConsole();
|
|
424
|
+
await s.measure("op", async () => ({ data: "x".repeat(50) }));
|
|
425
|
+
out.restore();
|
|
426
|
+
expect(out.logs[1]).toContain("…");
|
|
427
|
+
});
|
|
388
428
|
});
|
|
389
429
|
|
|
390
430
|
// ─── measure.retry ───────────────────────────────────────────────────
|
package/index.ts
CHANGED
|
@@ -273,10 +273,13 @@ export const resetCounter = () => {
|
|
|
273
273
|
globalRootCounter = 0;
|
|
274
274
|
};
|
|
275
275
|
|
|
276
|
-
|
|
276
|
+
export type ScopeOpts = {
|
|
277
|
+
maxResultLength?: number;
|
|
278
|
+
};
|
|
277
279
|
|
|
278
|
-
const createMeasureImpl = (prefix?: string, counterRef?: { value: number }) => {
|
|
280
|
+
const createMeasureImpl = (prefix?: string, counterRef?: { value: number }, scopeOpts?: ScopeOpts) => {
|
|
279
281
|
const counter = counterRef ?? { get value() { return globalRootCounter; }, set value(v) { globalRootCounter = v; } };
|
|
282
|
+
const scopeMaxLen = scopeOpts?.maxResultLength;
|
|
280
283
|
let _lastError: unknown = null;
|
|
281
284
|
|
|
282
285
|
const _measureInternal = async <U>(
|
|
@@ -295,8 +298,8 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }) => {
|
|
|
295
298
|
const localMaxLen = extractMaxResultLength(actionInternal);
|
|
296
299
|
const effectiveMaxLen = localMaxLen ?? inheritedMaxLen;
|
|
297
300
|
|
|
298
|
-
const currentId = toAlpha(parentIdChain.pop() ?? 0);
|
|
299
|
-
const fullIdChain = [...parentIdChain, currentId];
|
|
301
|
+
const currentId = toAlpha(Number(parentIdChain.pop() ?? 0));
|
|
302
|
+
const fullIdChain: string[] = [...parentIdChain.map(String), currentId];
|
|
300
303
|
const idStr = fullIdChain.join('-');
|
|
301
304
|
|
|
302
305
|
emit({
|
|
@@ -346,7 +349,7 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }) => {
|
|
|
346
349
|
actionInternal: string | object,
|
|
347
350
|
parentIdChain: (string | number)[],
|
|
348
351
|
depth: number,
|
|
349
|
-
_onError?: undefined,
|
|
352
|
+
_onError?: ((error: unknown) => any) | undefined,
|
|
350
353
|
inheritedMaxLen?: number
|
|
351
354
|
): U | null => {
|
|
352
355
|
const start = performance.now();
|
|
@@ -357,8 +360,8 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }) => {
|
|
|
357
360
|
const localMaxLen = extractMaxResultLength(actionInternal);
|
|
358
361
|
const effectiveMaxLen = localMaxLen ?? inheritedMaxLen;
|
|
359
362
|
|
|
360
|
-
const currentId = toAlpha(parentIdChain.pop() ?? 0);
|
|
361
|
-
const fullIdChain = [...parentIdChain, currentId];
|
|
363
|
+
const currentId = toAlpha(Number(parentIdChain.pop() ?? 0));
|
|
364
|
+
const fullIdChain: string[] = [...parentIdChain.map(String), currentId];
|
|
362
365
|
const idStr = fullIdChain.join('-');
|
|
363
366
|
|
|
364
367
|
if (hasNested) {
|
|
@@ -394,7 +397,7 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }) => {
|
|
|
394
397
|
arg3?: (error: unknown) => any
|
|
395
398
|
): Promise<T | null> => {
|
|
396
399
|
if (typeof arg2 === 'function') {
|
|
397
|
-
return _measureInternal(arg2 as any, arg1, [counter.value++], 0, arg3) as Promise<T | null>;
|
|
400
|
+
return _measureInternal(arg2 as any, arg1, [counter.value++], 0, arg3, scopeMaxLen) as Promise<T | null>;
|
|
398
401
|
} else {
|
|
399
402
|
const currentId = toAlpha(counter.value++);
|
|
400
403
|
emit({
|
|
@@ -502,7 +505,7 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }) => {
|
|
|
502
505
|
const results: (R | null)[] = [];
|
|
503
506
|
for (let i = 0; i < items.length; i++) {
|
|
504
507
|
try {
|
|
505
|
-
results.push(await fn(items[i]
|
|
508
|
+
results.push(await fn(items[i]!, i));
|
|
506
509
|
} catch {
|
|
507
510
|
results.push(null);
|
|
508
511
|
}
|
|
@@ -539,7 +542,7 @@ const createMeasureImpl = (prefix?: string, counterRef?: { value: number }) => {
|
|
|
539
542
|
arg2?: ((measure: MeasureSyncFn) => T)
|
|
540
543
|
): T | null => {
|
|
541
544
|
if (typeof arg2 === 'function') {
|
|
542
|
-
return _measureInternalSync(arg2, arg1, [counter.value++], 0) as T | null;
|
|
545
|
+
return _measureInternalSync(arg2, arg1, [counter.value++], 0, undefined, scopeMaxLen) as T | null;
|
|
543
546
|
} else {
|
|
544
547
|
const currentId = toAlpha(counter.value++);
|
|
545
548
|
emit({
|
|
@@ -595,9 +598,9 @@ export const measureSync = globalInstance.measureSync;
|
|
|
595
598
|
|
|
596
599
|
// ─── Scoped instances ────────────────────────────────────────────────
|
|
597
600
|
|
|
598
|
-
export const createMeasure = (scopePrefix: string) => {
|
|
601
|
+
export const createMeasure = (scopePrefix: string, opts?: ScopeOpts) => {
|
|
599
602
|
const scopeCounter = { value: 0 };
|
|
600
|
-
const scoped = createMeasureImpl(scopePrefix, scopeCounter);
|
|
603
|
+
const scoped = createMeasureImpl(scopePrefix, scopeCounter, opts);
|
|
601
604
|
return {
|
|
602
605
|
...scoped,
|
|
603
606
|
resetCounter: () => { scopeCounter.value = 0; },
|
package/package.json
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "measure-fn",
|
|
3
|
-
"module": "index.ts",
|
|
4
|
-
"main": "./index.ts",
|
|
5
|
-
"types": "./index.ts",
|
|
6
|
-
"version": "3.
|
|
7
|
-
"type": "module",
|
|
8
|
-
"private": false,
|
|
9
|
-
"description": "Zero-dependency function performance measurement with hierarchical logging",
|
|
10
|
-
"keywords": [
|
|
11
|
-
"measure",
|
|
12
|
-
"performance",
|
|
13
|
-
"timing",
|
|
14
|
-
"logging",
|
|
15
|
-
"tracing",
|
|
16
|
-
"hierarchical"
|
|
17
|
-
],
|
|
18
|
-
"license": "MIT",
|
|
19
|
-
"repository": {
|
|
20
|
-
"type": "git",
|
|
21
|
-
"url": "https://github.com/7flash/measure-fn"
|
|
22
|
-
},
|
|
23
|
-
"devDependencies": {
|
|
24
|
-
"@types/bun": "latest"
|
|
25
|
-
},
|
|
26
|
-
"peerDependencies": {
|
|
27
|
-
"typescript": "^5"
|
|
28
|
-
},
|
|
29
|
-
"scripts": {
|
|
30
|
-
"test": "bun test",
|
|
31
|
-
"example": "bun run example.ts"
|
|
32
|
-
}
|
|
33
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "measure-fn",
|
|
3
|
+
"module": "index.ts",
|
|
4
|
+
"main": "./index.ts",
|
|
5
|
+
"types": "./index.ts",
|
|
6
|
+
"version": "3.10.0",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"private": false,
|
|
9
|
+
"description": "Zero-dependency function performance measurement with hierarchical logging",
|
|
10
|
+
"keywords": [
|
|
11
|
+
"measure",
|
|
12
|
+
"performance",
|
|
13
|
+
"timing",
|
|
14
|
+
"logging",
|
|
15
|
+
"tracing",
|
|
16
|
+
"hierarchical"
|
|
17
|
+
],
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/7flash/measure-fn"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/bun": "latest"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"typescript": "^5"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"test": "bun test",
|
|
31
|
+
"example": "bun run example.ts"
|
|
32
|
+
}
|
|
33
|
+
}
|