Skip to content
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

feat: workflow-benchmarking #2980

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
55 changes: 55 additions & 0 deletions keep/api/routes/workflows.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import logging
import os
import time
from typing import Any, Dict, List, Optional

import validators
Expand All @@ -26,6 +27,7 @@
get_session,
get_workflow,
get_workflow_by_name,
get_workflow_executions_count,
)
from keep.api.core.db import get_workflow_executions as get_workflow_executions_db
from keep.api.models.alert import AlertDto, IncidentDto
Expand Down Expand Up @@ -748,3 +750,56 @@ def get_workflow_execution_status(
event_type=event_type,
)
return workflow_execution_dto


@router.post(
"/{workflow_id}/benchmark",
description="Benchmark a workflow",
)
def benchmark_workflow(
workflow_id: str,
authenticated_entity: AuthenticatedEntity = Depends(
IdentityManagerFactory.get_auth_verifier(["write:workflows"])
),
):
if os.environ.get("KEEP_WF_BENCHMARK_ENABLED", "").lower() != "true":
return HTTPException(status_code=405, detail="Workflow benchmarking is not avaliable")

TOTAL_WORKFLOWS_TO_RUN = 1000
tenant_id = authenticated_entity.tenant_id
created_by = authenticated_entity.email
workflowmanager = WorkflowManager.get_instance()

event = AlertDto({"tenant_id": tenant_id, "id": "benchmark", "name": "benchmark"})

time_start = datetime.datetime.now()

result_lines = []
result_lines.append(f"Started {TOTAL_WORKFLOWS_TO_RUN} workflows")

def get_total_workflow_executions_count(tenant_id):
wf_executions_before_benchmark = get_workflow_executions_count(tenant_id)
return wf_executions_before_benchmark.get("success", 0)

total_wf_executions_before_benchmark = get_total_workflow_executions_count(tenant_id)

for _ in range(1, TOTAL_WORKFLOWS_TO_RUN):
workflowmanager.scheduler.handle_manual_event_workflow(
workflow_id, tenant_id, created_by, event
)

result_lines.append(f"Scheduling took {datetime.datetime.now() - time_start}")
execution_time_start = datetime.datetime.now()

while get_total_workflow_executions_count(tenant_id) - \
total_wf_executions_before_benchmark < TOTAL_WORKFLOWS_TO_RUN - 1:

time.sleep(1)
result_lines.append(f"Time: {datetime.datetime.now() - time_start}, workflows: {get_total_workflow_executions_count(tenant_id) - total_wf_executions_before_benchmark}")
print("\n".join(result_lines))

result_lines.append(f"Finished {TOTAL_WORKFLOWS_TO_RUN} workflows in {datetime.datetime.now() - time_start}")
result_lines.append(f"After scheduling finished: {datetime.datetime.now() - execution_time_start}")
result_lines.append(f"WF/second: {TOTAL_WORKFLOWS_TO_RUN / (datetime.datetime.now() - execution_time_start).total_seconds()}")

return "\r\n".join(result_lines)
Loading