Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions backend/controllers/submissionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ export const runCode = async (req, res) => {
return res.status(202).json(jobStatus);

} catch (error) {
if (error.statusCode === 429 || error.code === 'QUEUE_FULL') {
return res.status(429).json({ message: error.message || 'Execution queue is full. Please try again shortly.' });
}
res.status(500).json({ message: error.message });
}
};
Expand Down Expand Up @@ -237,32 +240,15 @@ export const submitCode = async (req, res) => {

// Check if this user already solved this question before (for isFirstAccepted)
const user = await User.findById(userId);
const alreadySolved = user.solvedQuestions.some(
const alreadySolved = user ? user.solvedQuestions.some(
(id) => id.toString() === question._id.toString()
);
) : false;
const isFirstAccepted = isAccepted && !alreadySolved;

const submission = await Submission.create({
user: userId,
question: question._id,
code,
language,
status: overallVerdict,
passedCount,
totalCount,
errorMessage: firstErrorMessage,
runTime: runTimeMax,
isFirstAccepted
});

// Auto-update Question stats (totalSubmissions + totalAccepted)
await updateQuestionStats(question._id, isAccepted);

// Update User statistics
user.submissionsCount += 1;

if (isAccepted) {
if (!alreadySolved) {
// Update user statistics in memory
if (user) {
user.submissionsCount += 1;
if (isAccepted && !alreadySolved) {
user.solvedQuestions.push(question._id);

// Update solved difficulty counts
Expand All @@ -273,14 +259,34 @@ export const submitCode = async (req, res) => {
}
}

await user.save();
// Parallelize genuinely independent database writes
const [submission] = await Promise.all([
Submission.create({
user: userId,
question: question._id,
code,
language,
status: overallVerdict,
passedCount,
totalCount,
errorMessage: firstErrorMessage,
runTime: runTimeMax,
isFirstAccepted
}),
updateQuestionStats(question._id, isAccepted),
user ? user.save() : Promise.resolve()
]);

return submission;
};

const jobStatus = compilerQueue.enqueue(runFn);
return res.status(202).json(jobStatus);

} catch (error) {
if (error.statusCode === 429 || error.code === 'QUEUE_FULL') {
return res.status(429).json({ message: error.message || 'Execution queue is full. Please try again shortly.' });
}
res.status(500).json({ message: error.message });
}
};
Expand Down
144 changes: 144 additions & 0 deletions backend/tests/benchmark_pipeline_baseline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { runLocalCodeMulti } from '../utils/localRunner.js';
import User from '../models/User.js';

const questionData = {
visibleTestCases: [
{ input: '4 10\n2 3 5 7', output: '10' },
{ input: '3 9\n4 8 6', output: '8' },
{ input: '3 5\n1 2 3', output: '5' }
],
hiddenTestCases: [
{ input: '2 5\n2 4', output: '4' },
{ input: '1 100\n50', output: '50' },
{ input: '5 20\n1 2 3 4 5', output: '15' },
{ input: '4 15\n5 5 5 5', output: '15' },
{ input: '3 10\n10 20 30', output: '10' },
{ input: '4 11\n2 4 6 8', output: '10' }
]
};

const allTestCases = [...questionData.visibleTestCases, ...questionData.hiddenTestCases];
const inputs = allTestCases.map(tc => tc.input);

const cppCode = '#include <iostream>\n#include <vector>\nusing namespace std;\nint main() { int n; long long max_sum; if (!(cin >> n >> max_sum)) return 0; vector<long long> costs(n); for (int i = 0; i < n; i++) cin >> costs[i]; vector<bool> dp(max_sum + 1, false); dp[0] = true; for (int i = 0; i < n; i++) { long long c = costs[i]; for (long long j = max_sum; j >= c; j--) { if (dp[j - c]) dp[j] = true; } } for (long long j = max_sum; j >= 0; j--) { if (dp[j]) { cout << j << endl; break; } } return 0; }';

const javaCode = 'import java.util.Scanner;\npublic class Main {\n public static void main(String[] args) {\n Scanner sc = new Scanner(System.in);\n if (!sc.hasNextInt()) return;\n int n = sc.nextInt();\n int maxSum = sc.nextInt();\n int[] costs = new int[n];\n for (int i = 0; i < n; i++) costs[i] = sc.nextInt();\n boolean[] dp = new boolean[maxSum + 1];\n dp[0] = true;\n for (int c : costs) {\n for (int j = maxSum; j >= c; j--) {\n if (dp[j - c]) dp[j] = true;\n }\n }\n for (int j = maxSum; j >= 0; j--) {\n if (dp[j]) { System.out.println(j); break; }\n }\n }\n}';

const pythonCode = 'import sys\ninput_data = sys.stdin.read().split()\nif input_data:\n n, max_sum = int(input_data[0]), int(input_data[1])\n costs = [int(x) for x in input_data[2:2+n]]\n dp = [False] * (max_sum + 1)\n dp[0] = True\n for c in costs:\n for j in range(max_sum, c - 1, -1):\n if dp[j - c]:\n dp[j] = True\n for j in range(max_sum, -1, -1):\n if dp[j]:\n print(j)\n break\n';

export async function runFullBenchmark(label = 'Baseline', customOptions = {}) {
console.log(`\n================================================================`);
console.log(` NQTCoder Benchmark: ${label}`);
console.log(`================================================================\n`);

const mockUser = User.hydrate({
_id: '507f1f77bcf86cd799439011',
username: 'benchmark_student',
email: 'student@nqtcoder.dev',
password: '$2a$10$abcdefghijklmnopqrstuu',
submissionsCount: 5,
solvedQuestions: [],
solvedCount: { easy: 2, medium: 2, hard: 1 }
});

const runLang = async (langName, code, langId) => {
console.log(`--- Benchmarking 5 Full Runs for ${langName} (9 Test Cases) ---`);
const runs = [];
for (let r = 1; r <= 5; r++) {
const tStart = performance.now();

// 1. Request / Auth simulation
const tAuth = 1.0;

// 2. Queue scheduling simulation
const tQueue = 0.5;

// 3. Execution (Compile + 9 Test Cases)
const tExecStart = performance.now();
const execRes = await runLocalCodeMulti(code, langId, inputs, 2);
const tExec = performance.now() - tExecStart;

// 4. Result verdict check
let passed = 0;
for (let i = 0; i < allTestCases.length; i++) {
if (execRes.results && execRes.results[i]?.stdout?.trim() === allTestCases[i].output.trim()) {
passed++;
}
}

// 5. DB Persistence simulation (User pre-save hook with fix)
const tUserSaveStart = performance.now();
mockUser.submissionsCount += 1;
mockUser.solvedCount.medium += 1;
await new Promise((resolve) => {
mockUser.schema.s.hooks.execPre('save', mockUser, () => resolve());
});
const tUserSave = performance.now() - tUserSaveStart;
const tDbTotal = tUserSave + 40.0; // ~40ms network write

// 6. Polling simulation
const pollingInterval = customOptions.pollingDelay || 1000;
// Fixed 1000ms polling vs adaptive:
let tPollingLag = 500.0; // average fixed 1000ms delay lag
if (customOptions.adaptivePolling) {
// Adaptive schedule: [0, 150, 250, 400, 600, 1000]
// If execution finishes at tExec, the next poll tick triggers in ~75ms on average
tPollingLag = 75.0;
}

const totalBackend = tAuth + tQueue + tExec + tDbTotal;
const totalUserPerceived = totalBackend + tPollingLag;

runs.push({
run: r,
backendTime: totalBackend,
pollingLag: tPollingLag,
userPerceivedTime: totalUserPerceived,
execTime: tExec,
dbTime: tDbTotal,
passed
});

console.log(` Run #${r}: Backend = ${totalBackend.toFixed(1)} ms | Polling Lag = ${tPollingLag.toFixed(1)} ms | User-Perceived = ${totalUserPerceived.toFixed(1)} ms (Passed: ${passed}/9)`);
}

const calcStats = (arr) => {
const sorted = [...arr].sort((a, b) => a - b);
const min = sorted[0];
const max = sorted[sorted.length - 1];
const avg = sorted.reduce((a, b) => a + b, 0) / sorted.length;
const med = sorted[Math.floor(sorted.length / 2)];
const p95 = sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * 0.95))];
return { min, max, avg, med, p95 };
};

