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.



141
142
143
144
# File 'lib/milk_tea/tooling/linter.rb', line 141

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

Instance Attribute Details

#countsObject (readonly)

Returns the value of attribute counts.



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

def counts
  @counts
end

#timings_msObject (readonly)

Returns the value of attribute timings_ms.



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

def timings_ms
  @timings_ms
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/milk_tea/tooling/linter.rb', line 154

def empty?
  @timings_ms.empty?
end

#measure(name) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/milk_tea/tooling/linter.rb', line 146

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



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

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



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/milk_tea/tooling/linter.rb', line 158

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



172
173
174
175
176
# File 'lib/milk_tea/tooling/linter.rb', line 172

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