Skip to content

Instantly share code, notes, and snippets.

@maleadt
Created July 13, 2018 06:55
Show Gist options
  • Save maleadt/c6802770130038b798161387f22d1d16 to your computer and use it in GitHub Desktop.
Save maleadt/c6802770130038b798161387f22d1d16 to your computer and use it in GitHub Desktop.
Measure Julia init and REPL start-up time
#!/usr/bin/env julia
children(pid::Integer=getpid()) = parse.(Int, split(readline("/proc/$pid/task/$pid/children")))
# TODO: timeout
function measure(cmd; until=nothing)
sleep(1)
mktemp() do path, io
time_cmd = `/usr/bin/time --output=$path --format="timings: %U %S %M" -- $cmd`
# spawn the command
# NOTE: we use STDIN or we wouldn't get a REPL
info("Timing $cmd")
stream, process = open(time_cmd, "r", STDIN)
# if we need to wait for a string, read from the stream and kill the command after
if until != nothing
try
str = readuntil(stream, until)
isempty(str) && error("could not find requested string")
catch e
isa(e, ErrorException) || rethrow(e)
finally
# NOTE: can't get the process' PID, so look at our children
for child in children(), pid in children(child)
# NOTE Base.kill only works on processes
run(`kill -9 $pid`)
end
end
end
# wait for the time process to finish, or we wouldn't have timings
# TODO: we could pipe stream to STDOUT here
wait(process)
if until == nothing
success(process) || error("command failed")
end
for line in readlines(path)
if startswith(line, "timings: ")
timings = split(line)[2:end]
return parse.(Float64, timings)
end
end
end
end
measure_start() = measure(`/usr/bin/julia -e 42`)
measure_repl() = measure(`/usr/bin/julia`; until="julia>")
@show measure_start()
@show measure_repl()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment