-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbenchmarker.rb
115 lines (101 loc) · 3.98 KB
/
benchmarker.rb
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require 'erb'
require 'ostruct'
require 'fileutils'
require 'json'
# For memory debugging - if the core_ext is not loaded, things break inside mass
# require 'mass'
require 'objspace'
# Only runs for Ruby > 2.1.0, and must do this early since ObjectSpace.trace_object_allocations_start must be called
# as early as possible.
#
RUBYVER_ARRAY = RUBY_VERSION.split(".").collect {|s| s.to_i }
RUBYVER = (RUBYVER_ARRAY[0] << 16 | RUBYVER_ARRAY[1] << 8 | RUBYVER_ARRAY[2])
if RUBYVER < (2 << 16 | 1 << 8 | 0)
puts "catalog_memory requires Ruby version >= 2.1.0 to run. Skipping"
exit(0)
end
ObjectSpace.trace_object_allocations_start
class Benchmarker
include FileUtils
def initialize(target, size)
@target = target
@size = size
@@first_counts = nil
@@first_refs = nil
@@count = 0
end
def setup
end
def run(args=nil)
unless @initialized
require 'puppet'
config = File.join(@target, 'puppet.conf')
Puppet.initialize_settings(['--config', config])
@initialized = true
end
@@count += 1
env = Puppet.lookup(:environments).get('benchmarking')
node = Puppet::Node.new("testing", :environment => env)
# Mimic what apply does (or the benchmark will in part run for the *root* environment)
Puppet.push_context({:current_environment => env},'current env for benchmark')
Puppet::Resource::Catalog.indirection.find("testing", :use_node => node)
Puppet.pop_context
GC.start
sleep(2)
counted = ObjectSpace.count_objects({})
if @@first_counts && @@count == 10
diff = @@first_counts.merge(counted) {|k, base_v, new_v| new_v - base_v }
puts "Count of objects TOTAL = #{diff[:TOTAL]}, FREE = #{diff[:FREE]}, T_OBJECT = #{diff[:T_OBJECT]}, T_CLASS = #{diff[:T_CLASS]}"
changed = diff.reject {|k,v| v == 0}
puts "Number of changed classes = #{changed}"
GC.start
# Find references to leaked Objects
leaked_instances = ObjectSpace.each_object.reduce([]) {|x, o| x << o.object_id; x } - @@first_refs
File.open("diff.json", "w") do |f|
leaked_instances.each do |id|
o = ObjectSpace._id2ref(id)
f.write(ObjectSpace.dump(o)) if !o.nil?
end
end
# Output information where bound objects where instantiated
map_of_allocations = leaked_instances.reduce(Hash.new(0)) do |memo, x|
o = ObjectSpace._id2ref(x)
class_path = ObjectSpace.allocation_class_path(o)
class_path = class_path.nil? ? ObjectSpace.allocation_sourcefile(o) : class_path
if !class_path.nil?
method = ObjectSpace.allocation_method_id(o)
source_line = ObjectSpace.allocation_sourceline(o)
memo["#{class_path}##{method}-#{source_line}"] += 1
end
memo
end
map_of_allocations.sort_by {|k, v| v}.reverse_each {|k,v| puts "#{v} #{k}" }
# Dump the heap for further analysis
GC.start
ObjectSpace.dump_all(output: File.open('heap.json','w'))
elsif @@count == 1
# Set up baseline and output info for first run
@@first_counts = counted
@@first_refs = ObjectSpace.each_object.reduce([]) {|x, o| x << o.object_id; x }
diff = @@first_counts
puts "Count of objects TOTAL = #{diff[:TOTAL]}, FREE = #{diff[:FREE]}, T_OBJECT = #{diff[:T_OBJECT]}, T_CLASS = #{diff[:T_CLASS]}"
end
end
def generate
environment = File.join(@target, 'environments', 'benchmarking')
templates = File.join('benchmarks', 'empty_catalog')
mkdir_p(File.join(environment, 'modules'))
mkdir_p(File.join(environment, 'manifests'))
render(File.join(templates, 'site.pp.erb'),
File.join(environment, 'manifests', 'site.pp'),{})
render(File.join(templates, 'puppet.conf.erb'),
File.join(@target, 'puppet.conf'),
:location => @target)
end
def render(erb_file, output_file, bindings)
site = ERB.new(File.read(erb_file))
File.open(output_file, 'w') do |fh|
fh.write(site.result(OpenStruct.new(bindings).instance_eval { binding }))
end
end
end