LifeEngine icon indicating copy to clipboard operation
LifeEngine copied to clipboard

Added timer and stats for fps calculation

Open lancelot2112 opened this issue 4 years ago • 5 comments

Added timer and stats for throughput calculation to get an idea of where the bottlenecks are.

lancelot2112 avatar Dec 04 '21 17:12 lancelot2112

sim_time

Created some helper classes for timing. not sure on best way to show stats other than console.log. didn't want to hog throughput on that so updates every second. Max times were logged ~2400 organisms with the sim_time + ui_time == 53.8ms if running on a single thread the max fps would be 18.6fps.

Splitting the sim_time and ui_time onto different threads may be a viable short term improvement. Would change the maximum "simulation" fps calculation for 2400 organisms to just sim_time {20.1ms} or 50fps

lancelot2112 avatar Dec 04 '21 17:12 lancelot2112

I'm actually seeing the infinite fps bug #42 in this. Also could you name your variables more descriptively? val and take in particular are confusing to me. What average is being taken with take anyway, average fps? time per tick?

MaxRobinsonTheGreat avatar Dec 04 '21 20:12 MaxRobinsonTheGreat

I'll have to look a little closer at the infinity, might just be that two calls to the function happened so close together that the timer resolution wasn't big enough to update? I'll look closer.

It's a generic class, can be used anywhere you need a time or frequency of exec It's not specific until you create an instance of it. There are two instances one for the UI timer and one for the Simulation timer.

Usage

sim_timer = new Timer();  //instance 1
ui_timer = new Timer();  //instance 2

   sim_update() {
       //takes internal state of the sim_timer {instance 1} and get delta time since last update call...  does not modify ui_timer
       sim_timer.update();  
       
       //does not change internal state of timer but calculates delta time since last update call and returns.  
       let sim_exec_time = sim_timer.tick();  
   }

the Stats class is also generic. so it could be used to hold an average for anything. Am I misunderstanding you perhaps? So in the code there are two instances. One for the "tick" stats and one for the "update" stats.

lancelot2112 avatar Dec 04 '21 21:12 lancelot2112

Were you running firefox perchance? Seems firefox does some rounding to the nearest millisecond, will protect for this.

https://stackoverflow.com/questions/6875625/does-javascript-provide-a-high-resolution-timer

lancelot2112 avatar Dec 04 '21 21:12 lancelot2112

Changes to Timer class: changed "tick" to "time_since_last_update" and "update" to "update_state".
Changes to Stats class: changed "take" to "add_value"

lancelot2112 avatar Dec 04 '21 22:12 lancelot2112