Class: Sc2::StepTimer

Inherits:
Object
  • Object
show all
Defined in:
lib/sc2ai/step_timer.rb

Overview

Tracks various metrics about your step time performance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot) ⇒ StepTimer

Returns a new instance of StepTimer.

Parameters:



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_timeFloat (readonly)

Returns Realtime average time per step in ms. Includes SC2 wait time.

Returns:

  • (Float)

    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_timeFloat

Returns Running average time per step in ms for recent couple of steps.

Returns:

  • (Float)

    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_timeFloat (readonly)

Returns Total average time per step in ms. “Ladder Time” as measured by aiarena.

Returns:

  • (Float)

    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_countInteger (readonly)

Returns Number of frames which passed previous on_step.

Returns:

  • (Integer)

    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_timeFloat (readonly)

Returns Previous on_step took this amount of ms to run.

Returns:

  • (Float)

    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_averageFloat (readonly)

Returns Running average time per step in ms for recent couple of steps.

Returns:

  • (Float)

    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

#allowanceObject

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

#summaryString

A one-line string summary of all tracked times

Returns:

  • (String)


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_hHash

A hash containing :avg_real_time, :avg_step_time, :avg_recent_step_time, :previous_on_step_count, :previous_on_step_time

Returns:

  • (Hash)


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

#updateObject



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