-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadTest.js
More file actions
146 lines (116 loc) · 5.44 KB
/
Copy pathloadTest.js
File metadata and controls
146 lines (116 loc) · 5.44 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
const { chromium } = require('playwright');
const assert = require('assert');
const URL = 'https://edgemont-voting-tool.vercel.app';
const NUM_USERS = 30;
const CATEGORIES = ['Bread', 'Appetizers', 'Dessert', 'Entrée & Soups'];
async function simulateVoting(page, userId) {
try {
console.log(`User ${userId}: Starting vote submission`);
await page.goto(URL, {
timeout: 60000,
waitUntil: 'networkidle'
});
console.log(`User ${userId}: Page loaded successfully`);
// Wait for categories and log what we find
const categories = await page.$$('.category');
console.log(`User ${userId}: Found ${categories.length} categories`);
// Add a small delay between users
await page.waitForTimeout(userId * 100);
let votesSubmitted = 0;
// Submit votes for each category
for (const category of CATEGORIES) {
try {
const inputs = await page.$$(`input[data-category="${category}"]`);
console.log(`User ${userId}: Found ${inputs.length} inputs for ${category}`);
if (inputs.length >= 2) {
const dish1 = Math.floor(Math.random() * 20) + 1;
const dish2 = Math.floor(Math.random() * 20) + 1;
await inputs[0].fill(dish1.toString());
await inputs[1].fill(dish2.toString());
votesSubmitted++;
console.log(`User ${userId}: Submitted votes ${dish1} and ${dish2} for ${category}`);
}
await page.waitForTimeout(Math.random() * 500 + 200);
} catch (error) {
console.error(`User ${userId}: Error submitting votes for ${category}:`, error.message);
}
}
if (votesSubmitted > 0) {
console.log(`User ${userId}: Attempting to click submit button`);
// Try to find and click the submit button
const submitButton = await page.$('#submitVotes');
if (submitButton) {
await submitButton.click();
console.log(`User ${userId}: Submit button clicked`);
// Wait for either success or error toast
const success = await Promise.race([
page.waitForSelector('.toast', {
timeout: 20000,
state: 'attached'
}).then(() => true),
page.waitForTimeout(20000).then(() => false)
]);
console.log(`User ${userId}: Vote submission ${success ? 'successful' : 'timed out'}`);
return success;
} else {
console.error(`User ${userId}: Submit button not found`);
return false;
}
} else {
console.error(`User ${userId}: No votes were submitted`);
return false;
}
} catch (error) {
console.error(`User ${userId} encountered error:`, error.message);
return false;
}
}
async function runLoadTest() {
console.log(`Starting load test with ${NUM_USERS} concurrent users`);
console.log(`Testing URL: ${URL}`);
const browser = await chromium.launch({ headless: false }); // Make browser visible
const results = { success: 0, failed: 0 };
const startTime = Date.now();
try {
// Verify site accessibility
const testContext = await browser.newContext();
const testPage = await testContext.newPage();
console.log('Verifying site accessibility...');
await testPage.goto(URL, {
timeout: 60000,
waitUntil: 'networkidle'
});
console.log('Site is accessible, proceeding with load test');
await testContext.close();
// Run in batches of 5
const BATCH_SIZE = 5;
for (let i = 0; i < NUM_USERS; i += BATCH_SIZE) {
const batch = Array(Math.min(BATCH_SIZE, NUM_USERS - i))
.fill(0)
.map(async (_, index) => {
const userId = i + index + 1;
const context = await browser.newContext();
const page = await context.newPage();
const success = await simulateVoting(page, userId);
if (success) results.success++;
else results.failed++;
await context.close();
});
await Promise.all(batch);
console.log(`Completed batch of ${batch.length} users`);
}
} catch (error) {
console.error('Load test error:', error.message);
} finally {
const duration = (Date.now() - startTime) / 1000;
console.log('\nLoad Test Results:');
console.log('==================');
console.log(`Duration: ${duration.toFixed(2)} seconds`);
console.log(`Successful votes: ${results.success}`);
console.log(`Failed votes: ${results.failed}`);
console.log(`Success rate: ${((results.success / NUM_USERS) * 100).toFixed(2)}%`);
console.log(`Average time per vote: ${(duration / NUM_USERS).toFixed(2)} seconds`);
await browser.close();
}
}
runLoadTest().catch(console.error);