Skip to content

Commit

Permalink
add chunk timing example
Browse files Browse the repository at this point in the history
  • Loading branch information
shroominic committed Oct 30, 2024
1 parent a45f814 commit 3169dbd
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions examples/stream_chunk_timing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import asyncio
import time

from codeboxapi import CodeBox, ExecChunk


def sync_stream_exec(cb: CodeBox) -> None:
chunks: list[tuple[ExecChunk, float]] = []
t0 = time.perf_counter()
for chunk in cb.stream_exec(
"import time;\nfor i in range(3): time.sleep(1); print(i)"
):
chunks.append((chunk, time.perf_counter() - t0))

for chunk, t in chunks:
print(f"{t:.5f}: {chunk}")


async def async_stream_exec(cb: CodeBox) -> None:
chunks: list[tuple[ExecChunk, float]] = []
t0 = time.perf_counter()
async for chunk in cb.astream_exec(
"import time;\nfor i in range(3): time.sleep(1); print(i)"
):
chunks.append((chunk, time.perf_counter() - t0))

for chunk, t in chunks:
print(f"{t:.5f}: {chunk}")


print("remote")
cb = CodeBox()
sync_stream_exec(cb)
asyncio.run(async_stream_exec(cb))

print("local")
local = CodeBox(api_key="local")
sync_stream_exec(local)
asyncio.run(async_stream_exec(local))

print("docker")
docker = CodeBox(api_key="docker")
sync_stream_exec(docker)
asyncio.run(async_stream_exec(docker))

0 comments on commit 3169dbd

Please sign in to comment.