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 (49 lines) | stat: -rw-r--r-- 1,275 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
require 'puppet'

class Benchmarker
  def initialize(target, size)
    @size = size
    @direction = ENV['SER_DIRECTION'] == 'generate' ? :generate : :parse
    @format = ENV['SER_FORMAT'] == 'pson' ? :pson : :json

    puts "Benchmarker #{@direction} #{@format}"
  end

  def setup
  end

  def generate
    path = File.expand_path(File.join(__FILE__, '../catalog.json'))
    puts "Using catalog #{path}"

    @data = File.read(path)
    @catalog = JSON.parse(@data)
  end

  def run(args=nil)
    0.upto(@size) do |i|
      # This parses a catalog from JSON data, which is a combination of parsing
      # the data into a JSON hash, and the parsing the hash into a Catalog. It's
      # interesting to see just how slow that latter process is:
      #
      #   Puppet::Resource::Catalog.convert_from(:json, @data)
      #
      # However, for this benchmark, we're just testing how long JSON vs PSON
      # parsing and generation are, where we default to parsing JSON.
      #
      if @direction == :generate
        if @format == :pson
          PSON.dump(@catalog)
        else
          JSON.dump(@catalog)
        end
      else
        if @format == :pson
          PSON.parse(@data)
        else
          JSON.parse(@data)
        end
      end
    end
  end
end