summaryrefslogtreecommitdiffhomepage
path: root/test/benchmark.rb
blob: 38dcc8fc9c1a1aee606c815bab548ba471e09ae4 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env ruby -s
# frozen_string_literal: true

$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
require 'axlsx'
require 'csv'
require 'benchmark'
Axlsx::trust_input = true
row = []
input = (32..126).to_a.pack('U*').chars.to_a
20.times { row << input.shuffle.join }
times = 3000
Benchmark.bmbm(30) do |x|
  x.report('axlsx_noautowidth') do
    p = Axlsx::Package.new
    p.workbook do |wb|
      wb.add_worksheet do |sheet|
        times.times do
          sheet << row
        end
      end
    end
    p.use_autowidth = false
    p.serialize("example_noautowidth.xlsx")
  end

  x.report('axlsx') do
    p = Axlsx::Package.new
    p.workbook do |wb|
      wb.add_worksheet do |sheet|
        times.times do
          sheet << row
        end
      end
    end
    p.serialize("example_autowidth.xlsx")
  end

  x.report('axlsx_shared') do
    p = Axlsx::Package.new
    p.workbook do |wb|
      wb.add_worksheet do |sheet|
        times.times do
          sheet << row
        end
      end
    end
    p.use_shared_strings = true
    p.serialize("example_shared.xlsx")
  end

  x.report('axlsx_stream') do
    p = Axlsx::Package.new
    p.workbook do |wb|
      wb.add_worksheet do |sheet|
        times.times do
          sheet << row
        end
      end
    end
    s = p.to_stream
    File.binwrite('example_streamed.xlsx', s.read)
  end

  x.report('csv') do
    CSV.open("example.csv", "wb") do |csv|
      times.times do
        csv << row
      end
    end
  end
end
File.delete("example.csv", "example_streamed.xlsx", "example_shared.xlsx", "example_autowidth.xlsx", "example_noautowidth.xlsx")