ProfileSVG

ProfileSVG allows you to export profiling data as an SVG file. It can be used to display profiling results in Jupyter/IJulia notebooks, Pluto or any other SVG viewer.

Installation

The package can be installed with the Julia package manager. Run:

import Pkg
Pkg.add("ProfileSVG")

or, from the Julia REPL, type ] to enter the Pkg REPL mode and run:

pkg> add ProfileSVG

Usage

Displaying profiles

using ProfileSVG
@profview f(args...)

where f(args...) is the operation you want to profile.

For example:

function profile_test(n)
    for i = 1:n
        A = randn(100,100,20)
        m = maximum(A)
        Am = mapslices(sum, A; dims=2)
        B = A[:,:,5]
        Bsort = mapslices(sort, B; dims=1)
        b = rand(100)
        C = B.*b
    end
end

profile_test(1)   # run once to compile

using Profile, ProfileSVG

@profview profile_test(10)

Then, you can get something like this:

Profile results in :-1 eval in boot.jl:331 profile_test in REPL[2]:3 randn in normal.jl:190 randn in normal.jl:184 randn! in normal.jl:173 setindex! in array.jl:826 randn in normal.jl:167 randn in normal.jl:40 rand in Random.jl:253 rand in generation.jl:119 rand in Random.jl:253 rand in RNGs.jl:370 reserve_1 in RNGs.jl:190 gen_rand in RNGs.jl:186 dsfmt_fill_array_close1_open2! in DSFMT.jl:86 mt_empty in RNGs.jl:180 getproperty in Base.jl:33 rand in RNGs.jl:371 rand_inbounds in RNGs.jl:366 rand_inbounds in RNGs.jl:362 mt_pop! in RNGs.jl:183 + in int.jl:53 randn in normal.jl:43 * in promotion.jl:312 * in float.jl:405 randn in normal.jl:45 randn_unlikely in normal.jl:58 exp in exp.jl:136 - in float.jl:403 profile_test in REPL[2]:5 mapslices##kw in abstractarray.jl:1972 #mapslices#115 in abstractarray.jl:2029 inner_mapslices! in abstractarray.jl:2039 concatenate_setindex! in abstractarray.jl:2058 dotview in broadcast.jl:1138 maybeview in views.jl:124 profile_test in REPL[2]:7 mapslices##kw in abstractarray.jl:1972 #mapslices#115 in abstractarray.jl:2018 similar in abstractarray.jl:626 similar in array.jl:361 Array in boot.jl:415 Array in boot.jl:407

Note that collected profiles can vary from run-to-run, so don't be alarmed if you get something different.

@profview f(args...) is just shorthand for

Profile.clear()
@profile f(args...)
ProfileSVG.view()

If you've already collected profiling data with @profile, or if you want to customize the output, you can call ProfileSVG.view directly.

Using ProfileSVG within VSCode

VS Code with Julia Extension has a profile viewing feature. On the other hand, you can also display the SVG output of ProfileSVG in the Plot Pane in VS Code. Since @profview has a name collision with the Julia extension for VS Code, you need to explicitly specify ProfileSVG.@profview or use ProfileSVG.view.

Exporting to SVG file

Even if you don't use graphical front-ends such as Jupyter, you might want to export a flame graph as an SVG file as a convenient way to share the results with others. The ProfileSVG.save function provides the exporting feature.

Profile.clear()
@profile profile_test(10);

# Save a graph that looks like the Jupyter example above
ProfileSVG.save(joinpath("assets", "prof.svg"))

Exported Profile Note that the exported SVG files include the script for interactive features, but some viewers (browsers) do not support the script. In particular, when loading the SVG image from an HTML <img> element (as above), the interactive features are usually disabled.

Other tools for displaying profiles