-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
66 lines (48 loc) · 2.36 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os, uuid, timeit, functools
from query import get_query
from azure.kusto.data.request import KustoClient, KustoConnectionStringBuilder, ClientRequestProperties
from applicationinsights import TelemetryClient
######################################################
## AUTH ##
######################################################
cluster = os.environ.get("CLUSTER_QUERY_URL")
client_id = os.environ.get("CLIENT_ID")
client_secret = os.environ.get("CLIENT_SECRET")
tenant_id = os.environ.get("TENANT_ID")
kcsb = KustoConnectionStringBuilder.with_aad_application_key_authentication(
cluster, client_id, client_secret, tenant_id)
kusto_client = KustoClient(kcsb)
######################################################
## QUERY ##
######################################################
db_name = os.environ.get("DATABASE_NAME")
test_id = os.environ.get("TEST_ID", str(uuid.uuid4()))
query_consistency = os.environ.get("QUERY_CONSISTENCY", "weakconsistency")
request_properties = ClientRequestProperties()
request_properties.set_option("queryconsistency", query_consistency)
instrumentation_key = os.environ.get("APPINSIGHTS_INSTRUMENTATIONKEY")
telemetry_client = None
if instrumentation_key:
telemetry_client = TelemetryClient(instrumentation_key)
print("Test run for '{}' started.".format(test_id))
def execute_query(raw_query):
query = "{} //TEST_ID={}".format(raw_query, test_id)
response = kusto_client.execute(db_name, query, request_properties)
if response.errors_count > 0:
if telemetry_client:
telemetry_client.track_exception(response)
telemetry_client.flush()
raise Exception(response)
queries_total = int(os.environ.get("QUERIES_TOTAL", -1))
queries_executed = 0
while queries_executed < queries_total or queries_total < 0:
raw_query = get_query()
print("\nTest '{}' executing #{}:\n{}\n".format(test_id, queries_executed, raw_query))
t = timeit.Timer(functools.partial(execute_query, raw_query))
query_time = t.timeit(number=1)
print("Query took: {:.2f} seconds".format(query_time))
queries_executed += 1
if telemetry_client:
telemetry_client.track_metric("query_time", query_time, properties={"test_id": test_id})
telemetry_client.flush()
print("Test run for '{}' ended.".format(test_id))