Record per-step timings of offline/terrain builds
CI / check (push) Successful in 38s
CI / deploy (push) Successful in 12s

Live in build-status while running; every finished build (done or
error) is appended to data/build_history.jsonl with its step timings,
regions, thread count and, for offline builds, the tail of Planetiler's
output (its own per-phase summary). GET /api/build-history, shown in the
debug panel, gives a baseline to compare later builds against.
This commit is contained in:
randy committed 2026-11-12 11:54:49 +01:00
1 parent 7429e4ae33
commit 81e688be9b
4 files changed
+110 -9

No files matched your search

+88 -9
View File
@@ -105,13 +105,58 @@ module Maptool
TERRAIN_MUTEX = Mutex.new
def self.offline_status
OFFLINE_MUTEX.synchronize { OFFLINE_STATUS.dup }
OFFLINE_MUTEX.synchronize { with_live_timings(OFFLINE_STATUS) }
end
def self.terrain_status
TERRAIN_MUTEX.synchronize { TERRAIN_STATUS.dup }
TERRAIN_MUTEX.synchronize { with_live_timings(TERRAIN_STATUS) }
end
# While running, status[:steps] holds raw [label, monotonic start]
# pairs — rendered here into per-step seconds, the running one counting
# up, so the debug panel shows where a build is spending its time.
def self.with_live_timings(status)
copy = status.dup
steps = copy.delete(:steps)
copy[:timings] = step_durations(steps, monotonic_now) if steps
copy
end
private_class_method :with_live_timings
# --- build history ("benchmark") ---
# One JSON line per finished build (offline or terrain, done or error)
# with per-step timings — a record to compare later builds against,
# e.g. after tuning Planetiler's JVM heap. Kept across redeploys since
# it lives in data/.
def self.build_history_path(data_dir)
File.join(data_dir, "build_history.jsonl")
end
# Newest first.
def self.build_history(data_dir, limit: 50)
path = build_history_path(data_dir)
return [] unless File.exist?(path)
File.readlines(path).last(limit).reverse.filter_map do |line|
JSON.parse(line)
rescue JSON::ParserError
nil
end
end
def self.monotonic_now
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
private_class_method :monotonic_now
def self.step_durations(steps, finished)
steps.each_with_index.map do |(label, started), i|
{ step: label, seconds: ((steps[i + 1]&.last || finished) - started).round(1) }
end
end
private_class_method :step_durations
# Test-only: the status hashes are process-wide state (same caveat as
# GPX_STORE/LIVETRACK_STORE in app.rb), so the suite needs a way to
# reset them between tests the way AppTestHelper#teardown already does
@@ -196,7 +241,7 @@ module Maptool
begin_run!(OFFLINE_MUTEX, OFFLINE_STATUS)
Thread.new do
run_job(OFFLINE_MUTEX, OFFLINE_STATUS) do |report_step|
run_job(OFFLINE_MUTEX, OFFLINE_STATUS, kind: "offline", data_dir: data_dir, regions: codes) do |report_step|
extract_offline(codes, data_dir, download_extract, fetch_last_modified, report_step)
end
end
@@ -210,7 +255,7 @@ module Maptool
begin_run!(TERRAIN_MUTEX, TERRAIN_STATUS)
Thread.new do
run_job(TERRAIN_MUTEX, TERRAIN_STATUS) do |report_step|
run_job(TERRAIN_MUTEX, TERRAIN_STATUS, kind: "terrain", data_dir: data_dir, regions: codes) do |report_step|
build_terrain_pmtiles(codes, data_dir, fetch_manifest, report_step)
end
end
@@ -229,15 +274,44 @@ module Maptool
# SK (1/2)", "tiling with planetiler", ...) — surfaced as status[:step]
# so GET .../build-status can show progress beyond just "running since
# X" (see docs/development.adoc).
def self.run_job(mutex, status)
report_step = ->(text) { mutex.synchronize { status[:step] = text } }
yield report_step
#
# Each step's wall time is recorded too (label without its "— done: …"
# progress suffix) and the whole run appended to the build history
# above. The block may return { log: "..." } — Planetiler's own closing
# summary, with its per-phase breakdown — to keep alongside.
def self.run_job(mutex, status, kind:, data_dir:, regions:)
started = monotonic_now
steps = []
report_step = lambda do |text|
mutex.synchronize do
steps << [text.split(" — ").first, monotonic_now]
status[:step] = text
status[:steps] = steps
end
end
result = yield report_step
mutex.synchronize { status.merge!(state: "done", finished_at: Time.now.utc.iso8601).delete(:message) }
rescue => e
mutex.synchronize { status.merge!(state: "error", finished_at: Time.now.utc.iso8601, message: e.message) }
ensure
mutex.synchronize { status.delete(:step) }
mutex.synchronize do
status.delete(:step)
status.delete(:steps)
finished = monotonic_now
status[:timings] = step_durations(steps, finished)
status[:total_seconds] = (finished - started).round(1)
record_build(data_dir, kind: kind, regions: regions, status: status, log: result.is_a?(Hash) ? result[:log] : nil)
end
end
def self.record_build(data_dir, kind:, regions:, status:, log:)
entry = { kind: kind, regions: regions, threads: Etc.nprocessors, log: log }
.merge(status.slice(:state, :started_at, :finished_at, :total_seconds, :timings, :message)).compact
File.open(build_history_path(data_dir), "a") { |f| f.puts(JSON.generate(entry)) }
rescue SystemCallError
nil # a history write failing must never turn a finished build into an error
end
private_class_method :record_build
private_class_method :run_job
# --- offline.pmtiles ---
@@ -287,12 +361,13 @@ module Maptool
# leaving most of a many-core host idle — Etc.nprocessors asks the
# OS directly (sched_getaffinity on Linux), which correctly reflects
# whatever's actually usable by this process even under a container.
run_planetiler!("--osm-path=#{osm_path}", "--output=#{tmp_path}", "--download", "--threads=#{Etc.nprocessors}")
planetiler_log = run_planetiler!("--osm-path=#{osm_path}", "--output=#{tmp_path}", "--download", "--threads=#{Etc.nprocessors}")
File.rename(tmp_path, File.join(data_dir, "offline.pmtiles"))
File.write(offline_meta_path(data_dir), JSON.generate(sources: sources.map { |s| s.slice("code", "slug", "last_modified") }))
report_step.call("extracting hiking trails")
extract_hiking_trails(sources, data_dir)
{ log: planetiler_log }
ensure
FileUtils.remove_entry(scratch_dir) if scratch_dir && Dir.exist?(scratch_dir)
File.delete(tmp_path) if tmp_path && File.exist?(tmp_path)
@@ -734,9 +809,13 @@ module Maptool
end
private_class_method :run_osmium!
# Returns the output's tail — on success that's Planetiler's closing
# summary (per-phase times, memory), kept in the build history.
def self.run_planetiler!(*args)
stdout, stderr, status = Open3.capture3(java_bin, "-jar", planetiler_jar, *args)
raise BuildError, "planetiler exited with status #{status.exitstatus}: #{command_output(stdout, stderr)}" unless status.success?
command_output(stdout, stderr)
rescue Errno::ENOENT, Errno::EACCES => e
raise BuildError, "could not run '#{java_bin} -jar #{planetiler_jar}' (#{e.message}) — is Java and planetiler.jar installed? See docs/development.adoc."
end