-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-test-data.mjs
More file actions
265 lines (219 loc) · 8.91 KB
/
generate-test-data.mjs
File metadata and controls
265 lines (219 loc) · 8.91 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env node
/**
* Genera dati di test per stress testing NetMap
*
* Utilizzo:
* node generate-test-data.mjs [devices] [links_per_device]
*
* Esempi:
* node generate-test-data.mjs 200 4 # 200 devices, ~800 links
* node generate-test-data.mjs 500 3 # 500 devices, ~1500 links
*/
import NetMapDB from './libdb.js';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Parametri da CLI
const numDevices = parseInt(process.argv[2]) || 200;
const linksPerDevice = parseInt(process.argv[3]) || 4;
console.log(`🚀 Generazione dati di test:`);
console.log(` - Devices: ${numDevices}`);
console.log(` - Links per device: ~${linksPerDevice}`);
console.log(` - Totale links attesi: ~${numDevices * linksPerDevice}`);
console.log();
// Inizializza DB
const db = new NetMapDB('./netmap.db');
// Helper per generare IP casuali (subnet 10.x.x.x)
function generateIP(index) {
const subnet = 10;
const octet2 = Math.floor(index / 65536) % 256;
const octet3 = Math.floor(index / 256) % 256;
const octet4 = index % 256;
return `${subnet}.${octet2}.${octet3}.${octet4}`;
}
// Helper per generare MAC casuali
function generateMAC() {
const hex = '0123456789ABCDEF';
let mac = '00:50:56'; // VMware OUI prefix
for (let i = 0; i < 3; i++) {
mac += ':' + hex[Math.floor(Math.random() * 16)] + hex[Math.floor(Math.random() * 16)];
}
return mac;
}
// Device types con distribuzione realistica
const deviceTypes = [
{ type: 'l3', prefix: 'l3_sw', weight: 0.05, vendor: 'Huawei', model: 'S6730', level: 0 },
{ type: 'l2', prefix: 'l2_sw', weight: 0.35, vendor: 'Huawei', model: 'S5735', level: 1 },
{ type: 'router', prefix: 'rt', weight: 0.05, vendor: 'Huawei', model: 'NE8000', level: 0 },
{ type: 'ap', prefix: 'ap', weight: 0.45, vendor: 'Ubiquiti', model: 'UAP-AC-PRO', level: 2 },
{ type: 'neighbor', prefix: 'neighbor', weight: 0.10, vendor: 'Unknown', model: 'Unknown', level: 3 },
];
// Funzione per selezionare tipo device basato su peso
function selectDeviceType() {
const rand = Math.random();
let cumulativeWeight = 0;
for (const dt of deviceTypes) {
cumulativeWeight += dt.weight;
if (rand <= cumulativeWeight) return dt;
}
return deviceTypes[deviceTypes.length - 1];
}
// Genera locations realistiche
const locations = [
'Sede Centrale', 'Magazzino', 'Uffici Piano 1', 'Uffici Piano 2', 'Uffici Piano 3',
'Data Center', 'Server Room', 'Reception', 'Sala Riunioni', 'Cafeteria',
'Laboratorio', 'Produzione', 'Logistica', 'R&D', 'IT Room'
];
// ========== GENERAZIONE DEVICES ==========
console.log(`📦 Generando ${numDevices} devices...`);
const deviceIds = [];
const deviceIPs = [];
const startTime = Date.now();
for (let i = 0; i < numDevices; i++) {
const deviceType = selectDeviceType();
const ip = generateIP(i + 1000); // Offset per evitare .0 e .255
const sysname = `${deviceType.prefix}_${String(i + 1).padStart(4, '0')}`;
const location = locations[Math.floor(Math.random() * locations.length)];
const device = {
ip,
sysname,
sysdesc: `${deviceType.vendor} ${deviceType.model} - Test Device`,
sysuptime: Math.floor(Math.random() * 86400 * 30), // 0-30 giorni uptime
syslocation: location,
vendor: deviceType.vendor,
model: deviceType.model,
os: deviceType.type === 'ap' ? 'UniFi' : 'VRP V800R021',
serial: `SN${String(i + 1).padStart(10, '0')}`,
status: 'active',
level: deviceType.level,
};
const result = db.upsertDevice(device);
deviceIds.push(result.lastInsertRowid);
deviceIPs.push(ip);
if ((i + 1) % 50 === 0) {
process.stdout.write(`\r Creati ${i + 1}/${numDevices} devices`);
}
}
const deviceTime = Date.now() - startTime;
console.log(`\r✅ Creati ${numDevices} devices in ${deviceTime}ms (${(deviceTime / numDevices).toFixed(2)}ms/device)`);
console.log();
// ========== GENERAZIONE INTERFACES ==========
console.log(`🔌 Generando interfaces...`);
const interfaceIds = new Map(); // deviceId -> [interfaceId1, interfaceId2, ...]
const ifStartTime = Date.now();
let totalInterfaces = 0;
for (let i = 0; i < deviceIds.length; i++) {
const deviceId = deviceIds[i];
const deviceType = selectDeviceType();
// Numero interfacce basato su tipo device
let numInterfaces = 24; // Default
if (deviceType.type === 'l3') numInterfaces = 48;
if (deviceType.type === 'router') numInterfaces = 8;
if (deviceType.type === 'ap') numInterfaces = 2;
if (deviceType.type === 'neighbor') numInterfaces = 1;
const ifIds = [];
for (let ifIndex = 1; ifIndex <= numInterfaces; ifIndex++) {
const iface = {
device_id: deviceId,
ifindex: ifIndex,
ifname: `GigabitEthernet0/0/${ifIndex}`,
ifdescr: `Interface ${ifIndex}`,
iftype: 6, // ethernetCsmacd
ifspeed: [100, 1000, 10000][Math.floor(Math.random() * 3)] * 1000000, // 100M, 1G, 10G
ifadminstatus: 1, // up
ifoperstatus: Math.random() > 0.1 ? 1 : 2, // 90% up, 10% down
ifalias: `Port ${ifIndex}`,
ifphysaddress: generateMAC(),
};
const result = db.upsertInterface(iface);
ifIds.push(result.lastInsertRowid);
totalInterfaces++;
}
interfaceIds.set(deviceId, ifIds);
if ((i + 1) % 50 === 0) {
process.stdout.write(`\r Creati ${totalInterfaces} interfaces per ${i + 1}/${deviceIds.length} devices`);
}
}
const ifTime = Date.now() - ifStartTime;
console.log(`\r✅ Creati ${totalInterfaces} interfaces in ${ifTime}ms`);
console.log();
// ========== GENERAZIONE LINKS ==========
console.log(`🔗 Generando links (~${numDevices * linksPerDevice} attesi)...`);
const linkStartTime = Date.now();
let linksCreated = 0;
const linkedPairs = new Set(); // Track per evitare duplicati
for (let i = 0; i < deviceIds.length; i++) {
const deviceId = deviceIds[i];
const deviceIfaces = interfaceIds.get(deviceId) || [];
// Crea N links casuali per questo device
const numLinksForDevice = Math.min(linksPerDevice, deviceIfaces.length);
for (let j = 0; j < numLinksForDevice; j++) {
// Seleziona device remoto casuale (diverso da se stesso)
let remoteIdx = Math.floor(Math.random() * deviceIds.length);
while (remoteIdx === i) {
remoteIdx = Math.floor(Math.random() * deviceIds.length);
}
const remoteDeviceId = deviceIds[remoteIdx];
const remoteIp = deviceIPs[remoteIdx];
// Check duplicati (evita A->B e B->A)
const pairKey1 = `${deviceId}-${remoteDeviceId}`;
const pairKey2 = `${remoteDeviceId}-${deviceId}`;
if (linkedPairs.has(pairKey1) || linkedPairs.has(pairKey2)) {
continue; // Skip se già collegati
}
linkedPairs.add(pairKey1);
// Seleziona interfaccia locale casuale
const localIfIndex = Math.floor(Math.random() * deviceIfaces.length) + 1;
const remoteIfaces = interfaceIds.get(remoteDeviceId) || [];
const remoteIfIndex = Math.floor(Math.random() * remoteIfaces.length) + 1;
const protocols = ['LLDP', 'CDP', 'FDP', 'EDP'];
const protocol = protocols[Math.floor(Math.random() * protocols.length)];
const link = {
device_id: deviceId,
local_ifindex: localIfIndex,
local_ifname: `GigabitEthernet0/0/${localIfIndex}`,
remote_device_id: remoteDeviceId,
remote_ip: remoteIp,
remote_sysname: `test_device_${String(remoteIdx + 1).padStart(4, '0')}`,
remote_chassisid: generateMAC(),
remote_portid: `GigabitEthernet0/0/${remoteIfIndex}`,
remote_portdesc: `Remote Port ${remoteIfIndex}`,
protocol,
};
db.upsertLink(link);
linksCreated++;
}
if ((i + 1) % 50 === 0) {
process.stdout.write(`\r Creati ${linksCreated} links per ${i + 1}/${deviceIds.length} devices`);
}
}
const linkTime = Date.now() - linkStartTime;
console.log(`\r✅ Creati ${linksCreated} links in ${linkTime}ms (${(linkTime / linksCreated).toFixed(2)}ms/link)`);
console.log();
// ========== STATISTICHE FINALI ==========
const totalTime = Date.now() - startTime;
console.log(`📊 Statistiche finali:`);
console.log(` ✅ Devices: ${numDevices}`);
console.log(` ✅ Interfaces: ${totalInterfaces}`);
console.log(` ✅ Links: ${linksCreated}`);
console.log(` ⏱️ Tempo totale: ${totalTime}ms (${(totalTime / 1000).toFixed(2)}s)`);
console.log();
// Verifica dati nel DB
const devicesInDB = db.getAllDevices().length;
const linksInDB = db.getAllLinks().length;
console.log(`🔍 Verifica database:`);
console.log(` 📦 Devices nel DB: ${devicesInDB}`);
console.log(` 🔗 Links nel DB: ${linksInDB}`);
console.log();
if (devicesInDB === numDevices && linksInDB >= linksCreated) {
console.log(`✅ Generazione completata con successo!`);
console.log();
console.log(`🚀 Ora puoi testare la performance con:`);
console.log(` - http://localhost:4000/topology-nedi-layer.html (originale)`);
console.log(` - http://localhost:4000/topology-nedi-layer-optimized.html (ottimizzato)`);
} else {
console.log(`⚠️ Attenzione: discrepanze nei dati generati`);
}
db.close();
process.exit(0);