Class: Sc2::StepTimer
- Inherits:
-
Object
- Object
- Sc2::StepTimer
- Defined in:
- lib/sc2ai/step_timer.rb
Overview
Tracks various metrics about your step time performance.
Instance Attribute Summary collapse
-
#avg_real_time ⇒ Float
readonly
Realtime average time per step in ms.
-
#avg_recent_step_time ⇒ Float
Running average time per step in ms for recent couple of steps.
-
#avg_step_time ⇒ Float
readonly
Total average time per step in ms.
-
#previous_on_step_count ⇒ Integer
readonly
Number of frames which passed previous on_step.
-
#previous_on_step_time ⇒ Float
readonly
Previous on_step took this amount of ms to run.
-
#recent_average ⇒ Float
readonly
Running average time per step in ms for recent couple of steps.
Instance Method Summary collapse
-
#allowance ⇒ Object
How much time we have left in this step, to be healthy.
-
#initialize(bot) ⇒ StepTimer
constructor
A new instance of StepTimer.
-
#summary ⇒ String
A one-line string summary of all tracked times.
-
#to_h ⇒ Hash
A hash containing :avg_real_time, :avg_step_time, :avg_recent_step_time, :previous_on_step_count, :previous_on_step_time.
- #update ⇒ Object
Constructor Details
#initialize(bot) ⇒ StepTimer
Returns a new instance of StepTimer.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sc2ai/step_timer.rb', line 37 def initialize(bot) @bot = bot # Tracking vars @previous_external_time = @bot.api.external_time @previous_update_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) @recent_update_counter = 0 @recent_sum_steps = 0 @recent_sum_time = 0.0 @total_step_time = 0.0 @total_real_time = 0.0 # Output vars @avg_real_time = 0.0 @avg_step_time = 0.0 @avg_recent_step_time = 0.0 @previous_on_step_time = 0.0 @previous_on_step_count = 0 end |
Instance Attribute Details
#avg_real_time ⇒ Float (readonly)
Returns Realtime average time per step in ms. Includes SC2 wait time.
10 11 12 |
# File 'lib/sc2ai/step_timer.rb', line 10 def avg_real_time @avg_real_time end |
#avg_recent_step_time ⇒ Float
Returns Running average time per step in ms for recent couple of steps.
18 19 20 |
# File 'lib/sc2ai/step_timer.rb', line 18 def avg_recent_step_time @avg_recent_step_time end |
#avg_step_time ⇒ Float (readonly)
Returns Total average time per step in ms. “Ladder Time” as measured by aiarena.
14 15 16 |
# File 'lib/sc2ai/step_timer.rb', line 14 def avg_step_time @avg_step_time end |
#previous_on_step_count ⇒ Integer (readonly)
Returns Number of frames which passed previous on_step.
26 27 28 |
# File 'lib/sc2ai/step_timer.rb', line 26 def previous_on_step_count @previous_on_step_count end |
#previous_on_step_time ⇒ Float (readonly)
Returns Previous on_step took this amount of ms to run.
22 23 24 |
# File 'lib/sc2ai/step_timer.rb', line 22 def previous_on_step_time @previous_on_step_time end |
#recent_average ⇒ Float (readonly)
Returns Running average time per step in ms for recent couple of steps.
18 |
# File 'lib/sc2ai/step_timer.rb', line 18 attr_accessor :avg_recent_step_time |
Instance Method Details
#allowance ⇒ Object
How much time we have left in this step, to be healthy
58 59 60 61 62 63 |
# File 'lib/sc2ai/step_timer.rb', line 58 def allowance return 0.0 if @bot.realtime time_passed = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) - @previous_update_time external_delta = @bot.api.external_time - @previous_external_time (HEALTHY_STEP_TIME_MS * @bot.step_count) - (time_passed - external_delta) end |
#summary ⇒ String
A one-line string summary of all tracked times
67 68 69 70 71 72 |
# File 'lib/sc2ai/step_timer.rb', line 67 def summary "AVG Real: #{format_time(@avg_real_time)} | " \ "AVG Total: #{format_time(@avg_step_time)} | " \ "AVG Recent: #{format_time(@avg_recent_step_time)} | " \ "Previous #{@previous_on_step_count} Step(s): #{format_time(@previous_on_step_time)} (ms)" end |
#to_h ⇒ Hash
A hash containing :avg_real_time, :avg_step_time, :avg_recent_step_time, :previous_on_step_count, :previous_on_step_time
76 77 78 79 80 81 82 83 84 |
# File 'lib/sc2ai/step_timer.rb', line 76 def to_h { avg_real_time: avg_real_time, avg_step_time: avg_step_time, avg_recent_step_time: avg_recent_step_time, previous_on_step_count: previous_on_step_count, previous_on_step_time: previous_on_step_time } end |
#update ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/sc2ai/step_timer.rb', line 86 def update # Number of steps which have passed step_delta = @bot.game_loop - @bot.previous.game_loop # Time spent waiting for SC2 is not counted on ladder external_time = @bot.api.external_time external_delta = external_time - @previous_external_time @previous_external_time = external_time # Start calculating... now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) time_delta = now - @previous_update_time @previous_update_time = now # Update real time @total_real_time += time_delta # Update step time (excl external) time_delta -= external_delta @total_step_time += time_delta # Write public values... @previous_on_step_time = time_delta @previous_on_step_count = step_delta @avg_real_time = @total_real_time / (@bot.game_loop + 1) @avg_step_time = @total_step_time / (@bot.game_loop + 1) @recent_sum_time += time_delta @recent_sum_steps += step_delta if @recent_update_counter >= 11 @avg_recent_step_time = @recent_sum_time / @recent_sum_steps @recent_sum_time = 0.0 @recent_sum_steps = 0 @recent_update_counter = 0 end @recent_update_counter += 1 end |