File: table_bench.rb

package info (click to toggle)
ruby-prawn 2.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,320 kB
  • sloc: ruby: 15,654; sh: 43; makefile: 20
file content (52 lines) | stat: -rw-r--r-- 1,468 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'prawn'
require 'benchmark'

# Helpers for benchmark

class String
  CHARS = ('a'..'z').to_a
  def self.random(length)
    Array.new(length) { CHARS.sample }.join
  end
end

def data_for_table(columns, rows, string_size)
  Array.new(rows) { Array.new(columns) { String.random(string_size) } }
end

def benchmark_table_generation(columns, rows, string_size, options = {})
  data = data_for_table(columns, rows, string_size)
  Benchmark.bm do |x|
    x.report(
      "#{columns}x#{rows} table (#{columns * rows} cells, with #{string_size} "\
      'char string contents' \
      "#{", options = #{options.inspect}" unless options.empty?})"
    ) do
      Prawn::Document.new { table(data, options) }.render
    end
  end
end

# Slowest case: styled table, which is very squeezed horizontally,
#   so text has to be wrapped
benchmark_table_generation(
  26, 50, 10,
  row_colors: %w[FFFFFF F0F0FF],
  header: true,
  cell_style: { inline_format: true }
)

# Try building and rendering tables of different sizes
benchmark_table_generation(10, 400, 5)
benchmark_table_generation(10, 200, 5)
benchmark_table_generation(10, 100, 5)

# Try different optional arguments to Prawn::Document#table
benchmark_table_generation(10, 450, 5, cell_style: { inline_format: true })
benchmark_table_generation(
  10, 450, 5,
  row_colors: %w[FFFFFF F0F0FF],
  header: true,
  cell_style: { inline_format: true }
)