Skip to content

Commit fb05b64

Browse files
committed
feat: add collection, user credentials, and skip flags to loadtest-public-api.js
1 parent 847eaf5 commit fb05b64

1 file changed

Lines changed: 49 additions & 36 deletions

File tree

scripts/loadtest-public-api.js

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ const target = (args.target || process.env.PUBLIC_API_URL || 'http://localhost:5
2020
const connections = parseInt(args.connections || '50', 10);
2121
const duration = parseInt(args.duration || '10', 10);
2222
const apiKey = args.apiKey || args.pk || args.sk || process.env.TEST_API_KEY || process.env.PUBLIC_KEY || 'pk_live_demo_key';
23+
const collection = args.collection || 'posts';
24+
const email = args.email || 'loadtest@example.com';
25+
const password = args.password || 'password123';
26+
const skipAuth = args.skipAuth === 'true' || args.skipAuth === true;
27+
const skipData = args.skipData === 'true' || args.skipData === true;
28+
2329
const keyType = apiKey.startsWith('sk_live_') ? 'Secret Key (sk_live_)' : apiKey.startsWith('pk_live_') ? 'Publishable Key (pk_live_)' : 'Custom Key';
2430

2531
console.log('====================================================');
@@ -29,6 +35,7 @@ console.log(`📍 Target URL : ${target}`);
2935
console.log(`⚡ Connections : ${connections} VUs (Virtual Users)`);
3036
console.log(`⏱️ Duration : ${duration} seconds`);
3137
console.log(`🔑 Key Type : ${keyType}`);
38+
console.log(`📁 Collection : ${collection}`);
3239
console.log('====================================================\n');
3340

3441
async function runBenchmark(name, opts) {
@@ -60,49 +67,55 @@ async function runBenchmark(name, opts) {
6067
}
6168

6269
async function startLoadTest() {
63-
// Test 1: Health Check Endpoint
70+
// Test 1: Health Check Endpoint (Works without any project setup)
6471
await runBenchmark('1. Health Check Endpoint (GET /api/health)', {
6572
url: `${target}/api/health`,
6673
method: 'GET',
6774
});
6875

69-
// Test 2: API Key Auth & Data Collection Read (RLS + DB Query)
70-
await runBenchmark('2. Data Collection Read (GET /api/data/posts)', {
71-
url: `${target}/api/data/posts?limit=10`,
72-
method: 'GET',
73-
headers: {
74-
'x-api-key': apiKey,
75-
'accept': 'application/json',
76-
},
77-
});
76+
// Test 2: Data Collection Read
77+
if (!skipData) {
78+
await runBenchmark(`2. Data Collection Read (GET /api/data/${collection})`, {
79+
url: `${target}/api/data/${collection}?limit=10`,
80+
method: 'GET',
81+
headers: {
82+
'x-api-key': apiKey,
83+
'accept': 'application/json',
84+
},
85+
});
86+
}
7887

79-
// Test 3: User Auth Login (Bcrypt Hash & JWT Signing Load)
80-
await runBenchmark('3. User Auth Login (POST /api/userAuth/login)', {
81-
url: `${target}/api/userAuth/login`,
82-
method: 'POST',
83-
headers: {
84-
'x-api-key': apiKey,
85-
'content-type': 'application/json',
86-
},
87-
body: {
88-
email: 'loadtest@example.com',
89-
password: 'password123',
90-
},
91-
});
88+
// Test 3: User Auth Login
89+
if (!skipAuth) {
90+
await runBenchmark('3. User Auth Login (POST /api/userAuth/login)', {
91+
url: `${target}/api/userAuth/login`,
92+
method: 'POST',
93+
headers: {
94+
'x-api-key': apiKey,
95+
'content-type': 'application/json',
96+
},
97+
body: {
98+
email,
99+
password,
100+
},
101+
});
102+
}
92103

93-
// Test 4: Data Write / Document Insert (POST /api/data/posts)
94-
await runBenchmark('4. Document Insert (POST /api/data/posts)', {
95-
url: `${target}/api/data/posts`,
96-
method: 'POST',
97-
headers: {
98-
'x-api-key': apiKey,
99-
'content-type': 'application/json',
100-
},
101-
body: {
102-
title: 'Load Test Post',
103-
content: 'Benchmarking database write performance',
104-
},
105-
});
104+
// Test 4: Data Write / Document Insert
105+
if (!skipData) {
106+
await runBenchmark(`4. Document Insert (POST /api/data/${collection})`, {
107+
url: `${target}/api/data/${collection}`,
108+
method: 'POST',
109+
headers: {
110+
'x-api-key': apiKey,
111+
'content-type': 'application/json',
112+
},
113+
body: {
114+
title: 'Load Test Post',
115+
content: 'Benchmarking database write performance',
116+
},
117+
});
118+
}
106119

107120
console.log('🎉 Load Testing Complete!');
108121
}

0 commit comments

Comments
 (0)