# MyHomeWork **Repository Path**: xilong008/MyHomeWork ## Basic Information - **Project Name**: MyHomeWork - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-04-10 - **Last Updated**: 2026-04-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README Implementation Steps: 1. Create SessionManager.java to handle session management. - Requires 3 concurrent maps: id-session map, session-id map, and session-timeout map. 2. Create StakeManager.java to manage the prize pool and stake data. 3. Create RouterHandler.java to process HTTP requests. 4. Create Application.java to bootstrap and start the server. JDK8 api doc -https://docs.oracle.com/javase/8/docs/api/index.html https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html k6 Load Testing Configuration The application performance is tested under heavy concurrency using [k6](https://k6.io/). **Run Command:** `k6 run load_test.js` Test Stages - Ramp-up: Scale from 0 to 2,000 VUs over 5 seconds. - Peak Load: Maintain a massive peak of 5,000 concurrent VUS for 15 seconds. - Ramp-down: Gradually scale down back to 0 VUs over 5 seconds. KPI Thresholds: - http_req_duration: The 95th percentile response time (P95) must be strictly under 200 milliseconds (p(95) < 200). - http_req_failed: The total API failure rate must remain below 1% (rate < 0.01). Reports: An automated visual HTML report is generated at `./report/summary.html` via k6-reporter after execution. V2 Optimizations Memory & Algorithm: - Added a background Timer to clean up expired sessions. - Replaced full sorting with a PriorityQueue to only keep the top 20 scores. - Added a threshold value to drop low scores before they enter the map. Performance & Throughput: - Replaced regex "split" with manual "indexOf" for faster path parsing and less GC. - Replaced UUID with ThreadLocalRandom to avoid global locks under high concurrency. - Built an O(1) Cache-Aside map for static leaderboard reads. - Increased HttpServer TCP backlog queue from 0 to 5000 to prevent connection refused errors. Security & Anti-Spam: - Catch invalid number injections and return HTTP 400. - Drop negative and zero stakes immediately. - Added an IP-based rate limiter to block IPs exceeding 50 requests per second with HTTP 429. V3 Optimizations Concurrency & Thread Safety: - Eliminated `check-then-act` race conditions in SessionManager using atomic `compute()`. - Introduced fine-grained, `betid`-level mutex locks to avoid heavy global locks. - Implemented Double-Checked Locking on cache lookups to secure strong Cache Coherency. Memory Leak & Expansion Guard: - Implemented an active memory bouncer to auto-drop stakes when pool size breaks 200. - Added dynamic pool LRU tracking to clear unvisited, stale `betid` maps completely. ## V3 Performance Test Report: `report/summary_v3.html` ![image](report/summary_v3.html)The backend achieved outstanding stability under extreme 5,000 VUs peak traffic: - **Throughput**: Processed 340,000+ total requests smoothly. - **Avg Latency**: Ultra-low 9.76 milliseconds average response time. - **Data Coherency**: Achieved a flawless **99.90%** consistency pass rate. - **Cache Efficiency**: Over 49.40% read queries served directly by memory cache.