const backendStats = calcStats(runs.map(r => r.backendTime));
const userStats = calcStats(runs.map(r => r.userPerceivedTime));

return {
langName,
backendStats,
userStats,
runs
};
};

const pyResults = await runLang('Python', pythonCode, 'python');
const cppResults = await runLang('C++', cppCode, 'cpp');
const javaResults = await runLang('Java', javaCode, 'java');

console.log(`\n================================================================`);
console.log(` ${label.toUpperCase()} SUMMARY RESULTS (AVERAGE OF 5 RUNS) `);
console.log(`================================================================`);
console.log(`Python: Backend = ${pyResults.backendStats.avg.toFixed(1)} ms | Perceived = ${pyResults.userStats.avg.toFixed(1)} ms (Median: ${pyResults.userStats.med.toFixed(1)} ms)`);
console.log(`C++: Backend = ${cppResults.backendStats.avg.toFixed(1)} ms | Perceived = ${cppResults.userStats.avg.toFixed(1)} ms (Median: ${cppResults.userStats.med.toFixed(1)} ms)`);
console.log(`Java: Backend = ${javaResults.backendStats.avg.toFixed(1)} ms | Perceived = ${javaResults.userStats.avg.toFixed(1)} ms (Median: ${javaResults.userStats.med.toFixed(1)} ms)`);
console.log(`================================================================\n`);

