Skip to content

Commit

Permalink
feat: rewrite run_query in python
Browse files Browse the repository at this point in the history
  • Loading branch information
de-sh committed Dec 16, 2024
1 parent 7a429e3 commit 8dc83b0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
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

0 comments on commit 8dc83b0

Please sign in to comment.