Repository navigation
Expand file tree
/
Copy pathfind_cheapest.py
More file actions
177 lines (148 loc) · 4.96 KB
/
Copy pathfind_cheapest.py
File metadata and controls
177 lines (148 loc) · 4.96 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""Benchmark sandbox backends and find the cheapest one for your workload.
Spins up each backend, runs a benchmark workload, measures cold-start and
execution time, then estimates cost using the bundled pricing data.
Usage:
python examples/find_cheapest.py
python examples/find_cheapest.py --backends local,docker,e2b
python examples/find_cheapest.py --sequential
"""
import argparse
import concurrent.futures
import json
import sys
import time
from dataclasses import asdict, dataclass
from bespokelabs.sandbox import Sandbox
from bespokelabs.sandbox.pricing import (
cost_per_second,
get_backend_pricing,
list_backends,
)
BENCHMARK_CODE = """
import math, hashlib, json
result = sum(math.sqrt(i) for i in range(10_000))
h = hashlib.sha256(json.dumps({"result": result}).encode()).hexdigest()
print(h)
"""
@dataclass
class BenchmarkResult:
backend: str
cold_start_secs: float
exec_secs: float
total_secs: float
estimated_cost_usd: float
success: bool
error: str = ""
def benchmark_backend(backend: str) -> BenchmarkResult:
sb = None
try:
t0 = time.perf_counter()
sb = Sandbox(backend)
cold_start = time.perf_counter() - t0
t1 = time.perf_counter()
result = sb.execute_code(BENCHMARK_CODE)
exec_time = time.perf_counter() - t1
total = cold_start + exec_time
cps = cost_per_second(backend)
cost = cps * total
return BenchmarkResult(
backend=backend,
cold_start_secs=round(cold_start, 3),
exec_secs=round(exec_time, 3),
total_secs=round(total, 3),
estimated_cost_usd=cost,
success=result.exit_code == 0,
)
except Exception as e:
return BenchmarkResult(
backend=backend,
cold_start_secs=0,
exec_secs=0,
total_secs=0,
estimated_cost_usd=float("inf"),
success=False,
error=str(e),
)
finally:
if sb is not None:
sb.destroy()
def find_cheapest(
backends: list[str], parallel: bool = True
) -> list[BenchmarkResult]:
if parallel:
with concurrent.futures.ThreadPoolExecutor() as ex:
results = list(ex.map(benchmark_backend, backends))
else:
results = [benchmark_backend(b) for b in backends]
results.sort(key=lambda r: (not r.success, r.estimated_cost_usd))
return results
def print_table(results: list[BenchmarkResult]) -> None:
print(
f"\n{'Backend':<12} {'Cold(s)':<10} {'Exec(s)':<10} {'Total(s)':<10} {'Est. Cost $':<14} {'OK'}"
)
print("-" * 70)
for r in results:
status = "yes" if r.success else f"no {r.error[:30]}"
print(
f"{r.backend:<12} {r.cold_start_secs:<10} {r.exec_secs:<10} "
f"{r.total_secs:<10} {r.estimated_cost_usd:<14.8f} {status}"
)
successful = [r for r in results if r.success]
if successful:
winner = successful[0]
print(
f"\n-> Cheapest available: {winner.backend} (${winner.estimated_cost_usd:.8f}/run)"
)
def print_pricing_table(backends: list[str]) -> None:
print(
f"\n{'Backend':<12} {'$/vCPU-hr':<12} {'$/GiB-hr':<12} {'Free tier':<30} {'Source'}"
)
print("-" * 90)
for b in backends:
info = get_backend_pricing(b)
if info is None:
print(f"{b:<12} {'?':<12} {'?':<12} {'?':<30} ?")
continue
vcpu = info.get("vcpu_per_hour_usd", 0.0)
ram = info.get("ram_gib_per_hour_usd", 0.0)
free = info.get("free_tier") or "-"
url = info.get("pricing_url") or "-"
print(f"{b:<12} {vcpu:<12.4f} {ram:<12.4f} {free:<30} {url}")
def main() -> None:
parser = argparse.ArgumentParser(
description="Benchmark sandbox backends and find the cheapest"
)
parser.add_argument(
"--backends",
default="local,docker",
help=f"Comma-separated backends to benchmark (available: {', '.join(list_backends())})",
)
parser.add_argument(
"--sequential",
action="store_true",
help="Run benchmarks sequentially instead of in parallel",
)
parser.add_argument(
"--pricing-only",
action="store_true",
help="Just print pricing table, don't benchmark",
)
parser.add_argument(
"--json", action="store_true", help="Output results as JSON"
)
args = parser.parse_args()
backends = [b.strip() for b in args.backends.split(",") if b.strip()]
if args.pricing_only:
print_pricing_table(backends)
return
if not args.json:
print(f"Benchmarking: {', '.join(backends)}")
print_pricing_table(backends)
print("\nRunning benchmarks...")
results = find_cheapest(backends, parallel=not args.sequential)
if args.json:
print(json.dumps([asdict(r) for r in results], indent=2))
else:
print_table(results)
if __name__ == "__main__":
sys.exit(main() or 0)