1
0
Fork 0
mirror of https://github.com/Findus23/cr-search.git synced 2024-09-19 15:23:44 +02:00
cr-search/benchmark.py

58 lines
1.9 KiB
Python
Raw Permalink Normal View History

2020-08-15 12:28:29 +02:00
import json
import shutil
from statistics import mean, stdev
from alive_progress import alive_bar
from peewee import SelectQuery
from psycopg2._psycopg import cursor
2021-07-07 17:27:21 +02:00
from app import db
from server import search, suggest, exact_search
2020-08-15 12:28:29 +02:00
2023-04-25 22:51:19 +02:00
def benchmark_query(query: SelectQuery, filename: str = None) -> tuple[float, float]:
2021-07-07 17:27:21 +02:00
query, params = query.sql()
2020-08-15 12:28:29 +02:00
query = "EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON) " + query
cur: cursor = db.execute_sql(query, params=params)
result = cur.fetchone()[0][0]
if filename:
with open(f"benchmark/{filename}.json", "w") as f:
json.dump(result, f, indent=2)
with open(f"benchmark/{filename}.txt", "w") as f:
f.write(query)
return result["Planning Time"], result["Execution Time"]
2020-08-30 22:11:28 +02:00
def statistics(query: SelectQuery, filename: str, repeats: int = 500) -> None:
2020-08-15 12:28:29 +02:00
ts = shutil.get_terminal_size((80, 20))
print(filename.center(ts.columns, "-"))
planning_times = []
execution_times = []
benchmark_query(query, filename=filename)
with alive_bar(repeats) as bar:
for i in range(repeats):
plantime, exetime = benchmark_query(query)
planning_times.append(plantime)
execution_times.append(exetime)
bar()
print(mean(planning_times), stdev(planning_times))
print(mean(execution_times), stdev(execution_times))
2021-07-07 17:27:21 +02:00
test_search = search("hello", 1000, "campaign2", 200)
2020-08-15 12:28:29 +02:00
statistics(test_search, filename="search_hello")
2021-07-07 17:27:21 +02:00
test_search = exact_search("hello", 1000, "campaign2", 200)
statistics(test_search, filename="exact_search", repeats=50)
test_search = search("a very long search query with a lot of stop word", 1000, "campaign2", 200)
2020-08-15 12:28:29 +02:00
statistics(test_search, filename="search_long")
2021-07-07 17:27:21 +02:00
test_search = suggest("gnoll", 1000, "campaign2")
2020-08-15 12:28:29 +02:00
statistics(test_search, filename="suggest_simple")
2021-07-07 17:27:21 +02:00
test_search = suggest("gu", 1000, "campaign2")
2020-08-15 12:28:29 +02:00
statistics(test_search, filename="suggest_two_letter", repeats=100)