UNPKG

traceperf

Version:

High-performance function execution tracking and monitoring for Node.js

104 lines 3.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AsciiArtGenerator = void 0; const constants_1 = require("../core/constants"); const timing_1 = require("../utils/timing"); /** * ASCII art generator for execution flow charts */ class AsciiArtGenerator { /** * Create a new AsciiArtGenerator instance * * @param options - Generator options */ constructor(options = {}) { var _a; this._boxWidth = (_a = options.boxWidth) !== null && _a !== void 0 ? _a : 30; } /** * Generate a flow chart for a sequence of function executions * * @param executions - Array of execution data * @returns ASCII art flow chart */ generateFlowChart(executions) { let chart = ''; for (let i = 0; i < executions.length; i++) { const execution = executions[i]; // Generate box for function chart += this.generateFunctionBox(execution); // Add arrow to next function if not the last one if (i < executions.length - 1) { chart += this.generateArrow(executions[i + 1].level - execution.level); } } return chart; } /** * Generate a box for a function execution * * @param execution - The execution data * @returns ASCII art box */ generateFunctionBox(execution) { const { name, duration, isSlow, memoryUsage } = execution; // Calculate padding for the function name const nameLength = name.length; const padding = Math.max(0, this._boxWidth - nameLength - 2); const leftPadding = Math.floor(padding / 2); const rightPadding = padding - leftPadding; // Generate the box let box = ''; // Top border box += constants_1.BOX_CHARS.topLeft + constants_1.BOX_CHARS.horizontal.repeat(this._boxWidth) + constants_1.BOX_CHARS.topRight + '\n'; // Function name box += constants_1.BOX_CHARS.vertical + ' '.repeat(leftPadding) + name + ' '.repeat(rightPadding) + constants_1.BOX_CHARS.vertical; // Performance metrics box += ` ${constants_1.PERFORMANCE_ICONS.timer} ${(0, timing_1.formatDuration)(duration)}`; if (isSlow) { box += ` ${constants_1.PERFORMANCE_ICONS.slow} SLOW`; } if (memoryUsage !== undefined) { box += ` ${constants_1.PERFORMANCE_ICONS.memory} ${this.formatMemory(memoryUsage)}`; } box += '\n'; // Bottom border box += constants_1.BOX_CHARS.bottomLeft + constants_1.BOX_CHARS.horizontal.repeat(this._boxWidth) + constants_1.BOX_CHARS.bottomRight + '\n'; return box; } /** * Generate an arrow between function boxes * * @param levelDiff - Difference in nesting level * @returns ASCII art arrow */ generateArrow(levelDiff) { let arrow = ''; // Indent based on level difference const indent = ' '.repeat(Math.max(0, levelDiff)); // Vertical line arrow += indent + ' '.repeat(this._boxWidth / 2) + constants_1.BOX_CHARS.vertical + '\n'; // Arrow arrow += indent + ' '.repeat(this._boxWidth / 2) + constants_1.BOX_CHARS.downArrow + '\n'; return arrow; } /** * Format memory usage in a human-readable format * * @param bytes - Memory usage in bytes * @returns Formatted memory usage */ formatMemory(bytes) { const units = ['B', 'KB', 'MB', 'GB']; let size = bytes; let unitIndex = 0; while (size >= 1024 && unitIndex < units.length - 1) { size /= 1024; unitIndex++; } return `${size.toFixed(2)}${units[unitIndex]}`; } } exports.AsciiArtGenerator = AsciiArtGenerator; //# sourceMappingURL=ascii.js.map