1 Star 0 Fork 0

john_yuan/baoforpostgresql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
run_queries.py 1.87 KB
一键复制 编辑 原始数据 按行查看 历史
Ryan Marcus 提交于 2020-08-01 22:30 . Initial files
import psycopg2
import os
import sys
import random
from time import time, sleep
USE_BAO = True
PG_CONNECTION_STR = "dbname=imdb user=imdb host=localhost"
# https://stackoverflow.com/questions/312443/
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
def run_query(sql, bao_select=False, bao_reward=False):
start = time()
while True:
try:
conn = psycopg2.connect(PG_CONNECTION_STR)
cur = conn.cursor()
cur.execute(f"SET enable_bao TO {bao_select or bao_reward}")
cur.execute(f"SET enable_bao_selection TO {bao_select}")
cur.execute(f"SET enable_bao_rewards TO {bao_reward}")
cur.execute("SET bao_num_arms TO 5")
cur.execute("SET statement_timeout TO 300000")
cur.execute(q)
cur.fetchall()
conn.close()
break
except:
sleep(1)
continue
stop = time()
return stop - start
query_paths = sys.argv[1:]
queries = []
for fp in query_paths:
with open(fp) as f:
query = f.read()
queries.append((fp, query))
print("Read", len(queries), "queries.")
print("Using Bao:", USE_BAO)
random.seed(42)
query_sequence = random.choices(queries, k=500)
pg_chunks, *bao_chunks = list(chunks(query_sequence, 25))
print("Executing queries using PG optimizer for initial training")
for fp, q in pg_chunks:
pg_time = run_query(q, bao_reward=True)
print("x", "x", time(), fp, pg_time, "PG", flush=True)
for c_idx, chunk in enumerate(bao_chunks):
if USE_BAO:
os.system("cd bao_server && python3 baoctl.py --retrain")
os.system("sync")
for q_idx, (fp, q) in enumerate(chunk):
q_time = run_query(q, bao_reward=USE_BAO, bao_select=USE_BAO)
print(c_idx, q_idx, time(), fp, q_time, flush=True)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/johnyuan518/baoforpostgresql.git
git@gitee.com:johnyuan518/baoforpostgresql.git
johnyuan518
baoforpostgresql
baoforpostgresql
master

搜索帮助