Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 18 additions & 10 deletions src/ProfileCanvas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,22 @@ function Base.display(_::ProfileDisplay, canvas::ProfileData)
rootpath = artifact"jlprofilecanvas"
path = joinpath(rootpath, "jl-profile.js-0.5.2", "dist", "profile-viewer.js")

file = string(tempname(), ".html")
file = html_file(string(tempname(), ".html"), canvas)
url = "file://$file"

if Sys.iswindows()
run(`cmd /c "start $url"`)
elseif Sys.isapple()
run(`open $url`)
elseif Sys.islinux() || Sys.isbsd()
run(`xdg-open $url`)
end
end

html_file(filename, data=Profile.fetch(); kwargs...) = html_file(filename, view(data; kwargs...))

function html_file(file::AbstractString, canvas::ProfileData)
@assert endswith(file, ".html")
open(file, "w") do io
id = "profiler-container-$(round(Int, rand()*100000))"

Expand Down Expand Up @@ -97,16 +112,9 @@ function Base.display(_::ProfileDisplay, canvas::ProfileData)
"""
)
end
url = "file://$file"

if Sys.iswindows()
run(`cmd /c "start $url"`)
elseif Sys.isapple()
run(`open $url`)
elseif Sys.islinux() || Sys.isbsd()
run(`xdg-open $url`)
end
return file
end

using Profile

# https://github.com/timholy/FlameGraphs.jl/blob/master/src/graph.jl
Expand Down
28 changes: 17 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
using ProfileCanvas
using Test

@testset "ProfileCanvas.jl" begin
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
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

@testset "ProfileCanvas.jl" begin
trace = @profview profile_test(10)
html = sprint(show, "text/html", trace)
@test occursin("const viewer = new ProfileViewer(", html)
end

@testset "html file" begin
@profview profile_test(10)
ProfileCanvas.html_file(joinpath(@__DIR__, "flame.html"))
@test isfile(joinpath(@__DIR__, "flame.html"))
end