Skip to content

Commit

Permalink
Produce a nodejs profile together with a perf_trace. (bazel-contrib#149)
Browse files Browse the repository at this point in the history
This allows source level profiling of the JavaScript code, in addition to the more coarsely grained application level profile.
  • Loading branch information
mprobst authored and alexeagle committed Feb 16, 2018
1 parent 990321b commit 6d4a205
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ load(":executables.bzl", "get_tsc")
load(":common/tsconfig.bzl", "create_tsconfig")
load(":ts_config.bzl", "TsConfigInfo")

def _compile_action(ctx, inputs, outputs, tsconfig_file):
def _compile_action(ctx, inputs, outputs, tsconfig_file, node_opts):
externs_files = []
action_outputs = []
for output in outputs:
Expand All @@ -48,14 +48,17 @@ def _compile_action(ctx, inputs, outputs, tsconfig_file):
if TsConfigInfo in ctx.attr.tsconfig:
action_inputs += ctx.attr.tsconfig[TsConfigInfo].deps

# Pass actual options for the node binary in the special "--node_options" argument.
arguments = ["--node_options=%s" % opt for opt in node_opts]
# One at-sign makes this a params-file, enabling the worker strategy.
# Two at-signs escapes the argument so it's passed through to tsc_wrapped
# rather than the contents getting expanded.
if ctx.attr.supports_workers:
arguments = ["@@" + tsconfig_file.path]
arguments.append("@@" + tsconfig_file.path)
mnemonic = "TypeScriptCompile"
else:
arguments = ["-p", tsconfig_file.path]
arguments.append("-p")
arguments.append(tsconfig_file.path)
mnemonic = "tsc"

ctx.action(
Expand All @@ -80,8 +83,8 @@ def _compile_action(ctx, inputs, outputs, tsconfig_file):
)


def _devmode_compile_action(ctx, inputs, outputs, tsconfig_file):
_compile_action(ctx, inputs, outputs, tsconfig_file)
def _devmode_compile_action(ctx, inputs, outputs, tsconfig_file, node_opts):
_compile_action(ctx, inputs, outputs, tsconfig_file, node_opts)

def tsc_wrapped_tsconfig(ctx,
files,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,24 @@ def compile_ts(ctx,
tsconfig_es6["compilerOptions"]["declaration"] = False
tsconfig_es6["compilerOptions"].pop("declarationDir")
outputs = transpiled_closure_js + tsickle_externs

node_profile_args = []
if perf_trace and has_sources:
perf_trace_file = ctx.new_file(ctx.label.name + ".es6.trace")
tsconfig_es6["bazelOptions"]["perfTracePath"] = perf_trace_file.path
outputs.append(perf_trace_file)
files += [perf_trace_file]

profile_file = ctx.new_file(ctx.label.name + ".es6.v8.log")
node_profile_args = ["--prof",
# Without nologfile_per_isolate, v8 embeds an
# unpredictable hash code in the file name, which
# doesn't work with blaze.
"--nologfile_per_isolate",
"--logfile=" + profile_file.path]
outputs.append(profile_file)

files += [perf_trace_file, profile_file]

ctx.file_action(output=ctx.outputs.tsconfig,
content=json_marshal(tsconfig_es6))

Expand All @@ -244,7 +257,8 @@ def compile_ts(ctx,

if has_sources:
inputs = compilation_inputs + [ctx.outputs.tsconfig]
replay_params = compile_action(ctx, inputs, outputs, ctx.outputs.tsconfig)
replay_params = compile_action(ctx, inputs, outputs, ctx.outputs.tsconfig,
node_profile_args)

devmode_manifest = ctx.new_file(ctx.label.name + ".es5.MF")
tsconfig_json_es5 = ctx.new_file(ctx.label.name + "_es5_tsconfig.json")
Expand All @@ -256,15 +270,28 @@ def compile_ts(ctx,
jsx_factory=jsx_factory,
devmode_manifest=devmode_manifest.path,
allowed_deps=allowed_deps)
node_profile_args = []
if perf_trace:
perf_trace_file = ctx.new_file(ctx.label.name + ".es5.trace")
tsconfig_es5["bazelOptions"]["perfTracePath"] = perf_trace_file.path
outputs.append(perf_trace_file)
files += [perf_trace_file]

profile_file = ctx.new_file(ctx.label.name + ".es5.v8.log")
node_profile_args = ["--prof",
# Without nologfile_per_isolate, v8 embeds an
# unpredictable hash code in the file name, which
# doesn't work with blaze.
"--nologfile_per_isolate",
"--logfile=" + profile_file.path]
outputs.append(profile_file)

files += [perf_trace_file, profile_file]

ctx.file_action(output=tsconfig_json_es5, content=json_marshal(
tsconfig_es5))
inputs = compilation_inputs + [tsconfig_json_es5]
devmode_compile_action(ctx, inputs, outputs, tsconfig_json_es5)
devmode_compile_action(ctx, inputs, outputs, tsconfig_json_es5,
node_profile_args)

# TODO(martinprobst): Merge the generated .d.ts files, and enforce strict
# deps (do not re-export transitive types from the transitive closure).
Expand Down

0 comments on commit 6d4a205

Please sign in to comment.