-
Notifications
You must be signed in to change notification settings - Fork 137
Add simple benchmark script #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| name: Nightly | ||
|
|
||
| on: | ||
| schedule: | ||
| # (12 AM PST) | ||
| - cron: "00 07 * * *" | ||
|
|
||
| jobs: | ||
| nightly: | ||
| uses: ./.github/workflows/run-bench.yml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| name: Run Bench | ||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| sandbox-arg: | ||
| description: "Sandbox argument" | ||
| required: false | ||
| default: "--sandbox" | ||
| type: string | ||
| workflow_dispatch: | ||
| inputs: | ||
| sandbox-arg: | ||
| description: "Sandbox argument" | ||
| required: false | ||
| default: "--sandbox" | ||
| type: choice | ||
| options: | ||
| - "--sandbox" | ||
| - "--no-sandbox" | ||
|
|
||
| jobs: | ||
| run-bench: | ||
| strategy: | ||
| matrix: | ||
| os: [ubuntu-latest-4-cores, windows-latest] | ||
| runs-on: ${{ matrix.os }} | ||
| steps: | ||
| # Prepare | ||
| - uses: actions/checkout@v2 | ||
| with: | ||
| submodules: recursive | ||
| - uses: actions-rs/toolchain@v1 | ||
| with: | ||
| toolchain: stable | ||
| - uses: Swatinem/rust-cache@v1 | ||
| with: | ||
| working-directory: temporalio/bridge | ||
| - uses: actions/setup-python@v4 | ||
| with: | ||
| python-version: "3.11" | ||
|
|
||
| # Build | ||
| - run: python -m pip install --upgrade wheel poetry poethepoet | ||
| - run: poetry install --no-root -E opentelemetry | ||
| - run: poe build-develop-with-release | ||
|
|
||
| # Run a bunch of bench tests. We run multiple times since results vary. | ||
|
|
||
| - run: poe run-bench --workflow-count 100 --max-cached-workflows 100 --max-concurrent 100 ${{ inputs.sandbox-arg }} | ||
| - run: poe run-bench --workflow-count 100 --max-cached-workflows 100 --max-concurrent 100 ${{ inputs.sandbox-arg }} | ||
| - run: poe run-bench --workflow-count 100 --max-cached-workflows 100 --max-concurrent 100 ${{ inputs.sandbox-arg }} | ||
|
|
||
| - run: poe run-bench --workflow-count 1000 --max-cached-workflows 1000 --max-concurrent 1000 ${{ inputs.sandbox-arg }} | ||
| - run: poe run-bench --workflow-count 1000 --max-cached-workflows 1000 --max-concurrent 1000 ${{ inputs.sandbox-arg }} | ||
| - run: poe run-bench --workflow-count 1000 --max-cached-workflows 1000 --max-concurrent 1000 ${{ inputs.sandbox-arg }} | ||
|
|
||
| - run: poe run-bench --workflow-count 1000 --max-cached-workflows 100 --max-concurrent 100 ${{ inputs.sandbox-arg }} | ||
| - run: poe run-bench --workflow-count 1000 --max-cached-workflows 100 --max-concurrent 100 ${{ inputs.sandbox-arg }} | ||
| - run: poe run-bench --workflow-count 1000 --max-cached-workflows 100 --max-concurrent 100 ${{ inputs.sandbox-arg }} | ||
|
|
||
| - run: poe run-bench --workflow-count 10000 --max-cached-workflows 10000 --max-concurrent 10000 ${{ inputs.sandbox-arg }} | ||
| - run: poe run-bench --workflow-count 10000 --max-cached-workflows 10000 --max-concurrent 10000 ${{ inputs.sandbox-arg }} | ||
|
|
||
| - run: poe run-bench --workflow-count 10000 --max-cached-workflows 1000 --max-concurrent 1000 ${{ inputs.sandbox-arg }} | ||
| - run: poe run-bench --workflow-count 10000 --max-cached-workflows 1000 --max-concurrent 1000 ${{ inputs.sandbox-arg }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import argparse | ||
| import asyncio | ||
| import json | ||
| import logging | ||
| import sys | ||
| import time | ||
| import uuid | ||
| from contextlib import asynccontextmanager | ||
| from datetime import timedelta | ||
| from typing import AsyncIterator | ||
|
|
||
| from temporalio import activity, workflow | ||
| from temporalio.testing import WorkflowEnvironment | ||
| from temporalio.worker import UnsandboxedWorkflowRunner, Worker | ||
| from temporalio.worker.workflow_sandbox import SandboxedWorkflowRunner | ||
|
|
||
|
|
||
| @workflow.defn | ||
| class BenchWorkflow: | ||
| @workflow.run | ||
| async def run(self, name: str) -> str: | ||
| return await workflow.execute_activity( | ||
| bench_activity, name, start_to_close_timeout=timedelta(seconds=30) | ||
| ) | ||
|
|
||
|
|
||
| @activity.defn | ||
| async def bench_activity(name: str) -> str: | ||
| return f"Hello, {name}!" | ||
|
|
||
|
|
||
| async def main(): | ||
| logging.basicConfig( | ||
| format="%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s", | ||
| level=logging.WARN, | ||
| datefmt="%Y-%m-%d %H:%M:%S", | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| max_mem = -1 | ||
|
|
||
| parser = argparse.ArgumentParser(description="Run bench") | ||
| parser.add_argument("--workflow-count", type=int, required=True) | ||
| parser.add_argument("--sandbox", action=argparse.BooleanOptionalAction) | ||
| parser.add_argument("--max-cached-workflows", type=int, required=True) | ||
| parser.add_argument("--max-concurrent", type=int, required=True) | ||
| args = parser.parse_args() | ||
|
|
||
| @asynccontextmanager | ||
| async def track_mem() -> AsyncIterator[None]: | ||
| # We intentionally import in here so the sandbox doesn't grow huge with | ||
| # this import | ||
| import psutil | ||
|
|
||
| # Get mem every 800ms | ||
| process = psutil.Process() | ||
|
|
||
| async def report_mem(): | ||
| nonlocal max_mem | ||
| while True: | ||
| try: | ||
| await asyncio.sleep(0.8) | ||
| finally: | ||
| # TODO(cretz): "vms" appears more accurate on Windows, but | ||
| # rss is more accurate on Linux | ||
| used_mem = process.memory_info().rss | ||
| if used_mem > max_mem: | ||
| max_mem = used_mem | ||
|
|
||
| report_mem_task = asyncio.create_task(report_mem()) | ||
| try: | ||
| yield None | ||
| finally: | ||
| report_mem_task.cancel() | ||
|
|
||
| logger.info("Running %s workflows", args.workflow_count) | ||
| async with track_mem(): | ||
| # Run with a local workflow environment | ||
| logger.debug("Starting local environment") | ||
| async with await WorkflowEnvironment.start_local() as env: | ||
| task_queue = f"task-queue-{uuid.uuid4()}" | ||
|
|
||
| # Create a bunch of workflows | ||
| logger.debug("Starting %s workflows", args.workflow_count) | ||
| pre_start_seconds = time.monotonic() | ||
| handles = [ | ||
| await env.client.start_workflow( | ||
| BenchWorkflow.run, | ||
| f"user-{i}", | ||
| id=f"workflow-{i}-{uuid.uuid4()}", | ||
| task_queue=task_queue, | ||
| ) | ||
| for i in range(args.workflow_count) | ||
| ] | ||
| start_seconds = time.monotonic() - pre_start_seconds | ||
|
|
||
| # Start a worker to run them | ||
| logger.debug("Starting worker") | ||
| async with Worker( | ||
| env.client, | ||
| task_queue=task_queue, | ||
| workflows=[BenchWorkflow], | ||
| activities=[bench_activity], | ||
| workflow_runner=SandboxedWorkflowRunner() | ||
| if args.sandbox | ||
| else UnsandboxedWorkflowRunner(), | ||
| max_cached_workflows=args.max_cached_workflows, | ||
| max_concurrent_workflow_tasks=args.max_concurrent, | ||
| max_concurrent_activities=args.max_concurrent, | ||
| ): | ||
| logger.debug("Worker started") | ||
| # Wait for them all | ||
| pre_result_seconds = time.monotonic() | ||
| for h in handles: | ||
| await h.result() | ||
| result_seconds = time.monotonic() - pre_result_seconds | ||
| logger.debug("All workflows complete") | ||
|
|
||
| # Print results | ||
| json.dump( | ||
| { | ||
| "workflow_count": args.workflow_count, | ||
| "sandbox": args.sandbox or False, | ||
| "max_cached_workflows": args.max_cached_workflows, | ||
| "max_concurrent": args.max_concurrent, | ||
| "max_mem_mib": round(max_mem / 1024**2, 1), | ||
| "start_seconds": round(start_seconds, 1), | ||
| "result_seconds": round(result_seconds, 1), | ||
| "workflows_per_second": round(args.workflow_count / result_seconds, 1), | ||
| }, | ||
| sys.stdout, | ||
| indent=2, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth waiting for the task to be cancelled here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just interrupts a sleep, no value I don't think, but I guess I could