File: bench_full_load_path.rb

package info (click to toggle)
jruby 9.4.8.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 89,244 kB
  • sloc: ruby: 548,574; java: 276,189; yacc: 25,873; ansic: 6,178; xml: 6,111; sh: 1,855; sed: 94; makefile: 78; jsp: 48; tcl: 40; exp: 12
file content (56 lines) | stat: -rw-r--r-- 1,214 bytes parent folder | download | duplicates (5)
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
require 'benchmark'
require 'fileutils'

size = (ARGV.shift || '500').to_i
target_size = (ARGV.shift || '10000').to_i # must be bigger than 50
require_size = 50

# for testing with abloute path
#TEST_DIR = File.expand_path('full_load_path_test', File.dirname(__FILE__))

# for testing with relative path
Dir.chdir File.dirname(__FILE__)
TEST_DIR = 'full_load_path_test'

unless File.directory?(TEST_DIR)
  FileUtils.mkdir(TEST_DIR)
  target_size.times do |idx|
    FileUtils.touch(File.join(TEST_DIR, "bogus#{idx}.rb"))
  end
end

def fill_load_path(size)
  $LOAD_PATH.unshift(TEST_DIR)
  size.times do |idx|
    $LOAD_PATH.unshift("/tmp/folder#{idx}")
  end
end

def with_loadvars(size)
  load_path, loaded_features = $LOAD_PATH.dup, $LOADED_FEATURES.dup
  begin
    fill_load_path(size)
    yield
  ensure
    $LOAD_PATH.replace(load_path)
    $LOADED_FEATURES.replace(loaded_features)
  end
end

gets

Benchmark.bmbm do |bm|
  bm.report("empty counterpart") do
    with_loadvars(size) do
      # do nothing
    end
  end

  bm.report("#{require_size} times require, #{size} in load path") do
    with_loadvars(size) do
      require_size.times do |idx|
        require "bogus#{idx}"
      end
    end
  end
end