-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark-pool-python.ts
More file actions
54 lines (46 loc) · 1.48 KB
/
benchmark-pool-python.ts
File metadata and controls
54 lines (46 loc) · 1.48 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
import { ExecutionEngine, ContainerStrategy } from '../src';
process.on('unhandledRejection', r => console.error('UNHANDLED', r));
process.on('uncaughtException', e => console.error('UNCAUGHT', e));
async function main() {
const engine = new ExecutionEngine();
const rounds = 20;
const executionTimes: number[] = [];
try {
const sessionId = await engine.createSession({
strategy: ContainerStrategy.POOL,
containerConfig: {
image: 'python:3.9-slim'
}
});
console.log(`Running Python benchmark with ${rounds} rounds...`);
for (let i = 1; i <= rounds; i++) {
try {
const result = await engine.executeCode(sessionId, {
language: 'python',
code: `
nums = list(range(1, 6))
sum_val = sum(nums)
avg_val = sum_val / len(nums)
print('Round', ${i}, ':', {'sum': sum_val, 'avg': avg_val})
`,
streamOutput: {
stdout: (d) => process.stdout.write(d),
stderr: (d) => process.stderr.write(d)
}
});
console.log(`Round ${i} execution time: ${result.executionTime} ms`);
executionTimes.push(result.executionTime);
} catch (err) {
console.error(`Round ${i} failed:`, err);
break;
}
}
const total = executionTimes.reduce((a, b) => a + b, 0);
console.log('Average per round:', (total / rounds).toFixed(2), 'ms');
} catch (e) {
console.error('Benchmark error:', e);
} finally {
await engine.cleanup();
}
}
main();