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: rewrite run_query in python #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
env
errors.log
result.csv
5 changes: 5 additions & 0 deletions clickbench/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
certifi==2024.12.14
charset-normalizer==3.4.0
idna==3.10
requests==2.32.3
urllib3==2.2.3
65 changes: 65 additions & 0 deletions clickbench/run_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import json
import requests
import time

# Read configuration from environment variables, with defaults
P_URL = os.getenv("P_URL", "https://demo.parseable.com:8000")
API_URL = P_URL + "/api/v1/query"
P_USERNAME = os.getenv("P_USERNAME", "admin")
P_PASSWORD = os.getenv("P_PASSWORD", "admin")
QUERY_FILE = os.getenv("QUERY_FILE", "queries.sql")
RESULT_FILE = os.getenv("RESULT_FILE", "result.csv")
ERROR_LOG = os.getenv("ERROR_LOG", "errors.log")

# Prepare the result CSV file
with open(RESULT_FILE, "w") as result_file:
result_file.write("QueryNumber,ElapsedTime(ms)\n")

# Process queries
query_num = 1

with open(QUERY_FILE, "r") as query_file:
for line in query_file:
query = line.strip()
if not query: # Skip empty lines
continue

# Prepare the JSON payload
payload = {
"query": query,
"startTime": "2024-11-29T00:00:00.000Z",
"endTime": "2024-11-29T23:00:00.000Z"
}

# Log start time
start_time = int(time.time() * 1000)

print(f"{query} => ", end="", flush=True)
try:
response = requests.post(
API_URL,
json=payload,
auth=(P_USERNAME, P_PASSWORD),
headers={"Content-Type": "application/json"},
verify=False
)

if response.status_code != 200:
raise Exception(f"HTTP {response.status_code}: {response.text}")

# Log end time and calculate elapsed time
end_time = int(time.time() * 1000)
elapsed_time = end_time - start_time

print(f"{response.text} ({elapsed_time}ms)")

# Write results to CSV
with open(RESULT_FILE, "a") as result_file:
result_file.write(f"{query_num},{elapsed_time}\n")
except Exception as e:
print("Failed")
with open(ERROR_LOG, "a") as error_log:
error_log.write(f"Error querying {query_num}: {str(e)}\n")

query_num += 1
Loading