import os
import sys
import time
from conch import Sandbox
def perf_print_hello_by_image():
t0 = time.perf_counter()
try:
box = Sandbox.create()
print(f'sandbox {box.sandbox_id} created')
ret = box.execute(cmd='python3', content='import os; print(1+1)')
output = ret.stdout.strip()
if output != "2":
print(f"Calculation failed at EOF, output: {output}")
return None
t1 = time.perf_counter()
print(f'image create print hello cost {t1 - t0:.3f}s')
snapshot = box.pause()
if snapshot is None:
raise RuntimeError("pause failed, no snapshot created")
print(f'snapshot {snapshot.snapshot_id} created, reaching EOF of image setup')
return snapshot.snapshot_id
except RuntimeError as e:
print(f'Error: {e}')
return None
def perf_print_hello_by_snapshot(snapshot_id):
if not snapshot_id:
return
t0 = time.perf_counter()
try:
box = Sandbox.create(snapshot_id)
ret = box.execute(cmd='python3', content='import os; print(1+1)')
if ret.stdout.strip() == "2":
t1 = time.perf_counter()
print(f'snapshot create print hello cost {t1 - t0:.3f}s')
print("Process reached EOF successfully.")
box.delete()
except RuntimeError as e:
print(f'Error: {e}')
def main():
snapshot_id = perf_print_hello_by_image()
if snapshot_id:
perf_print_hello_by_snapshot(snapshot_id)
else:
print("Process terminated before EOF.")
if __name__ == '__main__':
main()