-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress-pi-server.js
More file actions
92 lines (75 loc) · 2.49 KB
/
express-pi-server.js
File metadata and controls
92 lines (75 loc) · 2.49 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
const express = require('express');
const app = express();
app.use(express.json());
// Warm up function
function warmupEngine() {
for (let i = 0; i < 5; i++) {
calculatePiLeibniz(100000);
}
}
// Calculate π using Leibniz formula
function calculatePiLeibniz(iterations) {
let pi = 0.0;
for (let i = 0; i < iterations; i++) {
const term = 1.0 / (2 * i + 1);
if (i % 2 === 0) {
pi += term;
} else {
pi -= term;
}
}
return pi * 4.0;
}
app.post('/api/pi/calculate', (req, res) => {
const startTime = process.hrtime.bigint();
try {
let iterations = req.body.iterations || 1000000;
if (iterations <= 0) iterations = 1000000;
// Warm up for large calculations
if (iterations > 10000000) {
warmupEngine();
}
const pi = calculatePiLeibniz(iterations);
const endTime = process.hrtime.bigint();
const elapsedMs = Number(endTime - startTime) / 1000000;
const accuracy = Math.abs(Math.PI - pi);
res.json({
result: pi,
actualPi: Math.PI,
error: accuracy,
iterations: iterations,
timeMs: Math.round(elapsedMs * 100) / 100,
iterationsPerSecond: Math.round(iterations / elapsedMs * 1000),
runtime: `Express + Node.js ${process.version}`,
threadType: "Single Thread (Event Loop)"
});
} catch (error) {
const endTime = process.hrtime.bigint();
const elapsedMs = Number(endTime - startTime) / 1000000;
res.status(500).json({
error: 'π calculation failed',
message: error.message,
timeMs: Math.round(elapsedMs * 100) / 100
});
}
});
app.get('/api/pi/health', (req, res) => {
const memUsage = process.memoryUsage();
res.json({
status: 'ok',
timestamp: new Date(),
runtime: 'Express π Calculator',
nodeVersion: process.version,
virtualThreads: false,
availableProcessors: require('os').cpus().length,
memoryUsage: memUsage
});
});
app.listen(8080, '0.0.0.0', () => {
console.log('🚀 Express π Calculator running on port 8080');
console.log(`📊 Runtime: Node.js ${process.version}`);
console.log('Ready to calculate π with Leibniz formula...');
});
// Warm up V8 on startup
console.log('⚡ Warming up V8 engine...');
warmupEngine();