Class: Binpacker::Timing

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

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.

Parameters:

  • (Object)


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



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

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

#append_all(entries) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/binpacker/timing.rb', line 62

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

#load_per_fileHash[String, untyped], {}

Returns:

  • (Hash[String, untyped], {})


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(encoding: "UTF-8")
    .map { |line| parse_line(line) }
    .compact
    .group_by { |e| normalize_path(e.file) }
    .transform_values { |entries| entries.sum(&:time) }
end

#load_rawHash[[String, untyped], untyped], {}

Returns:

  • (Hash[[String, untyped], untyped], {})


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(encoding: "UTF-8")
    .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

#measured?(file:, name:) ⇒ Boolean

True when a measured Weight already exists for this Test.

Parameters:

  • file: (Object)
  • name: (Object)

Returns:

  • (Boolean)


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

def measured?(file:, name:)
  load_raw.key?([normalize_path(file), name])
end

#normalize_path(path) ⇒ String

Parameters:

  • (Object)

Returns:

  • (String)


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



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

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