代码拉取完成,页面将自动刷新
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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。