Skip to content

Commit 102a6f0

Browse files
authored
Add @bprofile (#254)
To collect enough data, profiling benefits from long(ish) execution times. That makes `@benchmark`/`@btime` attractive workloads for profiling. Unfortunately, there's a lot of "junk" that appears in the profile records, including many for type-inference. This adds `@bprofile`, a substitute for `@profile @b...`, that restricts profiling to the `run` phase of benchmarking. Other than a fairly small amount of inference on the first session use of `@bprofile` itself, this eliminates all the inference and tuning in the trace.
1 parent d34a767 commit 102a6f0

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
name = "BenchmarkTools"
22
uuid = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
3-
version = "1.1.4"
3+
version = "1.2.0"
44

55
[deps]
66
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
77
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
88
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
9+
Profile = "9abbd945-dff8-562f-b5e8-e1ebf5ef1b79"
910
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1011
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
1112

1213
[compat]
13-
julia = "1"
1414
JSON = "0.18, 0.19, 0.20, 0.21"
15+
julia = "1"
1516

1617
[extras]
1718
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

src/BenchmarkTools.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using Logging: @logmsg, LogLevel
77
using Statistics
88
using UUIDs: uuid4
99
using Printf
10+
using Profile
1011

1112

1213
const BENCHMARKTOOLS_VERSION = v"1.0.0"
@@ -68,7 +69,8 @@ export tune!,
6869
@benchmark,
6970
@benchmarkable,
7071
@belapsed,
71-
@btime
72+
@btime,
73+
@bprofile
7274

7375
#################
7476
# Serialization #

src/execution.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,30 @@ macro btime(args...)
575575
$result
576576
end)
577577
end
578+
579+
"""
580+
@bprofile expression [other parameters...]
581+
582+
Run `@benchmark` while profiling. This is similar to
583+
584+
@profile @benchmark expression [other parameters...]
585+
586+
but the profiling is applied only to the main
587+
execution (after compilation and tuning).
588+
The profile buffer is cleared prior to execution.
589+
590+
View the profile results with `Profile.print(...)`.
591+
See the profiling section of the Julia manual for more
592+
information.
593+
"""
594+
macro bprofile(args...)
595+
_, params = prunekwargs(args...)
596+
tmp = gensym()
597+
return esc(quote
598+
local $tmp = $BenchmarkTools.@benchmarkable $(args...)
599+
$BenchmarkTools.warmup($tmp)
600+
$(hasevals(params) ? :() : :($BenchmarkTools.tune!($tmp)))
601+
$BenchmarkTools.Profile.clear()
602+
$BenchmarkTools.@profile $BenchmarkTools.run($tmp)
603+
end)
604+
end

test/ExecutionTests.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module ExecutionTests
22

33
using BenchmarkTools
4+
using Profile
45
using Test
56

67
seteq(a, b) = length(a) == length(b) == length(intersect(a, b))
@@ -134,6 +135,36 @@ tune!(b)
134135
# test kwargs separated by `,`
135136
@benchmark(output=sin(x), setup=(x=1.0; output=0.0), teardown=(@test output == sin(x)))
136137

138+
#############
139+
# @bprofile #
140+
#############
141+
142+
function likegcd(a::T, b::T) where T<:Base.BitInteger
143+
za = trailing_zeros(a)
144+
zb = trailing_zeros(b)
145+
k = min(za, zb)
146+
u = unsigned(abs(a >> za))
147+
v = unsigned(abs(b >> zb))
148+
while u != v
149+
if u > v
150+
u, v = v, u
151+
end
152+
v -= u
153+
v >>= trailing_zeros(v)
154+
end
155+
r = u << k
156+
return r % T
157+
end
158+
159+
b = @bprofile likegcd(x, y) setup=(x = rand(2:200); y = rand(2:200))
160+
@test isa(b, BenchmarkTools.Trial)
161+
io = IOBuffer()
162+
Profile.print(IOContext(io, :displaysize=>(24,200)))
163+
str = String(take!(io))
164+
@test occursin(r"BenchmarkTools(\.jl)?/src/execution\.jl:\d+; _run", str)
165+
@test !occursin(r"BenchmarkTools(\.jl)?/src/execution\.jl:\d+; warmup", str)
166+
@test !occursin(r"BenchmarkTools(\.jl)?/src/execution\.jl:\d+; tune!", str)
167+
137168
########
138169
# misc #
139170
########

0 commit comments

Comments
 (0)