File: table_bench.rb

package info (click to toggle)
ruby-prawn 2.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,220 kB
  • ctags: 826
  • sloc: ruby: 14,469; sh: 43; makefile: 18
file content (40 lines) | stat: -rw-r--r-- 1,479 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
# encoding: utf-8

$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)
    length.times.collect { CHARS.sample }.join
  end
end

def data_for_table(columns, rows, string_size)
  rows.times.collect { columns.times.collect { 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 => ['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 => ['FFFFFF', 'F0F0FF'], :header => true, :cell_style => { :inline_format => true })