Class: Binpacker::Timing

Inherits:
Object
  • Object
show all
Defined in:
lib/binpacker/timing.rb

Defined Under Namespace

Classes: Entry

Constant Summary collapse

DEFAULT_WEIGHT =
1.0

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Timing

Returns a new instance of Timing.



11
12
13
# File 'lib/binpacker/timing.rb', line 11

def initialize(path)
  @path = Pathname(path)
end

Instance Method Details

#append(file:, name:, time:) ⇒ Object



52
53
54
55
# File 'lib/binpacker/timing.rb', line 52

def append(file:, name:, time:)
  @path.dirname.mkpath unless @path.dirname.directory?
  @path.open("a") { |io| io.puts JSON.generate({ file: file, name: name, time: time }) }
end

#append_all(entries) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/binpacker/timing.rb', line 57

def append_all(entries)
  return if entries.empty?
  @path.dirname.mkpath unless @path.dirname.directory?
  @path.open("a") do |io|
    entries.each { |e| io.puts JSON.generate({ file: e[:file], name: e[:name], time: e[:time] }) }
  end
end

#load_per_fileObject



33
34
35
36
37
38
39
40
41
# File 'lib/binpacker/timing.rb', line 33

def load_per_file
  return {} unless @path.exist?

  @path.each_line
    .map { |line| parse_line(line) }
    .compact
    .group_by { |e| normalize_path(e.file) }
    .transform_values { |entries| entries.sum(&:time) }
end

#load_rawObject



23
24
25
26
27
28
29
30
31
# File 'lib/binpacker/timing.rb', line 23

def load_raw
  return {} unless @path.exist?

  @path.each_line
    .map { |line| parse_line(line) }
    .compact
    .group_by { |e| [normalize_path(e.file), e.name] }
    .transform_values { |entries| entries.last.time }
end

#load_with_fallback(tests) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/binpacker/timing.rb', line 15

def load_with_fallback(tests)
  per_file = load_per_file
  tests.each_with_object({}) do |test, hash|
    key = normalize_path(test.file)
    hash[test.key] = per_file.fetch(key) { filesize_weight(test.file) }
  end
end

#normalize_path(path) ⇒ Object



43
44
45
# File 'lib/binpacker/timing.rb', line 43

def normalize_path(path)
  Pathname(path).cleanpath.to_s.sub(/\A\.\//, "")
end

#weight_for(file:, name:) ⇒ Object



47
48
49
50
# File 'lib/binpacker/timing.rb', line 47

def weight_for(file:, name:)
  measured = load_raw
  measured.fetch([file, name]) { filesize_weight(file) }
end