Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / maps / src / main / rust / tests / traffic_update_bench.rs
traffic_update_bench.rs108 lines · 4.0 KiBRaw
| 1 | //! Host timing for `updateTrafficNative`'s per-square walk, against a REAL planet archive. |
| 2 | //! |
| 3 | //! Ignored by default and gated on `MAMAPS_REAL_ARCHIVE` like `real_archive.rs`: |
| 4 | //! |
| 5 | //! ```text |
| 6 | //! MAMAPS_REAL_ARCHIVE=/path/to/world.mamaps \ |
| 7 | //! cargo test -p offlinerouter --release --test traffic_update_bench -- --ignored --nocapture |
| 8 | //! ``` |
| 9 | //! |
| 10 | //! `updateTrafficNative` runs on an IO thread after each 1° traffic square lands, and for every |
| 11 | //! edge in it recovers the source node (a binary search over the planet's node array), reads the |
| 12 | //! edge and both endpoints. Until this change it did all of that while holding `traffic_meta`, |
| 13 | //! the same lock the main thread's camera-settle prefetch (`ensureTrafficLoadedNative`) takes |
| 14 | //! first, so the main thread could sit behind the whole walk. This harness times that walk. |
| 15 | |
| 16 | use offlinerouter::graph::{Graph, DRIVING}; |
| 17 | use offlinerouter::routing::find_nearest_edge; |
| 18 | use std::collections::{HashSet, VecDeque}; |
| 19 | use std::time::Instant; |
| 20 | |
| 21 | fn archive() -> Option<String> { |
| 22 | match std::env::var("MAMAPS_REAL_ARCHIVE") { |
| 23 | Ok(p) if std::path::Path::new(&p).is_file() => Some(p), |
| 24 | _ => { |
| 25 | eprintln!("MAMAPS_REAL_ARCHIVE not set: skipped"); |
| 26 | None |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /// Up to `cap` edge ids whose source node lies in the 1° square at (`lat0`, `lon0`), found by a |
| 32 | /// breadth-first walk from the node nearest the square's centre — a stand-in for the edge ids a |
| 33 | /// traffic fetch for that square carries. |
| 34 | fn square_edges(g: &Graph, lat0: f64, lon0: f64, cap: usize) -> Vec<u64> { |
| 35 | let start = find_nearest_edge(g, lat0 + 0.5, lon0 + 0.5, DRIVING).node_a; |
| 36 | let inside = |n: u32| { |
| 37 | let m = g.node(n); |
| 38 | let (lat, lon) = (m.lat_e7 as f64 * 1e-7, m.lon_e7 as f64 * 1e-7); |
| 39 | lat >= lat0 && lat < lat0 + 1.0 && lon >= lon0 && lon < lon0 + 1.0 |
| 40 | }; |
| 41 | let mut seen = HashSet::new(); |
| 42 | let mut queue = VecDeque::from([start]); |
| 43 | let mut edges = Vec::new(); |
| 44 | while let Some(n) = queue.pop_front() { |
| 45 | if !seen.insert(n) || !inside(n) { |
| 46 | continue; |
| 47 | } |
| 48 | let (s, e) = g.edge_range(n); |
| 49 | for idx in s..e { |
| 50 | edges.push(idx); |
| 51 | let t = g.edge(n, idx).target; |
| 52 | if t < g.node_count { |
| 53 | queue.push_back(t); |
| 54 | } |
| 55 | } |
| 56 | if edges.len() >= cap { |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | edges |
| 61 | } |
| 62 | |
| 63 | /// The per-edge work `updateTrafficNative` does, minus the JNI copy. |
| 64 | fn walk(g: &Graph, edges: &[u64]) -> Vec<f64> { |
| 65 | let mut segments = Vec::with_capacity(edges.len() * 5); |
| 66 | for &edge_id in edges { |
| 67 | if edge_id >= g.edge_count { |
| 68 | continue; |
| 69 | } |
| 70 | let source = g.find_node_idx_for_edge(edge_id); |
| 71 | let edge = g.edge(source, edge_id); |
| 72 | let node_u = g.node(source); |
| 73 | if edge.target < g.node_count { |
| 74 | let node_v = g.node(edge.target); |
| 75 | segments.extend_from_slice(&[ |
| 76 | node_u.lat_e7 as f64 * 1e-7, |
| 77 | node_u.lon_e7 as f64 * 1e-7, |
| 78 | node_v.lat_e7 as f64 * 1e-7, |
| 79 | node_v.lon_e7 as f64 * 1e-7, |
| 80 | 1.0, |
| 81 | ]); |
| 82 | } |
| 83 | } |
| 84 | segments |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | #[ignore = "needs MAMAPS_REAL_ARCHIVE (80 GB planet archive)"] |
| 89 | fn the_traffic_square_walk() { |
| 90 | let Some(path) = archive() else { return }; |
| 91 | let g = Graph::load_archive(&path).expect("graph"); |
| 92 | for (name, lat0, lon0) in [("SF", 37.0, -123.0), ("LA", 34.0, -119.0), ("London", 51.0, -1.0)] { |
| 93 | let edges = square_edges(&g, lat0, lon0, 60_000); |
| 94 | let t = Instant::now(); |
| 95 | let first = walk(&g, &edges); |
| 96 | let first_ms = t.elapsed().as_secs_f64() * 1e3; |
| 97 | let t = Instant::now(); |
| 98 | let again = walk(&g, &edges); |
| 99 | let again_ms = t.elapsed().as_secs_f64() * 1e3; |
| 100 | assert_eq!(first.len(), again.len()); |
| 101 | println!( |
| 102 | "{name}: {} edges, walk {first_ms:.1} ms first pass, {again_ms:.1} ms warm \ |
| 103 | ({:.2} us/edge warm)", |
| 104 | edges.len(), |
| 105 | again_ms * 1e3 / edges.len().max(1) as f64 |
| 106 | ); |
| 107 | } |
| 108 | } |