in scripts/weather_perf.py [0:0]
def thread_func(endpoint, username, password, schema, max_id, iterations):
# Specify that query_count is a global variable
global query_count
global lock
count = 0
try:
# Resolve the endpoint
host = socket.gethostbyname(endpoint)
# Connect to the reader endpoint
conn = pymysql.connect(host=host, user=username, password=password, database=schema, autocommit=True)
# Run multiple queries per connection
for iter in range(iterations):
# Generate a random number to use as the lookup value
# we will arbitrarily switch between a few query types
temp_value = random.randrange(10, temp_id)
key_value = random.randrange(1, max_id)
key_offset = random.randrange(1, 1000)
query_type = random.randrange(0,9)
station_name: str=random.choice(stations)
stationid: str=random.choice(station_id)
type=random.choice(types)
# queries of multiple types
if query_type == 0:
# Point query
sql_command = "SELECT SQL_NO_CACHE id, SHA2(c, 512) AS token FROM sbtest1 WHERE id= %d;" % (key_value)
elif query_type == 1:
# Range query
sql_command = "SELECT sql_no_cache count(id) FROM weather WHERE station_name = '%s' and type = '%s';" % (station_name,type)
elif query_type == 2:
# stored procedure
sql_command = "CALL insert_temp;"
elif query_type == 3:
# Point query with hashing
sql_command = "CALL minute_rollup(%d);" % (key_offset * 10)
elif query_type == 4:
# Point query with hashing
sql_command = "UPDATE mylab.weather SET max_temp = %d where id='%s';" %(temp_value,stationid)
elif query_type == 5:
# Point query
sql_command = "SELECT SQL_NO_CACHE * FROM sbtest1 WHERE id= %d;" % (key_value)
elif query_type == 6:
# Range query
sql_command = "SELECT SQL_NO_CACHE *, SHA2(c, 512), SQRT(k) FROM sbtest1 WHERE id BETWEEN %d AND %d ORDER BY id DESC LIMIT 10;" % (key_value, key_value + key_offset)
elif query_type == 7:
# Aggregation
sql_command = "SELECT SQL_NO_CACHE k, COUNT(k), SQRT(SUM(k)), SQRT(AVG(k)) FROM sbtest1 WHERE id BETWEEN %d AND %d GROUP BY k ORDER BY k;" % (key_value, key_value + key_offset)
elif query_type == 8:
# Point query with hashing
sql_command = "SELECT sql_no_cache max_temp,min_temp,station_name FROM weather WHERE max_temp > %d and id = '%s' ORDER BY max_temp DESC;" % (temp_value,stationid)
# run query
with conn.cursor() as cursor:
print("Executing %s \t" %sql_command)
cursor.execute(sql_command)
cursor.close()
# run query
# Increment the executed query count
with lock:
query_count += 1
# Close the connection
conn.close()
except:
# Display any exception information
print(sys.exc_info()[1])