Class: MilkTea::Linter::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/milk_tea/tooling/linter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProfile

Returns a new instance of Profile.



138
139
140
141
# File 'lib/milk_tea/tooling/linter.rb', line 138

def initialize
  @timings_ms = Hash.new(0.0)
  @counts = Hash.new(0)
end

Instance Attribute Details

#countsObject (readonly)

Returns the value of attribute counts.



136
137
138
# File 'lib/milk_tea/tooling/linter.rb', line 136

def counts
  @counts
end

#timings_msObject (readonly)

Returns the value of attribute timings_ms.



136
137
138
# File 'lib/milk_tea/tooling/linter.rb', line 136

def timings_ms
  @timings_ms
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/milk_tea/tooling/linter.rb', line 151

def empty?
  @timings_ms.empty?
end

#measure(name) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/milk_tea/tooling/linter.rb', line 143

def measure(name)
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  result = yield
  @timings_ms[name] += (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000.0
  @counts[name] += 1
  result
end

#rule_breakdown(limit: 10, min_ms: 0.1) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/milk_tea/tooling/linter.rb', line 175

def rule_breakdown(limit: 10, min_ms: 0.1)
  @timings_ms
    .filter_map do |name, total_ms|
      next unless name.start_with?("rule.")
      next if total_ms < min_ms

      count = @counts[name]
      {
        phase: name,
        code: name.delete_prefix("rule.").tr("_", "-"),
        total_ms: total_ms,
        count:,
        avg_ms: count.positive? ? (total_ms / count) : total_ms,
      }
    end
    .sort_by { |entry| -entry[:total_ms] }
    .first(limit)
end

#summary(limit: 10, min_ms: 0.1) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/milk_tea/tooling/linter.rb', line 155

def summary(limit: 10, min_ms: 0.1)
  @timings_ms
    .sort_by { |_name, total_ms| -total_ms }
    .filter_map do |name, total_ms|
      rounded_ms = total_ms.round(1)
      next if rounded_ms < min_ms

      count = @counts[name]
      count > 1 ? "#{name}:#{count}x/#{rounded_ms}" : "#{name}:#{rounded_ms}"
    end
    .first(limit)
    .join(',')
end

#total_time_ms(prefix: nil) ⇒ Object



169
170
171
172
173
# File 'lib/milk_tea/tooling/linter.rb', line 169

def total_time_ms(prefix: nil)
  return @timings_ms.values.sum unless prefix

  @timings_ms.sum { |name, total_ms| name.start_with?(prefix) ? total_ms : 0.0 }
end