return { pyResults, cppResults, javaResults };
}

// Auto-run if executed directly
if (process.argv[1]?.endsWith('benchmark_pipeline_baseline.js')) {
runFullBenchmark('Pre-Optimization Baseline').catch(console.error);
}
146 changes: 146 additions & 0 deletions backend/tests/load_test_queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import { compilerQueue, QueueCapacityError } from '../utils/compilerQueue.js';
import { runLocalCodeMulti } from '../utils/localRunner.js';

const pyCode = 'import sys\ninput_data = sys.stdin.read().split()\nif input_data:\n n, max_sum = int(input_data[0]), int(input_data[1])\n costs = [int(x) for x in input_data[2:2+n]]\n dp = [False] * (max_sum + 1)\n dp[0] = True\n for c in costs:\n for j in range(max_sum, c - 1, -1):\n if dp[j - c]:\n dp[j] = True\n for j in range(max_sum, -1, -1):\n if dp[j]:\n print(j)\n break\n';
const cppCode = '#include <iostream>\n#include <vector>\nusing namespace std;\nint main() { int n; long long max_sum; if (!(cin >> n >> max_sum)) return 0; vector<long long> costs(n); for (int i = 0; i < n; i++) cin >> costs[i]; vector<bool> dp(max_sum + 1, false); dp[0] = true; for (int i = 0; i < n; i++) { long long c = costs[i]; for (long long j = max_sum; j >= c; j--) { if (dp[j - c]) dp[j] = true; } } for (long long j = max_sum; j >= 0; j--) { if (dp[j]) { cout << j << endl; break; } } return 0; }';

const sampleInputs = [
'4 10\n2 3 5 7',
'3 9\n4 8 6',
'3 5\n1 2 3',
'2 5\n2 4',
'1 100\n50',
'5 20\n1 2 3 4 5',
'4 15\n5 5 5 5',
'3 10\n10 20 30',
'4 11\n2 4 6 8'
];

async function pollJob(jobId) {
const t0 = performance.now();
while (true) {
const job = compilerQueue.getJobStatus(jobId);
if (!job) throw new Error('Job not found');
if (job.status === 'completed') {
return { elapsed: performance.now() - t0, result: job.result, status: 'completed' };
}
if (job.status === 'failed') {
return { elapsed: performance.now() - t0, error: job.error, status: 'failed' };
}
await new Promise(r => setTimeout(r, 100));
}
}

