Record per-step timings of offline/terrain builds
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:
1 parent
7429e4ae33
commit
81e688be9b
4 files changed
+110
-9
No files matched your search
@@ -320,6 +320,12 @@ module Maptool
|
||||
json PmtilesBuilder.offline_status
|
||||
end
|
||||
|
||||
# Per-step timings of past offline/terrain builds, newest first — the
|
||||
# "benchmark" to compare a later build against (debug panel).
|
||||
get "/api/build-history" do
|
||||
json PmtilesBuilder.build_history(DATA_DIR)
|
||||
end
|
||||
|
||||
# Deletes offline.pmtiles plus its whole per-country download cache —
|
||||
# a manual "reclaim the space"/"start over" action, distinct from
|
||||
# unticking countries in the region picker (which only changes what a
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -13,6 +13,7 @@ const ENDPOINTS = [
|
||||
"/api/offline-tiles/cache",
|
||||
"/api/terrain-tiles",
|
||||
"/api/terrain-tiles/build-status",
|
||||
"/api/build-history",
|
||||
"/api/hiking-trails",
|
||||
"/api/kiwix",
|
||||
"/api/plugins",
|
||||
|
||||
@@ -216,6 +216,21 @@ class PmtilesBuilderTest < Minitest::Test
|
||||
)
|
||||
end
|
||||
|
||||
def test_finished_builds_are_recorded_with_per_step_timings_newest_first
|
||||
select_regions("SK")
|
||||
Maptool::PmtilesBuilder.build_offline(data_dir: @data_dir, download_extract: fake_download).join
|
||||
ENV["JAVA_BIN"] = @java_fail
|
||||
Maptool::PmtilesBuilder.build_offline(data_dir: @data_dir, download_extract: fake_download).join
|
||||
|
||||
failed, done = Maptool::PmtilesBuilder.build_history(@data_dir)
|
||||
assert_equal ["error", "done"], [failed["state"], done["state"]]
|
||||
assert_equal ["offline", ["SK"]], done.values_at("kind", "regions")
|
||||
assert_equal ["fetching SK (1/1)", "tiling with planetiler", "extracting hiking trails"], done["timings"].map { |t| t["step"] }
|
||||
assert(done["timings"].all? { |t| t["seconds"] >= 0 })
|
||||
assert done.key?("log")
|
||||
assert_match(/boom/, failed["message"])
|
||||
end
|
||||
|
||||
def test_build_offline_also_writes_a_per_country_hiking_trails_geojson
|
||||
select_regions("SK")
|
||||
thread = Maptool::PmtilesBuilder.build_offline(data_dir: @data_dir, download_extract: fake_download)
|
||||
|
||||
Reference in new issue
Block a user