From a4f6a773a909dda3b015835d8b7e57a99d6150af Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Wed, 8 May 2019 18:40:50 -0700 Subject: [PATCH] fix up time limits for headless/tests. fix a few tests now that peds walk slowly. --- docs/TODO_quality.md | 4 --- docs/TODO_ux.md | 2 ++ editor/src/game.rs | 4 ++- editor/src/render/pedestrian.rs | 2 +- geom/src/duration.rs | 2 +- headless/src/main.rs | 3 +- sim/src/sim.rs | 64 ++++++++++++++++----------------- tests/src/parking.rs | 8 ++--- tests/src/sim_determinism.rs | 16 ++++----- tests/src/trips.rs | 4 +-- 10 files changed, 53 insertions(+), 56 deletions(-) diff --git a/docs/TODO_quality.md b/docs/TODO_quality.md index 76c4a19245..1b0aca4786 100644 --- a/docs/TODO_quality.md +++ b/docs/TODO_quality.md @@ -137,7 +137,3 @@ - priority_queue crate... internally uses hash, so serialization and determinism probably borked - dig into individual events, still too many? - for laggy heads, often round down and try slightly too early - - - change sim.step API to something like step_to_time - - handle savestating better... use scheduler, but ensure at the end of that time - - make the desired speed in editor work better diff --git a/docs/TODO_ux.md b/docs/TODO_ux.md index a2d636a86f..5b89bdbb08 100644 --- a/docs/TODO_ux.md +++ b/docs/TODO_ux.md @@ -63,6 +63,8 @@ - it's a pity we have to redo DrawCar work for all those parked cars every tick - show FPS or some kind of measure of lag + - sim Benchmark seems less useful now + - some desired_speed should cause lag; cap it - sleep better in the event loop - first make UserInput borrow state and not need to consume - more speculative performance ideas diff --git a/editor/src/game.rs b/editor/src/game.rs index 8f6bbdc526..69280d9e46 100644 --- a/editor/src/game.rs +++ b/editor/src/game.rs @@ -38,7 +38,9 @@ pub enum Mode { impl GameState { pub fn new(flags: Flags, ctx: &mut EventCtx) -> GameState { - let splash = !flags.no_splash; + let splash = !flags.no_splash + && !format!("{}", flags.sim_flags.load.display()).contains("data/save"); + let mut rng = flags.sim_flags.make_rng(); let mut game = GameState { mode: Mode::Sandbox(SandboxMode::new(ctx)), diff --git a/editor/src/render/pedestrian.rs b/editor/src/render/pedestrian.rs index 41a31ec0c9..9f0defd6e2 100644 --- a/editor/src/render/pedestrian.rs +++ b/editor/src/render/pedestrian.rs @@ -64,7 +64,7 @@ impl DrawPedestrian { if input.waiting_for_turn.is_some() { draw_default.push((foot_color, left_foot.to_polygon())); draw_default.push((foot_color, right_foot.to_polygon())); - } else if (jitter && remainder < 3) || (!jitter && remainder >= 3) { + } else if jitter == (remainder < 3) { draw_default.push((foot_color, left_foot.to_polygon())); draw_default.push(( foot_color, diff --git a/geom/src/duration.rs b/geom/src/duration.rs index f622b4a623..dc9423d195 100644 --- a/geom/src/duration.rs +++ b/geom/src/duration.rs @@ -112,7 +112,7 @@ impl Duration { remainder -= minutes * 60.0; let seconds = remainder.floor(); remainder -= seconds; - let centis = (remainder / 0.1).round(); + let centis = (remainder / 0.1).floor(); ( hours as usize, diff --git a/headless/src/main.rs b/headless/src/main.rs index f50d65790d..65d1006d76 100644 --- a/headless/src/main.rs +++ b/headless/src/main.rs @@ -66,9 +66,10 @@ fn main() { sim.run_until_done( &map, move |sim, map| { + // TODO We want to savestate at the end of this time; this'll happen at the beginning. if Some(sim.time()) == save_at { sim.save(); - // Some simulatiosn run for a really long time, just do this. + // Some simulations run for a really long time, just do this. if enable_profiler { cpuprofiler::PROFILER.lock().unwrap().stop().unwrap(); } diff --git a/sim/src/sim.rs b/sim/src/sim.rs index 8e4193d1be..d274ea7e1c 100644 --- a/sim/src/sim.rs +++ b/sim/src/sim.rs @@ -429,13 +429,20 @@ impl Sim { &mut self, map: &Map, callback: F, + // Interpreted as a relative time time_limit: Option, ) { let mut benchmark = self.start_benchmark(); loop { + let dt = if let Some(lim) = time_limit { + // TODO Regular benchmark printing then doesn't happen :\ + self.time() + lim + } else { + Duration::seconds(30.0) + }; + match panic::catch_unwind(panic::AssertUnwindSafe(|| { - // TODO Doesn't respect time_limit! - self.step(&map, Duration::seconds(30.0)); + self.step(&map, dt); })) { Ok(()) => {} Err(err) => { @@ -454,9 +461,6 @@ impl Sim { ); } callback(self, map); - if Some(self.time()) >= time_limit { - panic!("Time limit {} hit", self.time); - } if self.is_done() { println!( "{}, speed = {}", @@ -465,6 +469,10 @@ impl Sim { ); break; } + + if let Some(lim) = time_limit { + panic!("Time limit {} hit", lim); + } } } @@ -472,40 +480,30 @@ impl Sim { &mut self, map: &Map, all_expectations: Vec, + // Interpreted as a relative time time_limit: Duration, ) { - // TODO Maybe can use run_until_done for this. - let mut benchmark = self.start_benchmark(); + // TODO No benchmark printing at all this way. + // TODO Doesn't stop early once all expectations are met. + let mut expectations = VecDeque::from(all_expectations); - loop { - if expectations.is_empty() { - return; - } - // TODO Doesn't respect time_limit! - self.step(&map, Duration::seconds(30.0)); - for ev in self.get_events_since_last_step() { - if ev == expectations.front().unwrap() { - println!("At {}, met expectation {:?}", self.time, ev); - expectations.pop_front(); - if expectations.is_empty() { - return; - } + self.step(&map, self.time() + time_limit); + for ev in self.get_events_since_last_step() { + if ev == expectations.front().unwrap() { + println!("At {}, met expectation {:?}", self.time, ev); + expectations.pop_front(); + if expectations.is_empty() { + return; } } - if benchmark.has_real_time_passed(Duration::seconds(1.0)) { - println!( - "{}, speed = {}", - self.summary(), - self.measure_speed(&mut benchmark, true) - ); - } - if self.time() == time_limit { - panic!( - "Time limit {} hit, but some expectations never met: {:?}", - self.time, expectations - ); - } } + if expectations.is_empty() { + return; + } + panic!( + "Time limit {} hit, but some expectations never met: {:?}", + time_limit, expectations + ); } } diff --git a/tests/src/parking.rs b/tests/src/parking.rs index 07b50a56d6..a3be05042f 100644 --- a/tests/src/parking.rs +++ b/tests/src/parking.rs @@ -38,9 +38,9 @@ pub fn run(t: &mut TestRunner) { car, ParkingSpot::new(north_parking, 4), )], - Duration::minutes(2), + Duration::minutes(6), ); - sim.just_run_until_done(&map, Some(Duration::minutes(4))); + sim.just_run_until_done(&map, Some(Duration::minutes(1))); }); t.run_slow("wander_around_for_parking", |h| { @@ -76,8 +76,8 @@ pub fn run(t: &mut TestRunner) { car, ParkingSpot::new(south_parking, 0), )], - Duration::minutes(2), + Duration::minutes(6), ); - sim.just_run_until_done(&map, Some(Duration::minutes(4))); + sim.just_run_until_done(&map, Some(Duration::minutes(1))); }); } diff --git a/tests/src/sim_determinism.rs b/tests/src/sim_determinism.rs index 7edd430d2b..c25bd4096a 100644 --- a/tests/src/sim_determinism.rs +++ b/tests/src/sim_determinism.rs @@ -1,5 +1,6 @@ use crate::runner::TestRunner; use abstutil::Timer; +use geom::Duration; use sim::{Scenario, Sim, SimFlags}; pub fn run(t: &mut TestRunner) { @@ -32,6 +33,7 @@ pub fn run(t: &mut TestRunner) { &mut Timer::throwaway(), ); + let dt = Duration::seconds(0.1); for _ in 1..600 { if sim1 != sim2 { // TODO need to sort dicts in json output to compare @@ -41,8 +43,8 @@ pub fn run(t: &mut TestRunner) { sim2.save() ); } - sim1.step(&map); - sim2.step(&map); + sim1.step(&map, dt); + sim2.step(&map, dt); } }); @@ -64,10 +66,8 @@ pub fn run(t: &mut TestRunner) { &mut Timer::throwaway(), ); - for _ in 1..600 { - sim1.step(&map); - sim2.step(&map); - } + sim1.step(&map, Duration::minutes(10)); + sim2.step(&map, Duration::minutes(10)); if sim1 != sim2 { panic!( @@ -79,9 +79,7 @@ pub fn run(t: &mut TestRunner) { let sim1_save = sim1.save(); - for _ in 1..60 { - sim1.step(&map); - } + sim1.step(&map, Duration::seconds(30.0)); if sim1 == sim2 { panic!( diff --git a/tests/src/trips.rs b/tests/src/trips.rs index b0186eb4a2..89d4b6fefd 100644 --- a/tests/src/trips.rs +++ b/tests/src/trips.rs @@ -32,8 +32,8 @@ pub fn run(t: &mut TestRunner) { ), Event::PedReachedBuilding(ped.unwrap(), goal_bldg), ], - Duration::minutes(3), + Duration::minutes(7), ); - sim.just_run_until_done(&map, Some(Duration::minutes(4))); + sim.just_run_until_done(&map, Some(Duration::minutes(1))); }); }