async function runLoadTestScenario(name, submissionCount, lang = 'python') {
console.log(`\n================================================================`);
console.log(` LOAD SCENARIO: ${name} (${submissionCount} Submissions, Lang: ${lang})`);
console.log(`================================================================`);

const initialMemory = process.memoryUsage().rss / (1024 * 1024);
const codeToRun = lang === 'cpp' ? cppCode : pyCode;
const submissions = [];
const rejected = [];
const tStartAll = performance.now();

for (let i = 0; i < submissionCount; i++) {
const submitStart = performance.now();
try {
const runFn = async () => {
const tStartExec = performance.now();
const res = await runLocalCodeMulti(codeToRun, lang, sampleInputs, 2);
const execDuration = performance.now() - tStartExec;
return { res, execDuration };
};

const jobStatus = compilerQueue.enqueue(runFn);
submissions.push({
index: i + 1,
jobId: jobStatus.jobId,
submitTime: submitStart
});
} catch (err) {
if (err instanceof QueueCapacityError || err.statusCode === 429) {
rejected.push({ index: i + 1, error: err.message });
} else {
throw err;
}
}
}

console.log(` Enqueued: ${submissions.length} jobs | Rejected (HTTP 429): ${rejected.length} jobs`);

// Wait for all enqueued submissions to complete
const results = await Promise.all(
submissions.map(async (s) => {
const pollRes = await pollJob(s.jobId);
const totalJobLatency = performance.now() - s.submitTime;
return {
index: s.index,
totalLatency: totalJobLatency,
backendDuration: pollRes.result?.execDuration || 0,
status: pollRes.status
};
})
);

const totalTime = performance.now() - tStartAll;
const finalMemory = process.memoryUsage().rss / (1024 * 1024);

const latencies = results.map(r => r.totalLatency).sort((a, b) => a - b);
const min = latencies[0] || 0;
const max = latencies[latencies.length - 1] || 0;
const avg = latencies.reduce((a, b) => a + b, 0) / (latencies.length || 1);
const p50 = latencies[Math.floor(latencies.length * 0.5)] || 0;
const p95 = latencies[Math.floor(latencies.length * 0.95)] || 0;
const throughputPerMin = ((results.length / totalTime) * 60000).toFixed(1);

console.log(` Completed: ${results.filter(r => r.status === 'completed').length}/${results.length}`);
console.log(` Total Scenario Time: ${(totalTime / 1000).toFixed(2)}s`);
console.log(` Throughput Rate: ${throughputPerMin} submissions/minute`);
console.log(` Latency Metrics: Min: ${min.toFixed(0)}ms | p50: ${p50.toFixed(0)}ms | Avg: ${avg.toFixed(0)}ms | p95: ${p95.toFixed(0)}ms | Max: ${max.toFixed(0)}ms`);
console.log(` Memory Impact (RSS): Initial: ${initialMemory.toFixed(1)}MB -> Final: ${finalMemory.toFixed(1)}MB (Delta: ${(finalMemory - initialMemory).toFixed(1)}MB)`);

return {
name,
submissionCount,
completed: results.length,
rejected: rejected.length,
throughputPerMin,
min,
p50,
avg,
p95,
max,
rssDelta: finalMemory - initialMemory
};
}

async function runAllLoadTests() {
console.log('================================================================');
console.log(' NQTCoder Queue Load & Concurrency Benchmark Suite ');
console.log('================================================================');

// Scenario 1: 1 Single User
await runLoadTestScenario('Single User Baseline', 1, 'python');

// Scenario 2: 2 Simultaneous Submissions
await runLoadTestScenario('2 Concurrent Submissions', 2, 'python');

// Scenario 3: 5 Queued Submissions Burst
await runLoadTestScenario('5 Queued Burst (Python)', 5, 'python');

// Scenario 4: 10 Queued Submissions Burst
await runLoadTestScenario('10 Queued Burst (Python)', 10, 'python');

// Scenario 5: 20 Queued Submissions Burst
await runLoadTestScenario('20 Queued Burst (Python)', 20, 'python');

// Scenario 6: Overload Capacity Test (35 Submissions with MAX_QUEUE_SIZE=30)
await runLoadTestScenario('35 Overload Test (Capacity Limit Check)', 35, 'python');

console.log('\n================================================================');
console.log(' LOAD TESTING SUITE COMPLETE ');
console.log('================================================================\n');
}

runAllLoadTests().catch(console.error);
Loading
Loading