| 390 | |
| 391 | |
| 392 | def _apply_events(engine): |
| 393 | queries = defaultdict(list) |
| 394 | |
| 395 | now = 0 |
| 396 | |
| 397 | @sa.event.listens_for(engine, "before_cursor_execute") |
| 398 | def before_cursor_execute( |
| 399 | conn, cursor, statement, parameters, context, executemany |
| 400 | ): |
| 401 | nonlocal now |
| 402 | now = time.time() |
| 403 | |
| 404 | @sa.event.listens_for(engine, "after_cursor_execute") |
| 405 | def after_cursor_execute( |
| 406 | conn, cursor, statement, parameters, context, executemany |
| 407 | ): |
| 408 | total = time.time() - now |
| 409 | |
| 410 | if context and context.compiled: |
| 411 | statement_str = context.compiled.string |
| 412 | else: |
| 413 | statement_str = statement |
| 414 | queries[statement_str].append(total) |
| 415 | |
| 416 | return queries |
| 417 | |
| 418 | |
| 419 | def _print_query_stats(queries): |