Class: Bong

Inherits:
Object
  • Object
show all
Defined in:
lib/bong.rb

Overview

A tool for running httperf against a website. Documentation coming soon.

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_yml_path, label) ⇒ Bong

Returns a new instance of Bong.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bong.rb', line 30

def initialize(config_yml_path, label)
  unless File.exist?(config_yml_path)
    puts <<-MESSAGE

    A config file could not be found at '#{config_yml_path}'.

    Please generate one with the -g option.

    MESSAGE
    exit
  end

  @config       = YAML.load(File.read(config_yml_path))
  @label        = label
  @stats        = {}

  @logger       = Logger.new(STDOUT)
  @logger.level = Logger::DEBUG
  
  @logger.info "Running suite for label '#{@label}'"
end

Class Method Details

.generate(config_yml_path) ⇒ Object

Generate a sample config file.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bong.rb', line 13

def self.generate(config_yml_path)
  config_data = {
    'servers' => ['localhost:3000'],
    'uris'    => ['/', '/pages/about'],
    'samples' => 2
  }

  if File.exist?(config_yml_path)
    puts("A config file already exists at '#{config_yml_path}'.")
    exit
  end

  File.open(config_yml_path, 'w') do |f|
    f.write config_data.to_yaml
  end
end

Instance Method Details

#load_report(report_yml_path, label = nil) ⇒ Object



62
63
64
65
66
# File 'lib/bong.rb', line 62

def load_report(report_yml_path, label=nil)
  @report = YAML.load(File.read(report_yml_path))
  label = label || @label || @report.keys.first
  @stats = @report[label]
end

#reportObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bong.rb', line 68

def report
  length_of_longest_uri = uris.inject(0) { |length, uri| uri_length = uri.length; (uri_length > length ? uri_length : length) }
  
  output = ["\n#{@label}"]
  servers.each do |server, port|
    output << "  #{server}"
    uris.each do |uri|
      output << "    #{format_string(uri, length_of_longest_uri)} #{format_rounded(@stats[server][uri]['avg_low'])}-#{format_rounded(@stats[server][uri]['avg_high'])} req/sec"
    end
  end
  output.join("\n")
end

#runObject



52
53
54
55
56
57
58
59
60
# File 'lib/bong.rb', line 52

def run
  servers.each do |server, port|
    port ||= 80
    @stats[server] = {}
    uris.each do |uri|
      run_benchmark(server, port, uri)
    end
  end
end

#save_report(path) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bong.rb', line 81

def save_report(path)
  @all_stats = {}
  if File.exist?(path)
    @all_stats = YAML.load(File.read(path))
  end
  
  @all_stats[@label] = @stats
  
  File.open(path, 'w') do |f|
    f.write @all_stats.to_yaml
  end
end