Home / Class/ AstroTimer Class — astro Architecture

AstroTimer Class — astro Architecture

Architecture documentation for the AstroTimer class in timer.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  90d3bda7_4cb5_4581_6649_981fa38bed51["AstroTimer"]
  587108a1_d18f_e006_6243_92250d699327["timer.ts"]
  90d3bda7_4cb5_4581_6649_981fa38bed51 -->|defined in| 587108a1_d18f_e006_6243_92250d699327
  c997bf1a_894b_3b3d_0677_487504009e45["constructor()"]
  90d3bda7_4cb5_4581_6649_981fa38bed51 -->|method| c997bf1a_894b_3b3d_0677_487504009e45
  a887a94f_53d4_d2cd_d7e2_7bf7b8fbe196["start()"]
  90d3bda7_4cb5_4581_6649_981fa38bed51 -->|method| a887a94f_53d4_d2cd_d7e2_7bf7b8fbe196
  46c3f783_b0c5_1290_298e_453955b2ba95["end()"]
  90d3bda7_4cb5_4581_6649_981fa38bed51 -->|method| 46c3f783_b0c5_1290_298e_453955b2ba95
  7bf2af9c_77a8_1e2a_8e68_9c10812398ce["writeStats()"]
  90d3bda7_4cb5_4581_6649_981fa38bed51 -->|method| 7bf2af9c_77a8_1e2a_8e68_9c10812398ce

Relationship Graph

Source Code

packages/astro/src/core/config/timer.ts lines 19–65

export class AstroTimer {
	private enabled: boolean;
	private ongoingTimers = new Map<string, OngoingStat>();
	private stats: Record<string, Stat> = {};

	constructor() {
		this.enabled = !!process.env.ASTRO_TIMER_PATH;
	}

	/**
	 * Start a timer for a scope with a given name.
	 */
	start(name: string) {
		if (!this.enabled) return;
		globalThis.gc?.();
		this.ongoingTimers.set(name, {
			startTime: performance.now(),
			startHeap: process.memoryUsage().heapUsed,
		});
	}

	/**
	 * End a timer for a scope with a given name.
	 */
	end(name: string) {
		if (!this.enabled) return;
		const stat = this.ongoingTimers.get(name);
		if (!stat) return;
		globalThis.gc?.();
		const endHeap = process.memoryUsage().heapUsed;
		this.stats[name] = {
			elapsedTime: performance.now() - stat.startTime,
			heapUsedChange: endHeap - stat.startHeap,
			heapUsedTotal: endHeap,
		};
		this.ongoingTimers.delete(name);
	}

	/**
	 * Write stats to `process.env.ASTRO_TIMER_PATH`
	 */
	writeStats() {
		if (!this.enabled) return;
		// @ts-expect-error
		fs.writeFileSync(process.env.ASTRO_TIMER_PATH, JSON.stringify(this.stats, null, 2));
	}
}

Frequently Asked Questions

What is the AstroTimer class?
AstroTimer is a class in the astro codebase, defined in packages/astro/src/core/config/timer.ts.
Where is AstroTimer defined?
AstroTimer is defined in packages/astro/src/core/config/timer.ts at line 19.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free