Class: Pdfrb::Task::Benchmark

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/task/benchmark.rb

Overview

Performance benchmarks for parsing, serialization, and round-trip. Uses the corpus generator to produce test PDFs of various sizes, then measures throughput.

Usage:

Pdfrb::Task::Benchmark.run
Pdfrb::Task::Benchmark.run(repetitions: 100)

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.bench_parse(name, pdf_bytes, repetitions) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pdfrb/task/benchmark.rb', line 36

def bench_parse(name, pdf_bytes, repetitions)
  io = StringIO.new(pdf_bytes)
  elapsed = ::Benchmark.realtime do
    repetitions.times do
      io.rewind
      Pdfrb::Document.new(io: StringIO.new(pdf_bytes))
    end
  end

  Result.new(
    name: "parse:#{name}",
    repetitions: repetitions,
    total_seconds: elapsed,
    ops_per_second: repetitions / elapsed,
    bytes_per_op: pdf_bytes.bytesize
  )
end

.bench_round_trip(_results, _repetitions) ⇒ Object



71
72
73
# File 'lib/pdfrb/task/benchmark.rb', line 71

def bench_round_trip(_results, _repetitions)
  nil
end

.bench_serialize(name, pdf_bytes, repetitions) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pdfrb/task/benchmark.rb', line 54

def bench_serialize(name, pdf_bytes, repetitions)
  doc = Pdfrb::Document.new(io: StringIO.new(pdf_bytes))
  elapsed = ::Benchmark.realtime do
    repetitions.times do
      StringIO.new.tap { |io| doc.write(io: io) }
    end
  end

  Result.new(
    name: "serialize:#{name}",
    repetitions: repetitions,
    total_seconds: elapsed,
    ops_per_second: repetitions / elapsed,
    bytes_per_op: pdf_bytes.bytesize
  )
end

.format_result(result) ⇒ Object



75
76
77
78
79
# File 'lib/pdfrb/task/benchmark.rb', line 75

def format_result(result)
  format("%-25s  %6.1f ops/s  %7.1f KB/op  %6.3fs total",
         result.name, result.ops_per_second,
         result.bytes_per_op / 1024.0, result.total_seconds)
end

.run(repetitions: 50) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pdfrb/task/benchmark.rb', line 20

def run(repetitions: 50)
  corpus = GenerateCorpus.all
  results = []

  corpus.each do |name, pdf_bytes|
    next if pdf_bytes.empty?

    r = bench_parse(name, pdf_bytes, repetitions)
    results << r
    puts format_result(r)
  end

  bench_round_trip(results, repetitions)
  results
end