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.2'

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

#graph_report(graph_path, report_yml_path) ⇒ Object



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
# File 'lib/bong.rb', line 63

def graph_report(graph_path, report_yml_path)
  # Require gruff here so people can run the rest of the app without gruff.
  require 'gruff'
  
  @report = YAML.load(File.read(report_yml_path))

  # Remove any with no date
  @report.reject! { |name, data| name.split("-").size != 2 }

  number_of_times = @report.size
  
  inverted_report = { }
  
  @report.each do |name, data|
    report_time = Time.at(name.split("-").last.to_i)
    date_time = report_time.strftime("%d/%m %H:%M")
    
    data.each do |host, urls|
      urls.each do |url, payload|
        inverted_report[url] ||= { }
        inverted_report[url][date_time] = payload['avg'] || nil
      end
    end      
  end
  
  inverted_report.each do |url, payload|
    inverted_report[url][:array] = inverted_report[url].to_a.sort.map{|ele| ele.last}
    missing_times = number_of_times - inverted_report[url][:array].size
    inverted_report[url][:array] = Array.new(missing_times) + inverted_report[url][:array]
  end
    
  g = Gruff::Line.new
  g.title = "Requests per second" 

  inverted_report.each do |url, payload|
    g.data(url, inverted_report[url][:array])
  end
  
  g.write(graph_path)
end

#load_report(report_yml_path, label = nil) ⇒ Object



104
105
106
107
108
# File 'lib/bong.rb', line 104

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



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bong.rb', line 110

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



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/bong.rb', line 123

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