Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/backend/src/altNodes/jsonNodeConversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PluginSettings } from "types";
import { variableToColorName } from "../tailwind/conversionTables";
import { HasGeometryTrait, Node, Paint } from "../api_types";
import { calculateRectangleFromBoundingBox } from "../common/commonPosition";
import { bench } from "../log";

// Performance tracking counters
export let getNodeByIdAsyncTime = 0;
Expand Down Expand Up @@ -547,7 +548,7 @@ export const nodesToJSON = async (

console.log("[debug] initial nodeJson", { ...nodes[0] });

console.log(
bench(
`[benchmark][inside nodesToJSON] JSON_REST_V1 export: ${Date.now() - exportJsonStart}ms`,
);

Expand All @@ -574,7 +575,7 @@ export const nodesToJSON = async (
}
}

console.log(
bench(
`[benchmark][inside nodesToJSON] Process node pairs: ${Date.now() - processNodesStart}ms`,
);

Expand Down
17 changes: 9 additions & 8 deletions packages/backend/src/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
processColorVariablesTime,
resetPerformanceCounters,
} from "./altNodes/jsonNodeConversion";
import { bench } from "./log";

export const run = async (settings: PluginSettings) => {
resetPerformanceCounters();
Expand All @@ -44,7 +45,7 @@ export const run = async (settings: PluginSettings) => {
console.log("convertedSelection", convertedSelection);
} else {
convertedSelection = await nodesToJSON(selection, settings);
console.log(`[benchmark] nodesToJSON: ${Date.now() - nodeToJSONStart}ms`);
bench(`[benchmark] nodesToJSON: ${Date.now() - nodeToJSONStart}ms`);
console.log("nodeJson", convertedSelection);
}

Expand All @@ -59,38 +60,38 @@ export const run = async (settings: PluginSettings) => {

const convertToCodeStart = Date.now();
const code = await convertToCode(convertedSelection, settings);
console.log(
bench(
`[benchmark] convertToCode: ${Date.now() - convertToCodeStart}ms`,
);

const generatePreviewStart = Date.now();
const htmlPreview = await generateHTMLPreview(convertedSelection, settings);
console.log(
bench(
`[benchmark] generateHTMLPreview: ${Date.now() - generatePreviewStart}ms`,
);

const colorPanelStart = Date.now();
const colors = retrieveGenericSolidUIColors(framework);
const gradients = retrieveGenericLinearGradients(framework);
console.log(
bench(
`[benchmark] color and gradient panel: ${Date.now() - colorPanelStart}ms`,
);
console.log(
bench(
`[benchmark] total generation time: ${Date.now() - nodeToJSONStart}ms`,
);

// Log performance statistics
console.log(
bench(
`[benchmark] getNodeByIdAsync: ${getNodeByIdAsyncTime}ms (${getNodeByIdAsyncCalls} calls, avg: ${(getNodeByIdAsyncTime / getNodeByIdAsyncCalls || 1).toFixed(2)}ms)`,
);
console.log(
bench(
`[benchmark] getStyledTextSegments: ${getStyledTextSegmentsTime}ms (${getStyledTextSegmentsCalls} calls, avg: ${
getStyledTextSegmentsCalls > 0
? (getStyledTextSegmentsTime / getStyledTextSegmentsCalls).toFixed(2)
: 0
}ms)`,
);
console.log(
bench(
`[benchmark] processColorVariables: ${processColorVariablesTime}ms (${processColorVariablesCalls} calls, avg: ${
processColorVariablesCalls > 0
? (processColorVariablesTime / processColorVariablesCalls).toFixed(2)
Expand Down
40 changes: 40 additions & 0 deletions packages/backend/src/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const enum LogLevel {
None = 0,
Benchmark,
Error,
Warn,
Info,
Log,
}

let loglevel = LogLevel.None;

const kConsoleLog = console.log;
const kConsoleInfo = console.info;
const kConsoleError = console.error;
const kConsoleWarn = console.warn;

export const bench = (message?: any, ...optionalParams: any[]) => {
if (loglevel < LogLevel.Benchmark) return;
kConsoleLog(message, optionalParams);
}

console.log = function(message?: any, ...optionalParams: any[]) {
if (loglevel < LogLevel.Log) return;
kConsoleLog(message, optionalParams);
}

console.info = function(message?: any, ...optionalParams: any[]) {
if (loglevel < LogLevel.Info) return;
kConsoleInfo(message, optionalParams);
}

console.warn = function(message?: any, ...optionalParams: any[]) {
if (loglevel < LogLevel.Warn) return;
kConsoleWarn(message, optionalParams);
}

console.error = function(message?: any, ...optionalParams: any[]) {
if (loglevel < LogLevel.Error) return;
kConsoleError(message, optionalParams);
}