summaryrefslogtreecommitdiff
path: root/support/benchmarks/load.rb
blob: 0712676534f6743fdd7e42621c33bf9a2e2c9f88 (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
# frozen_string_literal: true

require 'benchmark'
require 'mime/types'

module Benchmarks
  # Benchmark loading speed
  class Load
    def self.report(load_path, repeats)
      new(load_path, repeats.to_i).report
    end

    def initialize(load_path, repeats = nil)
      @cache_file = File.expand_path('../cache.mtc', __FILE__)
      @repeats    = repeats.to_i
      @repeats    = 50 if @repeats <= 0
      @load_path  = load_path
    end

    def reload_mime_types(repeats = 1, force: false, columnar: false, cache: false)
      loader = MIME::Types::Loader.new

      repeats.times {
        types = MIME::Types::Cache.load if cache
        unless types
          types = loader.load(columnar: columnar)
          MIME::Types::Cache.save(types) if cache
        end
        types.first.to_h if force
      }
    end

    def report
      remove_cache

      Benchmark.bm(30) do |mark|
        mark.report('Normal') do reload_mime_types(@repeats) end
        mark.report('Columnar') do
          reload_mime_types(@repeats, columnar: true)
        end
        mark.report('Columnar Full') do
          reload_mime_types(@repeats, columnar: true, force: true)
        end

        ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file
        mark.report('Cache Initialize') do reload_mime_types(cache: true) end
        mark.report('Cached') do reload_mime_types(@repeats, cache: true) end

        remove_cache
        ENV['RUBY_MIME_TYPES_CACHE'] = @cache_file
        mark.report('Columnar Cache Initialize') do
          reload_mime_types(columnar: true, cache: true)
        end
        mark.report('Columnar Cached') {
          reload_mime_types(@repeats, columnar: true, cache: true)
        }
      end
    ensure
      remove_cache
    end

    def remove_cache
      File.unlink(@cache_file) if File.exist?(@cache_file)
    end
  end
end