-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_js.cjs
More file actions
139 lines (114 loc) · 4.73 KB
/
benchmark_js.cjs
File metadata and controls
139 lines (114 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const fs = require('fs');
function getMemoryUsage() {
return process.memoryUsage().rss / 1024 / 1024;
}
class ChatbotFlowNavigatorJS {
constructor(nodes, edges) {
this.nodesIndex = new Map();
this.edgesBySource = new Map();
for (const node of nodes) {
this.nodesIndex.set(node.id, node);
}
for (const edge of edges) {
if (!this.edgesBySource.has(edge.source)) {
this.edgesBySource.set(edge.source, []);
}
this.edgesBySource.get(edge.source).push(edge);
}
}
findNextNode(currentNodeId, criteria) {
const outgoingEdges = this.edgesBySource.get(currentNodeId) || [];
for (const edge of outgoingEdges) {
if (this.matchesCriteria(edge, criteria)) {
return this.nodesIndex.get(edge.target) || null;
}
}
return null;
}
matchesCriteria(edge, criteria) {
if (criteria.button_type && edge.sourceHandle && edge.sourceHandle.includes(criteria.button_type)) {
return true;
}
if (criteria.target_contains && edge.target && edge.target.includes(criteria.target_contains)) {
return true;
}
return false;
}
}
function benchmarkJavaScript(nodesFile, edgesFile, datasetName) {
console.log(`\n🚀 JAVASCRIPT BENCHMARK - ${datasetName}`);
console.log('='.repeat(70));
if (global.gc) global.gc();
const initialMemory = getMemoryUsage();
const startLoad = performance.now();
const nodesData = JSON.parse(fs.readFileSync(nodesFile, 'utf-8'));
const edgesData = JSON.parse(fs.readFileSync(edgesFile, 'utf-8'));
const loadTime = performance.now() - startLoad;
const memoryAfterLoad = getMemoryUsage();
const memoryForData = memoryAfterLoad - initialMemory;
console.log(`📁 Datos cargados: ${nodesData.length.toLocaleString()} nodos, ${edgesData.length.toLocaleString()} edges`);
console.log(`⏱️ Tiempo de carga: ${loadTime.toFixed(3)}ms`);
console.log(`💾 Memoria para datos JSON: ${memoryForData.toFixed(2)} MB`);
const startInit = performance.now();
const navigator = new ChatbotFlowNavigatorJS(nodesData, edgesData);
const initTime = performance.now() - startInit;
const memoryAfterInit = getMemoryUsage();
const memoryForStructures = memoryAfterInit - memoryAfterLoad;
const totalMemory = memoryAfterInit - initialMemory;
console.log(`⏱️ Tiempo de inicialización: ${initTime.toFixed(3)}ms`);
console.log(`💾 Memoria para estructuras Map: ${memoryForStructures.toFixed(2)} MB`);
console.log(`💾 Memoria total: ${totalMemory.toFixed(2)} MB`);
console.log(`💾 Eficiencia: ${(totalMemory / nodesData.length * 1000).toFixed(3)} KB por nodo`);
// Benchmark de búsquedas
const iterations = 10000;
const testCases = [
{ name: "Búsqueda por QUICK_REPLY", criteria: { button_type: "QUICK_REPLY" } },
{ name: "Búsqueda combinada", criteria: { button_type: "POSTBACK", target_contains: "node-" } }
];
for (const testCase of testCases) {
let totalTime = 0;
let successes = 0;
for (let i = 0; i < iterations; i++) {
const start = performance.now();
const result = navigator.findNextNode("node-START", testCase.criteria);
totalTime += performance.now() - start;
if (result) successes++;
}
const avgTime = (totalTime / iterations) * 1000;
console.log(`🔍 ${testCase.name}: ${avgTime.toFixed(3)} μs promedio, ${(successes/iterations*100).toFixed(1)}% éxito`);
}
return {
dataset: datasetName,
nodes: nodesData.length,
edges: edgesData.length,
memory_total: totalMemory,
memory_per_node: totalMemory / nodesData.length * 1000,
init_time: initTime
};
}
const datasets = [
["data/nodes.json", "data/edges.json", "Dataset Original (11 nodos)"],
["data/nodes_1000.json", "data/edges_1000.json", "Dataset Mediano (1K nodos)"],
["data/nodes_5000.json", "data/edges_5000.json", "Dataset Grande (5K nodos)"],
["data/nodes_10000.json", "data/edges_10000.json", "Dataset Muy Grande (10K nodos)"]
];
const results = [];
for (const [nodesFile, edgesFile, name] of datasets) {
try {
const result = benchmarkJavaScript(nodesFile, edgesFile, name);
results.push(result);
} catch (error) {
console.log(`⚠️ Error: ${error.message}`);
}
}
console.log(`\n🏆 RESUMEN COMPARATIVO - JAVASCRIPT`);
console.log('='.repeat(80));
console.log('Dataset'.padEnd(30) + 'Nodos'.padEnd(8) + 'Memoria'.padEnd(10) + 'KB/nodo'.padEnd(10) + 'Init(ms)'.padEnd(10));
console.log('-'.repeat(80));
for (const result of results) {
console.log(result.dataset.padEnd(30) +
result.nodes.toLocaleString().padEnd(8) +
result.memory_total.toFixed(2).padEnd(10) +
result.memory_per_node.toFixed(3).padEnd(10) +
result.init_time.toFixed(3).padEnd(10));
}