-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
490 lines (410 loc) · 12.8 KB
/
main.ts
File metadata and controls
490 lines (410 loc) · 12.8 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import {
ApiClient,
ApiClientInMemoryContextProvider,
} from "@northflank/js-client";
import https from "https";
import Bottleneck from "bottleneck";
const API_HOST = "https://api.northflank.com";
const TOTAL_SERVICES = Number(process.env.TOTAL_SERVICES || 10000);
const CREATE_CONCURRENCY = Number(process.env.CREATE_CONCURRENCY || 100);
const DELETE_CONCURRENCY = Number(process.env.TOTAL_SERVICES || 50);
const DEPLOYMENT_PLAN = process.env.DEPLOYMENT_PLAN || "nf-compute-10";
const PROJECT_ID = process.env.PROJECT_ID || "";
const CLEANUP = process.argv.includes("--cleanup");
const DELAY_ON_RATE_LIMIT_MS = 5000;
const MAX_FAILURE_LOGS = 100;
const INTERNAL_IMAGE = {
internal: {
id: "internal",
branch: "main",
},
};
const EXTERNAL_IMAGE = {
external: {
imagePath:
"283492384.dkr.ecr.us-east-2.amazonaws.com/your-image/goes/here:latest",
credentials: "ecr-credentials", // You can add your custom ECR credentials in the platform on the team level under Integrations > Registries
},
};
// You can swap between internal (Northflank build services) and external images (e.g. ECR service)
const image = INTERNAL_IMAGE;
interface Stats {
created: number;
createFailed: number;
startTime: number;
}
interface FailureLog {
serviceId: string;
error: string;
timestamp: number;
}
const createTimings: number[] = [];
const failureLogs: FailureLog[] = [];
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function mean(values: number[]): number {
if (values.length === 0) return 0;
return values.reduce((sum, v) => sum + v, 0) / values.length;
}
function stddev(values: number[]): number {
if (values.length <= 1) return 0;
const m = mean(values);
const variance = mean(values.map((v) => (v - m) ** 2));
return Math.sqrt(variance);
}
function percentile(values: number[], p: number): number {
if (values.length === 0) return 0;
const sorted = [...values].sort((a, b) => a - b);
const idx = Math.ceil((p / 100) * sorted.length) - 1;
return sorted[Math.max(0, idx)];
}
function generateId(): string {
return `${Date.now()}-${crypto.randomUUID().substring(0, 8)}`;
}
function formatDuration(ms: number): string {
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
return `${minutes}m ${remainingSeconds}s`;
}
function logFailure(failure: FailureLog): void {
failureLogs.push(failure);
if (failureLogs.length > MAX_FAILURE_LOGS) {
failureLogs.shift();
}
}
function printRecentFailures(count: number = 10): void {
if (failureLogs.length === 0) return;
console.log(
`\nRecent failures (last ${Math.min(count, failureLogs.length)} of ${failureLogs.length}):`,
);
const recent = failureLogs.slice(-count);
for (const f of recent) {
const time = new Date(f.timestamp).toISOString().slice(11, 19);
console.log(` [${time}] ${f.serviceId}`);
console.log(` Error: ${f.error}`);
}
}
function printProgress(stats: Stats): void {
const elapsed = Date.now() - stats.startTime;
const elapsedSec = elapsed / 1000;
const createRate = stats.created / elapsedSec;
const pct = ((stats.created / TOTAL_SERVICES) * 100).toFixed(1);
process.stdout.write(
`\r[${pct}%] ` +
`Created: ${stats.created}/${TOTAL_SERVICES} | ` +
`Failed: ${stats.createFailed} | ` +
`Rate: ${createRate.toFixed(1)}/s | ` +
`${formatDuration(elapsed)} `,
);
}
function printTimingStats(): void {
if (createTimings.length === 0) return;
console.log("\n" + "=".repeat(75));
console.log("TIMING STATISTICS");
console.log("=".repeat(75));
console.log(`Successful services: ${createTimings.length}`);
console.log("");
console.log(
`Create (ms): ` +
`avg=${mean(createTimings).toFixed(0)} ` +
`std=${stddev(createTimings).toFixed(0)} ` +
`min=${Math.min(...createTimings)} ` +
`max=${Math.max(...createTimings)} ` +
`p50=${percentile(createTimings, 50)} ` +
`p95=${percentile(createTimings, 95)} ` +
`p99=${percentile(createTimings, 99)}`,
);
console.log("=".repeat(75));
}
async function deleteService(
api: ApiClient,
projectId: string,
serviceId: string,
maxRetries = 3,
): Promise<{ success: boolean; error?: string }> {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const res = await api.delete.service({
parameters: { projectId, serviceId },
});
if (res.error) {
const status = res.error.status;
if (status === 429) {
await sleep(DELAY_ON_RATE_LIMIT_MS);
continue;
}
if (status === 404) {
return { success: true };
}
if (status === 409) {
await sleep(2000);
continue;
}
if (attempt === maxRetries) {
return { success: false, error: res.error.message ?? `HTTP ${status}` };
}
await sleep(1000);
continue;
}
return { success: true };
} catch (error) {
const err = error as { message?: string };
if (attempt === maxRetries) {
return { success: false, error: err.message ?? "Unknown error" };
}
await sleep(1000);
}
}
return { success: false, error: "Max retries exceeded" };
}
async function listServices(
api: ApiClient,
projectId: string,
): Promise<{ id: string; name: string }[]> {
const allServices: { id: string; name: string }[] = [];
let cursor: string | undefined;
let page = 1;
console.log(`Listing all services in project ${projectId}...`);
while (true) {
const options: { per_page: number; cursor?: string } = { per_page: 100 };
if (cursor) {
options.cursor = cursor;
}
const res = await api.list.services({
parameters: { projectId },
options,
});
if (res.error) {
if (res.error.status === 429) {
console.log(
` Rate limited on page ${page}, waiting ${DELAY_ON_RATE_LIMIT_MS}ms...`,
);
await sleep(DELAY_ON_RATE_LIMIT_MS);
continue;
}
throw new Error(res.error.message ?? `HTTP ${res.error.status}`);
}
const services = res.data?.services ?? [];
if (!services.length) break;
allServices.push(...services.map((s) => ({ id: s.id, name: s.name })));
console.log(
` Page ${page}: ${services.length} services (${allServices.length} total)`,
);
const pagination = res.pagination;
if (!pagination?.hasNextPage) break;
cursor = pagination.cursor;
page++;
await sleep(200);
}
return allServices;
}
const deleteLimiter = new Bottleneck({
maxConcurrent: DELETE_CONCURRENCY,
minTime: 10,
});
async function cleanup(api: ApiClient, projectId: string): Promise<void> {
console.log(`Deleting all services in project ${projectId}...\n`);
const startTime = Date.now();
const allServices = await listServices(api, projectId);
const toDelete = allServices.filter(
(svc) =>
svc.name !== INTERNAL_IMAGE.internal.id &&
svc.id !== INTERNAL_IMAGE.internal.id,
);
const skipped = allServices.length - toDelete.length;
if (skipped > 0) {
console.log(
`Skipping ${skipped} protected service(s) (${INTERNAL_IMAGE.internal.id}).`,
);
}
console.log(`Found ${toDelete.length} services to delete.\n`);
if (toDelete.length === 0) return;
let deleted = 0;
let failed = 0;
await Promise.all(
toDelete.map((svc) =>
deleteLimiter.schedule(async () => {
const result = await deleteService(api, projectId, svc.id);
if (result.success) deleted++;
else failed++;
const elapsed = ((Date.now() - startTime) / 1000).toFixed(0);
const rate =
deleted > 0
? ((deleted / (Date.now() - startTime)) * 1000 * 60).toFixed(0)
: "0";
process.stdout.write(
`\r[${elapsed}s] Deleted: ${deleted}/${toDelete.length} | Failed: ${failed} | Rate: ${rate}/min `,
);
return result;
}),
),
);
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
console.log(
`\n\nCleanup complete: ${deleted} deleted, ${failed} failed in ${elapsed}s`,
);
}
async function createService(
api: ApiClient,
projectId: string,
stats: Stats,
): Promise<boolean> {
const serviceId = `svc-${generateId()}`;
const createStartMs = Date.now();
try {
const res = await api.create.service.deployment({
parameters: { projectId },
data: {
name: serviceId,
billing: { deploymentPlan: DEPLOYMENT_PLAN },
deployment: {
instances: 1,
...image,
},
ports: [
{
name: "http",
internalPort: 80,
protocol: "HTTP",
public: true,
},
],
runtimeEnvironment: {},
},
});
const createDurationMs = Date.now() - createStartMs;
if (res.error) {
stats.createFailed++;
logFailure({
serviceId,
error: JSON.stringify(res.error),
timestamp: Date.now(),
});
return false;
}
stats.created++;
createTimings.push(createDurationMs);
return true;
} catch (err) {
stats.createFailed++;
logFailure({
serviceId,
error: err instanceof Error ? err.message : String(err),
timestamp: Date.now(),
});
return false;
}
}
async function confirm(): Promise<boolean> {
return new Promise((resolve) => {
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("\nProceed? (y/N): ", (answer: string) => {
rl.close();
resolve(answer.toLowerCase() === "y" || answer.toLowerCase() === "yes");
});
});
}
async function main(): Promise<void> {
const token = process.env.NORTHFLANK_API_TOKEN ?? process.env.TOKEN;
if (!token) {
console.error(
"Error: NORTHFLANK_API_TOKEN environment variable is required",
);
process.exit(1);
}
if (!PROJECT_ID) {
console.error("Error: PROJECT_ID environment variable is required");
process.exit(1);
}
console.log("=".repeat(75));
console.log("Bulk Service Creation Stress Test");
console.log("=".repeat(75));
console.log(`Target API: ${API_HOST}`);
console.log(`Total services: ${TOTAL_SERVICES}`);
console.log(`Project: ${PROJECT_ID}`);
console.log(`Create concurrency: ${CREATE_CONCURRENCY}`);
console.log(`Deployment plan: ${DEPLOYMENT_PLAN}`);
console.log(`Cleanup services: ${CLEANUP}`);
console.log("=".repeat(75));
const confirmed = await confirm();
if (!confirmed) {
console.log("Aborted.");
process.exit(0);
}
const httpsAgent = new https.Agent({
keepAlive: true,
maxFreeSockets: CREATE_CONCURRENCY,
});
const contextProvider = new ApiClientInMemoryContextProvider();
await contextProvider.addContext({
name: "default",
token,
host: API_HOST,
});
const api = new ApiClient(contextProvider, { agent: httpsAgent });
const createLimiter = new Bottleneck({
maxConcurrent: CREATE_CONCURRENCY,
minTime: 10,
});
const stats: Stats = {
created: 0,
createFailed: 0,
startTime: Date.now(),
};
process.on("SIGINT", () => {
console.log("\n\nInterrupted!");
httpsAgent.destroy();
process.exit(1);
});
try {
if (CLEANUP) {
console.log("\nCleaning up existing services...");
await cleanup(api, PROJECT_ID);
}
console.log(`\nCreating ${TOTAL_SERVICES} services...\n`);
const createPromises: Promise<boolean>[] = [];
const progressInterval = setInterval(() => {
printProgress(stats);
}, 500);
for (let i = 0; i < TOTAL_SERVICES; i++) {
createPromises.push(
createLimiter.schedule(() => createService(api, PROJECT_ID, stats)),
);
}
await Promise.all(createPromises);
clearInterval(progressInterval);
printProgress(stats);
const totalElapsed = Date.now() - stats.startTime;
const successRate = ((stats.created / TOTAL_SERVICES) * 100).toFixed(1);
console.log("\n\n" + "=".repeat(75));
console.log("COMPLETE");
console.log("=".repeat(75));
console.log(`Project ID: ${PROJECT_ID}`);
console.log(`Services created: ${stats.created}`);
console.log(`Create failures: ${stats.createFailed}`);
console.log(`Success rate: ${successRate}%`);
console.log(`Total time: ${formatDuration(totalElapsed)}`);
console.log(
`Create rate: ${(stats.created / (totalElapsed / 1000)).toFixed(1)}/second`,
);
console.log("=".repeat(75));
if (createTimings.length > 0) {
printTimingStats();
}
if (stats.createFailed > 0) {
printRecentFailures(20);
}
} catch (err) {
console.error("\nFatal error:", err);
process.exit(1);
} finally {
httpsAgent.destroy();
}
}
main().catch(console.error);