require_bench.rb

Pavel Rosick√Ĺ, 2019-10-20 14:23

Download (1.42 KB)

 
1
env = (<<'EOS')
2
  require "bundler/inline"
3

4
  gemfile(false) do
5
    source "https://rubygems.org"
6
    gem "rails", '5.2.3'
7
    gem "get_process_mem", "0.2.4"
8
    gem "sys-proctable", "1.1.5" if Gem.win_platform?
9
    gem "memory_profiler", "0.9.14" if profile
10
  end
11

12
  require 'benchmark'
13
  require 'get_process_mem'
14

15
  profile ||= false
16
  MemoryProfiler.start if profile
17
  result = Benchmark.measure { test }
18
  profile = MemoryProfiler.stop if profile
19
  puts "benchmark #{name}, mem: #{GetProcessMem.new.mb}mb, time: #{result.total}s"
20
  if profile
21
    profile_name = "profile_#{name}_#{Process.pid}.txt"
22
    puts "Profile written to: #{profile_name}"
23
    profile.pretty_print(to_file: profile_name)
24
  end
25
EOS
26

    
27
new = <<-RUBY
28
  def test
29
    require 'rails'
30

31
    require 'active_model/railtie'
32
    require 'active_job/railtie'
33
    require 'active_record/railtie'
34
    require 'active_storage/engine'
35
    require 'action_controller/railtie'
36
    require 'action_mailer/railtie'
37
    require 'action_view/railtie'
38
    require 'action_cable/engine'
39
    # require 'sprockets/railtie'
40
    require 'rails/test_unit/railtie'
41
  end
42
RUBY
43

    
44
old = <<-RUBY
45
  def test
46
    require 'rails/all'
47
  end
48
RUBY
49

    
50
(ENV['ITERATIONS'] || 10).to_i.times do
51
  ['new', 'old'].each do |name|
52
    pid = Process.spawn(RbConfig.ruby, "-eprofile=#{!!ENV['PROFILE']};name='#{name}';#{eval(name)}#{env}")
53
    Process.wait pid
54
  end
55
end