CoolFace
Apppublic

Ratan1729/code-execution

sourceHugging Faceupdated 8mo agoView on Hugging Face
1likes
stress-test.js297 linesDownload Raw Back to test
1// ╔══════════════════════════════════════════════════════════════════╗2// ║  Stress Test — 5 Problems × 10 Test Cases — All Simultaneous  ║3// ║  Run: node test/stress-test.js                                ║4// ╚══════════════════════════════════════════════════════════════════╝5 6const http = require("http");7 8const BASE_URL = process.argv[2] || "http://140.245.240.6:3000";9 10function request(method, path, body) {11  return new Promise((resolve, reject) => {12    const url = new URL(path, BASE_URL);13    const options = {14      hostname: url.hostname,15      port: url.port,16      path: url.pathname,17      method,18      headers: { "Content-Type": "application/json" },19    };20    const req = http.request(options, (res) => {21      let data = "";22      res.on("data", (chunk) => (data += chunk));23      res.on("end", () => {24        try {25          resolve({ status: res.statusCode, body: JSON.parse(data) });26        } catch {27          resolve({ status: res.statusCode, body: data });28        }29      });30    });31    req.on("error", reject);32    if (body) req.write(JSON.stringify(body));33    req.end();34  });35}36 37// ═══════════════════════════════════════════════38//  5 PROBLEMS WITH 10 TEST CASES EACH39// ═══════════════════════════════════════════════40 41const problems = [42  // ─── Problem 1: Two Sum (C++) ───43  {44    name: "P1: Two Sum (C++)",45    language: "cpp",46    code: `#include <bits/stdc++.h>47using namespace std;48int main() {49    int a, b;50    cin >> a >> b;51    cout << a + b << endl;52    return 0;53}`,54    timeLimit: 2,55    memoryLimit: 256,56    testCases: [57      { input: "1 2\n", expectedOutput: "3" },58      { input: "0 0\n", expectedOutput: "0" },59      { input: "-5 5\n", expectedOutput: "0" },60      { input: "100 200\n", expectedOutput: "300" },61      { input: "999999 1\n", expectedOutput: "1000000" },62      { input: "-100 -200\n", expectedOutput: "-300" },63      { input: "2147483 0\n", expectedOutput: "2147483" },64      { input: "50 50\n", expectedOutput: "100" },65      { input: "1 -1\n", expectedOutput: "0" },66      { input: "12345 67890\n", expectedOutput: "80235" },67    ],68  },69 70  // ─── Problem 2: Factorial (C) ───71  {72    name: "P2: Factorial (C)",73    language: "c",74    code: `#include <stdio.h>75int main() {76    int n;77    scanf("%d", &n);78    long long fact = 1;79    for (int i = 2; i <= n; i++) fact *= i;80    printf("%lld\\n", fact);81    return 0;82}`,83    timeLimit: 2,84    memoryLimit: 256,85    testCases: [86      { input: "0\n", expectedOutput: "1" },87      { input: "1\n", expectedOutput: "1" },88      { input: "5\n", expectedOutput: "120" },89      { input: "10\n", expectedOutput: "3628800" },90      { input: "12\n", expectedOutput: "479001600" },91      { input: "15\n", expectedOutput: "1307674368000" },92      { input: "20\n", expectedOutput: "2432902008176640000" },93      { input: "3\n", expectedOutput: "6" },94      { input: "7\n", expectedOutput: "5040" },95      { input: "2\n", expectedOutput: "2" },96    ],97  },98 99  // ─── Problem 3: Fibonacci (Python) ───100  {101    name: "P3: Fibonacci (Python)",102    language: "python",103    code: `n = int(input())104a, b = 0, 1105for _ in range(n):106    a, b = b, a + b107print(a)`,108    timeLimit: 3,109    memoryLimit: 256,110    testCases: [111      { input: "0\n", expectedOutput: "0" },112      { input: "1\n", expectedOutput: "1" },113      { input: "2\n", expectedOutput: "1" },114      { input: "5\n", expectedOutput: "5" },115      { input: "10\n", expectedOutput: "55" },116      { input: "15\n", expectedOutput: "610" },117      { input: "20\n", expectedOutput: "6765" },118      { input: "30\n", expectedOutput: "832040" },119      { input: "40\n", expectedOutput: "102334155" },120      { input: "50\n", expectedOutput: "12586269025" },121    ],122  },123 124  // ─── Problem 4: Reverse Array (C++) ───125  {126    name: "P4: Reverse Array (C++)",127    language: "cpp",128    code: `#include <bits/stdc++.h>129using namespace std;130int main() {131    ios_base::sync_with_stdio(false);132    cin.tie(NULL);133    int n; cin >> n;134    vector<int> v(n);135    for (int i = 0; i < n; i++) cin >> v[i];136    reverse(v.begin(), v.end());137    for (int i = 0; i < n; i++) {138        if (i) cout << " ";139        cout << v[i];140    }141    cout << endl;142    return 0;143}`,144    timeLimit: 2,145    memoryLimit: 256,146    testCases: [147      { input: "5\n1 2 3 4 5\n", expectedOutput: "5 4 3 2 1" },148      { input: "1\n42\n", expectedOutput: "42" },149      { input: "3\n10 20 30\n", expectedOutput: "30 20 10" },150      { input: "4\n-1 -2 -3 -4\n", expectedOutput: "-4 -3 -2 -1" },151      { input: "6\n1 1 1 1 1 1\n", expectedOutput: "1 1 1 1 1 1" },152      { input: "2\n100 200\n", expectedOutput: "200 100" },153      { input: "7\n7 6 5 4 3 2 1\n", expectedOutput: "1 2 3 4 5 6 7" },154      { input: "3\n0 0 0\n", expectedOutput: "0 0 0" },155      { input: "5\n9 8 7 6 5\n", expectedOutput: "5 6 7 8 9" },156      { input: "4\n1 3 5 7\n", expectedOutput: "7 5 3 1" },157    ],158  },159 160  // ─── Problem 5: Prime Check (C++) ───161  {162    name: "P5: Prime Check (C++)",163    language: "cpp",164    code: `#include <bits/stdc++.h>165using namespace std;166int main() {167    int n; cin >> n;168    if (n < 2) { cout << "NO" << endl; return 0; }169    for (int i = 2; i * i <= n; i++) {170        if (n % i == 0) { cout << "NO" << endl; return 0; }171    }172    cout << "YES" << endl;173    return 0;174}`,175    timeLimit: 2,176    memoryLimit: 256,177    testCases: [178      { input: "2\n", expectedOutput: "YES" },179      { input: "3\n", expectedOutput: "YES" },180      { input: "4\n", expectedOutput: "NO" },181      { input: "17\n", expectedOutput: "YES" },182      { input: "1\n", expectedOutput: "NO" },183      { input: "100\n", expectedOutput: "NO" },184      { input: "97\n", expectedOutput: "YES" },185      { input: "0\n", expectedOutput: "NO" },186      { input: "49\n", expectedOutput: "NO" },187      { input: "7919\n", expectedOutput: "YES" },188    ],189  },190];191 192// ═══════════════════════════════════════════════193//  RUN ALL 5 SIMULTANEOUSLY194// ═══════════════════════════════════════════════195 196async function run() {197  console.log("\n╔══════════════════════════════════════════════════════════╗");198  console.log("║  🚀 STRESS TEST — 5 Problems × 10 Test Cases           ║");199  console.log("║  All 5 fired SIMULTANEOUSLY                            ║");200  console.log(`║  Target: ${BASE_URL.padEnd(44)}║`);201  console.log("╚══════════════════════════════════════════════════════════╝\n");202 203  const totalStart = Date.now();204 205  // Fire ALL 5 judge requests at the same time206  const promises = problems.map((problem, idx) =>207    new Promise((resolve) => setTimeout(resolve, idx * 500)).then(() =>208      request("POST", "/api/judge", {209        language: problem.language,210        code: problem.code,211        testCases: problem.testCases,212        timeLimit: problem.timeLimit,213        memoryLimit: problem.memoryLimit,214      }),215    ),216  );217 218  console.log(219    "⏳ All 5 requests fired (staggered 500ms apart for 1GB instance)...\n",220  );221 222  const results = await Promise.all(promises);223 224  const totalEnd = Date.now();225  const totalTime = totalEnd - totalStart;226 227  // ─── Display Results ───228  console.log("═══════════════════════════════════════════════════════════");229  console.log("  RESULTS");230  console.log("═══════════════════════════════════════════════════════════\n");231 232  let totalPassed = 0;233  let totalTests = 0;234 235  results.forEach((res, i) => {236    const p = problems[i];237    const d = res.body.data;238 239    if (!d) {240      console.log(`  ❌ ${p.name}: ERROR — ${JSON.stringify(res.body)}\n`);241      return;242    }243 244    const icon = d.overallVerdict === "AC" ? "✅" : "❌";245    totalPassed += d.passed;246    totalTests += d.totalTestCases;247 248    console.log(`  ${icon} ${p.name}`);249    console.log(`     Verdict:  ${d.overallVerdict}`);250    console.log(`     Passed:   ${d.passed}/${d.totalTestCases}`);251    console.log(`     Time:     ${d.totalTime}ms`);252    console.log(253      `     Memory:   ${d.maxMemory}KB (${(d.maxMemory / 1024).toFixed(1)}MB)`,254    );255 256    // Show failed test cases257    if (d.overallVerdict !== "AC") {258      d.results259        .filter((r) => r.verdict !== "AC")260        .slice(0, 3)261        .forEach((r) => {262          console.log(`     ── TC #${r.testCase}: ${r.verdict}`);263          if (r.verdict === "WA") {264            console.log(265              `        Expected: ${r.expectedOutput.substring(0, 50)}`,266            );267            console.log(`        Got:      ${r.actualOutput.substring(0, 50)}`);268          }269          if (r.error)270            console.log(`        Error: ${r.error.substring(0, 100)}`);271        });272    }273 274    console.log("");275  });276 277  // ─── Summary ───278  console.log("═══════════════════════════════════════════════════════════");279  console.log("  SUMMARY");280  console.log("═══════════════════════════════════════════════════════════");281  console.log(`  Total test cases:    ${totalTests}`);282  console.log(`  Passed:              ${totalPassed}/${totalTests}`);283  console.log(284    `  Total wall time:     ${totalTime}ms (${(totalTime / 1000).toFixed(1)}s)`,285  );286  console.log(`  Avg per problem:     ${(totalTime / 5).toFixed(0)}ms`);287  console.log(`  Concurrency:         5 simultaneous requests`);288  console.log(289    `  Throughput:          ${((totalTests / totalTime) * 1000).toFixed(1)} test cases/sec`,290  );291  console.log("═══════════════════════════════════════════════════════════\n");292}293 294run().catch((err) => {295  console.error("❌ Test failed:", err.message);296});297