forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofile_viewer.py
316 lines (271 loc) · 9.83 KB
/
profile_viewer.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import subprocess
import json
import requests
import os
import re
import argparse
FE_HOST = "http://127.0.0.1:8030"
MYSQL = "mysql -h:: -P9030 -u root "
DB = "tpcds_sf100"
WORK_DIR="./tmp/"
def execute_command(cmd: str):
result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
return result.stdout
def sql(query):
print("exec sql: " + query)
cmd = MYSQL + " -D " + DB + " -e \"" + query + "\""
result = execute_command(cmd)
return result
def last_query_id():
# 'YWRtaW46' is the base64 encoded result for 'admin:'
headers = {'Authorization': 'BASIC YWRtaW46'}
query_info_url = FE_HOST + '/rest/v2/manager/query/query_info'
resp_wrapper = requests.get(query_info_url, headers=headers)
resp_text = resp_wrapper.text
qinfo = json.loads(resp_text)
assert qinfo['msg'] == 'success'
assert len(qinfo['data']['rows']) > 0
return qinfo['data']['rows'][0][0]
def get_profile(query_id):
profile_url = FE_HOST + "/rest/v2/manager/query/profile/json/" + query_id
headers = {'Authorization': 'BASIC YWRtaW46'}
resp_wrapper = requests.get(profile_url, headers=headers)
resp_text = resp_wrapper.text
prof = json.loads(resp_text)
assert prof['msg'] == 'success', "query failed"
data = prof['data']['profile'].replace("\\n", "")
return data.replace("\\u003d", "=")
def get_fragments(obj):
return obj['children'][2]['children'][0]['children']
def get_children(obj):
return obj['children']
class Fragment:
def __init__(self, obj):
self.name = obj['name']
self.instances = []
for child in get_children(obj):
self.instances.append(Instance(child))
# the merged instance. merge corresponding Node.rows
self.merged = Instance(get_children(obj)[0])
self.merged.clear()
self.merge_instances()
m=re.search("Instance ([\w|\d]+-[\w|\d]+) ", self.merged.name)
assert m != None, "cannot find instance id: " + self.merged.name
self.first_inst_id = m.group(1)
def merge_instances(self):
for inst in self.instances:
self.merged.merge(inst)
def register(self, node_map):
self.merged.register(node_map)
class Instance:
def __init__(self, obj):
self.name = obj['name']
assert(len(get_children(obj)) > 1)
self.sender = Node(get_children(obj)[0])
self.dst_id = self.sender.get_dst_id()
self.root = Node(get_children(obj)[1])
if (len(get_children(obj)) > 2):
self.pipe = Node(get_children(obj)[2])
def clear(self):
self.root.clear()
def merge(self, other):
self.root.merge(other.root)
def register(self, node_map):
self.root.register(node_map)
class Node:
def __init__(self, obj):
self.obj = obj
self.name = self.get_name(obj)
if self.name == "RuntimeFilter":
self.rf_id = self.get_rf_id(obj)
self.id = -1
self.caption = self.name + "_" + str(self.rf_id)
else:
self.id = self.get_id(obj)
self.caption = self.name + "_" + str(self.id)
self.rows = obj['rowsReturned']
self.total_time = obj['totalTime']
self.children = []
for child in obj['children']:
self.children.append(Node(child))
def get_name(self, obj):
name = obj['name']
m=re.search("(\S+)", obj['name'])
if m != None:
name = m.group(1)
return name.replace("(", '_').replace(")", '').replace(':', '')
def get_id(self, obj):
m=re.search("id=([\d]+)", self.obj['name'])
if m != None:
return int(m.group(1))
else:
return -1
def get_rf_id(self, obj):
m=re.search("id = ([\d]+)", self.obj['name'])
if m != None:
return int(m.group(1))
else:
return -1
def get_dst_id(self):
m=re.search("dst_id=([\d]+)", self.obj['name'])
if m != None:
return int(m.group(1))
else:
return -1
def clear(self):
self.rows = 0
self.total_time = ""
for child in self.children:
child.clear()
def register(self, node_map):
node_map[self.id] = self
for child in self.children:
child.register(node_map)
def merge(self, other):
assert(self.name == other.name)
self.rows += other.rows
self.total_time += "/" + other.total_time
childCount =len(self.children)
if self.name.find("ScanNode") > 0:
return
assert(childCount == len(other.children))
for i in range(childCount):
self.children[i].merge(other.children[i])
def tree(self, prefix):
print(prefix + self.name + " id=" + str(self.id) + " rows=" + str(self.rows))
for child in self.children:
child.tree(prefix + "--")
def to_dot(self):
content = ""
list = self.children
if list != None:
list.reverse()
for child in list:
if child.id != -1:
content += "{}->{}[label={}]\n".format(child.caption, self.caption, child.rows)
for child in list:
if child.id != -1:
content += child.to_dot()
return content
def keep_alpha_numeric(input_str):
# 使用正则表达式只保留大写字母、小写字母和数字
pattern = r'[^A-Za-z0-9]'
result = re.sub(pattern, '', input_str)
return result
def to_dot_file(root, dot_file, title):
title = keep_alpha_numeric(title)
header = """
digraph BPMN {
rankdir=BT;
node[style="rounded,filled" color="lightgoldenrodyellow" ]
"""
label = "label = \"" + title + "\"\n"
tail = "}"
with open(dot_file, 'w') as f:
f.write(header)
f.write(label)
f.write(root.to_dot())
f.write(tail)
def draw_proile(query_id, title=""):
print("query_id: " + query_id)
prof = get_profile(query_id)
if title == "":
title = query_id
prf_file = WORK_DIR + title + ".profile"
png_file = WORK_DIR + title + ".png"
dot_file = WORK_DIR + title + ".dot"
with open(prf_file, "w") as f:
f.write(prof)
obj = json.loads(prof.replace("\n", ""))
fragments = []
node_map={}
for json_fr in get_fragments(obj):
fr = Fragment(json_fr)
fr.register(node_map)
fragments.append(fr)
root_fr = fragments[0]
for fr in fragments[1:]:
exchange = node_map[fr.merged.dst_id]
exchange.children.append(fr.merged.root)
root_fr.merged.root.tree("")
to_dot_file(root_fr.merged.root, dot_file, title)
cmd = "dot {} -T png -o {}".format(dot_file, png_file)
print(cmd)
status = os.system(cmd)
if status != 0:
print("convert to png failed")
else:
print("generate " + png_file)
def exec_sql_and_draw(sql_file):
if not os.path.isdir(WORK_DIR):
os.mkdir(WORK_DIR)
with open(sql_file, 'r') as f:
#trim sql comments
query = ""
for line in f.readlines():
pos = line.find("--")
if pos == -1:
query += line
else:
query += line[:pos]
sql("set global enable_nereids_planner=true")
sql("set global enable_pipeline_engine=true")
sql("set global enable_profile=true")
sql("set global runtime_filter_type=2")
sql("set global enable_runtime_filter_prune=false")
sql("set global runtime_filter_wait_time_ms=9000000")
sql(query)
query_id = last_query_id()
return query_id
def print_usage():
print("""
USAGE:
1. execute a sql file, and draw its profile
python3 profile_viewer.py -f [path to sql file]
2. draw a given profile
python3 profile_viewer.py -qid [query_id]
graphviz is required(https://graphviz.org/)
on linux: apt install graphviz
on mac: brew install graphviz
""")
if __name__ == '__main__':
USAGE ="""
1. execute a sql file, and draw its profile
python3 profile_viewer.py -f [path to sql file] -t [png title]
2. draw a given profile
python3 profile_viewer.py -qid [query_id] -t [png title]
graphviz is required(https://graphviz.org/)
on linux: apt install graphviz
on mac: brew install graphviz
"""
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", help="sql file", dest="sql_file", type=str, default ="")
parser.add_argument("-qid", "--query_id", help="query id", dest="query_id", type=str, default="")
parser.add_argument("-t", "--title", help="query title", dest="title", type=str, default="")
args = parser.parse_args()
if args.sql_file == "" and args.query_id == "":
print(USAGE)
exit(0)
title = args.title
if args.query_id == "":
query_id = exec_sql_and_draw(args.sql_file)
draw_proile(query_id, title)
else:
draw_proile(args.query_id, title)