Module: TWPipeline::Bench

Defined in:
lib/twpipeline/bench.rb,
sig/twpipeline.rbs

Defined Under Namespace

Classes: Sample

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cpu_secondsObject



42
43
44
45
# File 'lib/twpipeline/bench.rb', line 42

def cpu_seconds
  times = Process.times
  times.utime + times.stime + times.cutime + times.cstime
end

.measure(label, **facts) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/twpipeline/bench.rb', line 27

def measure(label, **facts)
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  cpu_before = cpu_seconds
  result = yield
  sample = Sample.new(
    label: label,
    seconds: Process.clock_gettime(Process::CLOCK_MONOTONIC) - started,
    cpu_seconds: cpu_seconds - cpu_before,
    peak_rss_bytes: peak_rss,
    facts: facts.merge(result.is_a?(Hash) ? result : {})
  )
  record(sample)
  sample
end

.pathObject



25
# File 'lib/twpipeline/bench.rb', line 25

def path = Pathname(ENV.fetch("TWP_BENCHMARKS", TWPipeline.work.join("benchmarks.jsonl").to_s))

.peak_rssObject



47
# File 'lib/twpipeline/bench.rb', line 47

def peak_rss = Rusage.peak

.record(sample) ⇒ Object



49
50
51
52
53
# File 'lib/twpipeline/bench.rb', line 49

def record(sample)
  path.dirname.mkpath
  path.open("a") { |io| io.puts(JSON.generate({at: Time.now.utc.iso8601}.merge(sample.to_h))) }
  sample
end

Instance Method Details

#self?.cpu_seconds::Float

Returns:

  • (::Float)


78
# File 'sig/twpipeline.rbs', line 78

def self?.cpu_seconds: () -> ::Float

#self?.measure { ... } ⇒ Sample

Parameters:

  • label (::String)
  • facts (Object)

Yields:

Yield Returns:

  • (Object)

Returns:



77
# File 'sig/twpipeline.rbs', line 77

def self?.measure: (::String label, **untyped facts) { () -> untyped } -> Sample

#self?.pathPathname

Returns:

  • (Pathname)


76
# File 'sig/twpipeline.rbs', line 76

def self?.path: () -> Pathname

#self?.peak_rss::Integer

Returns:

  • (::Integer)


79
# File 'sig/twpipeline.rbs', line 79

def self?.peak_rss: () -> ::Integer

#self?.recordSample

Parameters:

Returns:



80
# File 'sig/twpipeline.rbs', line 80

def self?.record: (Sample) -> Sample