-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathstartup_performance.py
More file actions
130 lines (104 loc) · 3.72 KB
/
startup_performance.py
File metadata and controls
130 lines (104 loc) · 3.72 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# /// script
# requires-python = ">=3.14"
# dependencies = [
# "daytona>=0.136.0",
# "mcp-run-python>=0.0.22",
# "pydantic-monty>=0.0.1",
# "starlark-pyo3>=2025.2.5",
# ]
# ///
import asyncio
import os
import subprocess
import time
from typing import Any
from mcp_run_python import code_sandbox
from pydantic_monty import Monty
code = '1 + 1'
def run_monty():
start = time.perf_counter()
result = Monty('1 + 1').run()
diff = time.perf_counter() - start
assert result == 2, f'Unexpected result: {result!r}'
print(f'Monty cold start time: {(diff * 1000):.3f} milliseconds')
def run_pyodide():
async def run() -> Any:
async with code_sandbox(dependencies=['numpy']) as sandbox:
return await sandbox.eval(code)
start = time.perf_counter()
result = asyncio.run(run())
diff = time.perf_counter() - start
assert result == {'status': 'success', 'output': [], 'return_value': 2}, f'Unexpected result: {result!r}'
print(f'Pyodide cold start time: {(diff * 1000):.3f} milliseconds')
def run_docker():
start = time.perf_counter()
result = subprocess.run(
['docker', 'run', '--rm', 'python:3.14-alpine', 'python', '-c', f'print({code})'],
capture_output=True,
text=True,
)
diff = time.perf_counter() - start
output = result.stdout.strip()
assert output == '2', f'Unexpected result: {output!r}'
print(f'Docker cold start time: {(diff * 1000):.3f} milliseconds')
def run_starlark():
import starlark as sl
start = time.perf_counter()
glb = sl.Globals.standard()
mod = sl.Module()
ast = sl.parse('bench.star', code)
result = sl.eval(mod, ast, glb)
diff = time.perf_counter() - start
assert result == 2, f'Unexpected result: {result!r}'
print(f'Starlark cold start time: {(diff * 1000):.3f} milliseconds')
def run_daytona():
from daytona import Daytona, DaytonaConfig
api_key = os.getenv('DAYTONA_API_KEY')
if not api_key:
print('DAYTONA_API_KEY environment variable is not set, skipping daytona')
return
# Initialize the Daytona client
daytona = Daytona(DaytonaConfig(api_key=api_key))
start = time.perf_counter()
response = daytona.create().process.code_run(f'print({code})')
diff = time.perf_counter() - start
assert response.result == '2', f'Unexpected result: {response.result!r}'
print(f'Daytona cold start time: {(diff * 1000):.3f} milliseconds')
def run_wasmer():
# requires wasmer to be installed, see https://docs.wasmer.io/install
start = time.perf_counter()
result = subprocess.run(
['wasmer', 'run', 'python/python', '--', '-c', f'print({code})'],
capture_output=True,
text=True,
)
diff = time.perf_counter() - start
output = result.stdout.strip()
assert output == '2', f'Unexpected result: {output!r}'
print(f'Wasmer cold start time: {(diff * 1000):.3f} milliseconds')
def run_subprocess_python():
start = time.perf_counter()
result = subprocess.run(
['python', '-c', f'print({code})'],
capture_output=True,
text=True,
)
diff = time.perf_counter() - start
output = result.stdout.strip()
assert output == '2', f'Unexpected result: {output!r}'
print(f'Subprocess Python cold start time: {(diff * 1000):.3f} milliseconds')
def run_exec_python():
start = time.perf_counter()
result = eval(code)
diff = time.perf_counter() - start
assert result == 2, f'Unexpected result: {result!r}'
print(f'Exec Python cold start time: {(diff * 1000):.3f} milliseconds')
if __name__ == '__main__':
run_monty()
run_pyodide()
run_docker()
run_starlark()
run_daytona()
run_wasmer()
run_subprocess_python()
run_exec_python()