@pi-archimedes/core 1.7.1 → 1.8.1

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.
Files changed (2) hide show
  1. package/package.json +3 -2
  2. package/src/profiler.ts +49 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-archimedes/core",
3
- "version": "1.7.1",
3
+ "version": "1.8.1",
4
4
  "type": "module",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -17,7 +17,8 @@
17
17
  "./text": "./src/text.ts",
18
18
  "./color": "./src/color.ts",
19
19
  "./config": "./src/config.ts",
20
- "./settings-io": "./src/settings-io.ts"
20
+ "./settings-io": "./src/settings-io.ts",
21
+ "./profiler": "./src/profiler.ts"
21
22
  },
22
23
  "peerDependencies": {
23
24
  "@earendil-works/pi-coding-agent": ">=0.1.0",
@@ -0,0 +1,49 @@
1
+ /** Lightweight startup profiler — prints per-package timings when PI_TIMING=1. */
2
+
3
+ const ENABLED = process.env.PI_TIMING === "1";
4
+
5
+ interface TimingState {
6
+ baseline: number;
7
+ entries: Array<{ label: string; ms: number }>;
8
+ }
9
+
10
+ // Module-level accumulator (survives hot-reloads via Symbol key)
11
+ const TIMINGS_KEY = Symbol.for("archimedes:timings");
12
+ declare const globalThis: typeof global & Record<typeof TIMINGS_KEY, TimingState>;
13
+
14
+ function getState(): TimingState {
15
+ if (!globalThis[TIMINGS_KEY]) {
16
+ globalThis[TIMINGS_KEY] = { baseline: Date.now(), entries: [] };
17
+ }
18
+ return globalThis[TIMINGS_KEY];
19
+ }
20
+
21
+ // ── Public API — zero-cost when disabled (early-return when PI_TIMING unset) ──
22
+
23
+ export function time(label: string): void {
24
+ if (!ENABLED) return;
25
+ const state = getState();
26
+ state.entries.push({ label, ms: Date.now() - state.baseline });
27
+ }
28
+
29
+ /** Reset timings for a fresh session (call on reload). */
30
+ export function reset(): void {
31
+ if (!ENABLED) return;
32
+ globalThis[TIMINGS_KEY] = { baseline: Date.now(), entries: [] };
33
+ }
34
+
35
+ /** Print accumulated timings to stderr. Call once at session_shutdown or on demand. */
36
+ export function print(): void {
37
+ if (!ENABLED) return;
38
+ const state = getState();
39
+ if (state.entries.length === 0) return;
40
+
41
+ let prevMs = 0;
42
+ console.error("");
43
+ for (const entry of state.entries) {
44
+ const delta = entry.ms - prevMs;
45
+ prevMs = entry.ms;
46
+ console.error(` archimedes: ${entry.label}: +${delta}ms (${entry.ms}ms cumulative)`);
47
+ }
48
+ console.error("".padEnd(60, "-"));
49
+ }