Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbt builtin profiler #1020

Merged
merged 6 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions dbt/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import dbt.ui.printer
import dbt.compat
import dbt.deprecations
import dbt.profiler

from dbt.utils import ExitCodes
from dbt.config import Project, RuntimeConfig, DbtProjectError, \
Expand Down Expand Up @@ -108,25 +109,34 @@ def handle(args):
def handle_and_check(args):
parsed = parse_args(args)

# this needs to happen after args are parsed so we can determine the
# correct profiles.yml file
profile_config = read_config(parsed.profiles_dir)
if not send_anonymous_usage_stats(profile_config):
dbt.tracking.do_not_track()
else:
dbt.tracking.initialize_tracking()
profiler_enabled = False

if colorize_output(profile_config):
dbt.ui.printer.use_colors()
if parsed.record_timing_info:
profiler_enabled = True

try:
task, res = run_from_args(parsed)
finally:
dbt.tracking.flush()
with dbt.profiler.profiler(
enable=profiler_enabled,
outfile=parsed.record_timing_info
):
# this needs to happen after args are parsed so we can determine the
# correct profiles.yml file
profile_config = read_config(parsed.profiles_dir)
if not send_anonymous_usage_stats(profile_config):
dbt.tracking.do_not_track()
else:
dbt.tracking.initialize_tracking()

success = task.interpret_results(res)
if colorize_output(profile_config):
dbt.ui.printer.use_colors()

return res, success
try:
task, res = run_from_args(parsed)
finally:
dbt.tracking.flush()

success = task.interpret_results(res)

return res, success


def get_nearest_project_dir():
Expand Down Expand Up @@ -275,6 +285,18 @@ def parse_args(args):
action='dbtversion',
help="Show version information")

p.add_argument(
'-r',
'--record-timing-info',
default=None,
type=str,
help="""
When this option is passed, dbt will output low-level timing
stats to the specified file. Example:
`--record-timing-info output.profile`
"""
)

p.add_argument(
'-d',
'--debug',
Expand Down
19 changes: 19 additions & 0 deletions dbt/profiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from contextlib import contextmanager
from cProfile import Profile
from pstats import Stats


@contextmanager
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work on py2 and py3? And does it have any impact on error handling?

def profiler(enable, outfile):
try:
if enable:
profiler = Profile()
profiler.enable()

yield
finally:
if enable:
profiler.disable()
stats = Stats(profiler)
stats.sort_stats('tottime')
stats.dump_stats(outfile)