File: benchmarker.rb

package info (click to toggle)
puppet-agent 8.10.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 27,392 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (112 lines) | stat: -rw-r--r-- 3,475 bytes parent folder | download
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
require 'erb'
require 'ostruct'
require 'fileutils'
require 'json'

class Benchmarker
  include FileUtils

  def initialize(target, size)
    @target = target
    @size = size
    @@benchmark_count ||= 0
  end

  def setup
    require 'puppet'
    config = File.join(@target, 'puppet.conf')
    Puppet.initialize_settings(['--config', config])
  end

  def run(args=nil)
    envs = Puppet.lookup(:environments)

    envs.clear('benchmarking')
#    # Uncomment to get some basic memory statistics
#    ObjectSpace.garbage_collect
#    result = {}
#    ObjectSpace.count_objects(result)
#    puts "C(#{result[:T_CLASS]}) O(#{result[:T_OBJECT]}) F(#{result[:FREE]}) #{result[:T_CLASS] + result[:T_OBJECT]}"
    node = Puppet::Node.new('testing', :environment => envs.get('benchmarking'))
    Puppet::Resource::Catalog.indirection.find('testing', :use_node => node)
  end

  def benchmark_count
    bc = @@benchmark_count
    @@benchmark_count += 1
    bc
  end

  def generate
    environment = File.join(@target, 'environments', 'benchmarking')
    modules = File.join(environment, 'modules')
    env_functions = File.join(environment, 'lib', 'puppet', 'functions', 'environment')
    templates = File.join('benchmarks', 'function_loading')

    mkdir_p(modules)
    mkdir_p(env_functions)
    mkdir_p(File.join(environment, 'manifests'))

    module_count = @size / 5
    function_count = @size * 3
    render(File.join(templates, 'site.pp.erb'),
           File.join(environment, 'manifests', 'site.pp'),
           :size => module_count,
           :function_count => function_count)

    env_function_template = File.join(templates, 'env_function.erb')
    function_count.times { |n| render(env_function_template, File.join(env_functions, "f#{n}.rb"), :n => n) }

    module_count.times do |i|
      module_name = "module#{i}"
      module_base = File.join(modules, module_name)
      manifests = File.join(module_base, 'manifests')
      module_functions = File.join(module_base, 'lib', 'puppet', 'functions', module_name)

      mkdir_p(manifests)
      mkdir_p(module_functions)

      File.open(File.join(module_base, 'metadata.json'), 'w') do |f|
        JSON.dump({
          'name' => "tester-#{module_name}",
          'author' => 'Puppet Labs tester',
          'license' => 'Apache 2.0',
          'version' => '1.0.0',
          'summary' => 'Benchmark module',
          'dependencies' => dependencies_for(i),
          'source' => ''
        }, f)
      end

      render(File.join(templates, 'module', 'init.pp.erb'),
             File.join(manifests, 'init.pp'),
             :name => module_name, :mc => i, :function_count => function_count)

      function_template = File.join(templates, 'module', 'function.erb')
      function_count.times do |n|
        render(function_template,
               File.join(module_functions, "f#{n}.rb"),
               :name => module_name, :n => n)
      end
    end

    render(File.join(templates, 'puppet.conf.erb'),
           File.join(@target, 'puppet.conf'),
           :location => @target)
  end

  def dependencies_for(n)
    return [] if n == 0
    Array.new(n) do |m|
      {'name' => "tester-module#{m}", 'version_requirement' => '1.0.0'}
    end
  end

  def render(erb_file, output_file, bindings)
    site = ERB.new(File.read(erb_file))
    site.filename = erb_file
    File.open(output_file, 'w') do |fh|
      fh.write(site.result(OpenStruct.new(bindings).instance_eval { binding }))
    end
  end
end