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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
#!/usr/bin/env python3
#
# See the accompanying LICENSE file.
#
# This module lets you automatically trace SQL operations in a program
# using APSW without having to modify the program in any way.
import time
import sys
import weakref
class APSWTracer(object):
def __init__(self, options):
self.u = ""
import _thread
self.threadid = _thread.get_ident
self.stringtypes = (str, )
self.numtypes = (int, float)
self.binarytypes = (bytes, )
self.options = options
if options.output in ("-", "stdout"):
self._writer = sys.stdout.write
elif options.output == "stderr":
self._writer = sys.stderr.write
else:
self._writer = open(options.output, "wt").write
try:
import apsw
apsw.connection_hooks.append(self.connection_hook)
except:
sys.stderr.write(self.u + "Unable to import apsw\n")
raise
self.mapping_open_flags = apsw.mapping_open_flags
self.zeroblob = apsw.zeroblob
self.apswConnection = apsw.Connection
self.newcursor = {}
self.threadsused = {} # really want a set
self.queries = {}
self.timings = {}
self.rowsreturned = 0
self.numcursors = 0
self.numconnections = 0
self.timestart = time.time()
def writerpy3(self, s):
self._writer(s + "\n")
writer = writerpy3
def format(self, obj):
if isinstance(obj, dict):
return self.formatdict(obj)
if isinstance(obj, tuple):
return self.formatseq(obj, '()')
if isinstance(obj, list):
return self.formatseq(obj, '[]')
if isinstance(obj, self.stringtypes):
return self.formatstring(obj)
if obj is True:
return "True"
if obj is False:
return "False"
if obj is None:
return "None"
if isinstance(obj, self.numtypes):
return repr(obj)
if isinstance(obj, self.binarytypes):
return self.formatbinary(obj)
if isinstance(obj, self.zeroblob):
return "zeroblob(%d)" % (obj.length(), )
return repr(obj)
def formatstring(self, obj, quote='"', checkmaxlen=True):
obj = obj.replace("\n", "\\n").replace("\r", "\\r")
if checkmaxlen and len(obj) > self.options.length:
obj = obj[:self.options.length] + '..'
return self.u + quote + obj + quote
def formatdict(self, obj):
items = list(obj.items())
items.sort()
op = []
for k, v in items:
op.append(self.format(k) + ": " + self.format(v))
return self.u + "{" + ", ".join(op) + "}"
def formatseq(self, obj, paren):
return self.u + paren[0] + ", ".join([self.format(v) for v in obj]) + paren[1]
def formatbinary(self, obj):
if len(obj) < self.options.length:
return "X'" + "".join(["%x" % obj[i] for i in range(len(obj))]) + "'"
return "(%d) X'" % (len(obj), ) + "".join(["%x" % obj[i] for i in range(self.options.length)]) + "..'"
def sanitizesql(self, sql):
sql = sql.strip("; \t\r\n")
while sql.startswith("--"):
sql = sql.split("\n", 1)[1]
sql = sql.lstrip("; \t\r\n")
return sql
def profiler(self, sql, nanoseconds):
sql = self.sanitizesql(sql)
if sql not in self.timings:
self.timings[sql] = [nanoseconds]
else:
self.timings[sql].append(nanoseconds)
def cursorfinished(self, cursor):
del self.newcursor[cursor]
def exectracer(self, cursor, sql, bindings):
tid = self.threadid()
if tid not in self.threadsused:
self.threadsused[tid] = True
if self.options.report:
fix = self.sanitizesql(sql)
if fix not in self.queries:
self.queries[fix] = 1
else:
self.queries[fix] = self.queries[fix] + 1
if not isinstance(cursor, self.apswConnection):
wr = weakref.ref(cursor, self.cursorfinished)
if wr not in self.newcursor:
self.newcursor[wr] = True
self.numcursors += 1
if self.options.sql:
self.log(id(cursor), "CURSORFROM:", "%x" % (id(cursor.connection), ), "DB:",
self.formatstring(cursor.connection.filename, checkmaxlen=False))
if self.options.sql:
args = [id(cursor), "SQL:", self.formatstring(sql, '', False)]
if bindings:
args.extend(["BINDINGS:", self.format(bindings)])
self.log(*args)
return True
def rowtracer(self, cursor, row):
if self.options.report:
self.rowsreturned += 1
if self.options.rows:
self.log(id(cursor), "ROW:", self.format(row))
return row
def flagme(self, value, mapping, strip=""):
v = [(k, v) for k, v in mapping.items() if isinstance(k, int)]
v.sort()
op = []
for k, v in v:
if value & k:
if v.startswith(strip):
v = v[len(strip):]
op.append(v)
return self.u + "|".join(op)
def connection_hook(self, con):
self.numconnections += 1
if self.options.report:
con.set_profile(self.profiler)
if self.options.sql or self.options.report:
con.exec_trace = self.exectracer
if self.options.rows or self.options.report:
con.row_trace = self.rowtracer
if self.options.sql:
self.log(id(con), "OPEN:", self.formatstring(con.filename, checkmaxlen=False), con.open_vfs,
self.flagme(con.open_flags, self.mapping_open_flags, "SQLITE_OPEN_"))
def log(self, lid, ltype, *args):
out = ["%x" % (lid, )]
if self.options.timestamps:
out.append("%.03f" % (time.time() - self.timestart, ))
if self.options.thread:
out.append("%x" % (self.threadid(), ))
out.append(ltype)
out.extend(args)
self.writer(self.u + " ".join(out))
def run(self):
import sys
import __main__
d = vars(__main__)
# We use compile so that filename is present in printed exceptions
code = compile(open(sys.argv[0], "rb").read(), sys.argv[0], "exec")
exec(code, d, d)
def mostpopular(self, howmany):
all = [(v, k) for k, v in self.queries.items()]
all.sort()
all.reverse()
return all[:howmany]
def longestrunningaggregate(self, howmany):
all = [(sum(v), len(v), k) for k, v in self.timings.items()]
all.sort()
all.reverse()
return all[:howmany]
def longestrunningindividual(self, howmany):
res = []
for k, v in self.timings.items():
for t in v:
res.append((t, k))
res.sort()
res.reverse()
res = res[:howmany]
return res
def report(self):
import time
if not self.options.report:
return
w = lambda *args: self.writer(self.u + " ".join(args))
if "summary" in self.options.reports:
w("APSW TRACE SUMMARY REPORT")
w()
w("Program run time ", "%.03f seconds" % (time.time() - self.timestart, ))
w("Total connections ", str(self.numconnections))
w("Total cursors ", str(self.numcursors))
w("Number of threads used for queries ", str(len(self.threadsused)))
total = 0
for k, v in self.queries.items():
total += v
fmtq = len("%d" % (total, )) + 1
if "summary" in self.options.reports:
w("Total queries ", str(total))
w("Number of distinct queries ", str(len(self.queries)))
w("Number of rows returned ", str(self.rowsreturned))
total = 0
for k, v in self.timings.items():
for v2 in v:
total += v2
w("Time spent processing queries ", "%.03f seconds" % (total / 1000000000.0))
# show most popular queries
if "popular" in self.options.reports:
w()
w("MOST POPULAR QUERIES")
w()
for count, query in self.mostpopular(self.options.reportn):
w("% *d" % (
fmtq,
count,
), self.formatstring(query, '', False))
# show longest running (aggregate)
if "aggregate" in self.options.reports:
w()
w("LONGEST RUNNING - AGGREGATE")
w()
fmtt = None
for total, count, query in self.longestrunningaggregate(self.options.reportn):
if fmtt is None:
fmtt = len(fmtfloat(total / 1000000000.0)) + 1
w("% *d %s" % (fmtq, count, fmtfloat(total / 1000000000.0, total=fmtt)),
self.formatstring(query, '', False))
# show longest running (individual)
if "individual" in self.options.reports:
w()
w("LONGEST RUNNING - INDIVIDUAL")
w()
fmtt = None
for t, query in self.longestrunningindividual(self.options.reportn):
if fmtt is None:
fmtt = len(fmtfloat(total / 1000000000.0)) + 1
w(fmtfloat(t / 1000000000.0, total=fmtt), self.formatstring(query, '', False))
def fmtfloat(n, decimals=3, total=None):
"Work around borken python float formatting"
s = "%0.*f" % (decimals, n)
if total:
s = (" " * total + s)[-total:]
return s
def main():
import argparse
import os
import sys
reports = ("summary", "popular", "aggregate", "individual")
parser = argparse.ArgumentParser(prog="python3 -m apsw.trace",
description="This script runs a Python program that uses APSW "
"and reports on SQL queries without modifying the program. This is "
"done by using connection_hooks and registering row and execution "
"tracers. See APSW documentation for more details on the output.")
parser.add_argument(
"-o",
"--output",
dest="output",
default="stdout",
help=
"Where to send the output. Use a filename, a single dash for stdout, or the words stdout and stderr. [%(default)s]"
)
parser.add_argument("-s",
"--sql",
dest="sql",
default=False,
action="store_true",
help="Log SQL statements as they are executed. [%(default)s]")
parser.add_argument("-r",
"--rows",
dest="rows",
default=False,
action="store_true",
help="Log returned rows as they are returned (turns on sql). [%(default)s]")
parser.add_argument("-t",
"--timestamps",
dest="timestamps",
default=False,
action="store_true",
help="Include timestamps in logging")
parser.add_argument("-i",
"--thread",
dest="thread",
default=False,
action="store_true",
help="Include thread id in logging")
parser.add_argument("-l",
"--length",
dest="length",
default=30,
type=int,
help="Max amount of a string to print [%(default)s]")
parser.add_argument(
"--no-report",
dest="report",
default=True,
action="store_false",
help="A summary report is normally generated at program exit. This turns off the report and saves memory.")
parser.add_argument("--report-items",
dest="reportn",
metavar="N",
default=15,
type=int,
help="How many items to report in top lists [%(default)s]")
parser.add_argument("--reports",
dest="reports",
default=",".join(reports),
help="Which reports to show [%(default)s]")
parser.add_argument("python-script", help="Python script to run")
parser.add_argument("script-args", nargs="*", help="Optional arguments for Python script")
options = parser.parse_args()
# it doesn't make the dashes underscore for some reason
for n in ("python-script", "script-args"):
setattr(options, n.replace("-", "_"), getattr(options, n))
options.reports = [x.strip() for x in options.reports.split(",") if x.strip()]
for r in options.reports:
if r not in reports:
parser.error(r + " is not a valid report. You should supply one or more of " + ", ".join(reports))
if options.rows:
options.sql = True
if not os.path.exists(options.python_script):
parser.error(f"Unable to find script { options.python_script }")
sys.argv = [options.python_script] + options.script_args
sys.path[0] = os.path.split(os.path.abspath(sys.argv[0]))[0]
t = APSWTracer(options)
try:
t.run()
finally:
t.report()
if __name__ == "__main__":
main()
|