benchy 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/benchy.rb +54 -40
  2. data/lib/benchy/version.rb +1 -1
  3. metadata +5 -5
@@ -23,7 +23,7 @@ module Benchy
23
23
  end
24
24
 
25
25
  def workers
26
- @workers ||= (0...concurrency).map{|n| Worker.new(request, "worker.#{n}") }
26
+ @workers ||= (0..concurrency).map{|n| Worker.new(request, "worker.#{n}") }
27
27
  end
28
28
  end
29
29
 
@@ -44,7 +44,7 @@ module Benchy
44
44
  run
45
45
  }
46
46
  http.errback {
47
- Benchy.logger.debug "Connection error!"
47
+ Benchy.logger.error "#{name}\t| Connection error!"
48
48
  halt # TODO - Make this fail the ping and try again, not halt
49
49
  }
50
50
  end
@@ -69,57 +69,71 @@ module Benchy
69
69
  # Grab an instance of an Em::Http request so we can run it somewhere.
70
70
  def em
71
71
  EventMachine::HttpRequest.new(url).send(method.downcase,
72
- :head => default_headers.merge(headers),
72
+ :head => headers,
73
73
  :body => body,
74
74
  :connect_timeout => 9000, # Disable
75
75
  :inactivity_timeout => 9000 # Disable
76
76
  )
77
77
  end
78
-
79
- # Setup smart default headers to minimize the chances that a request gets rejected.
80
- def default_headers
81
- default_headers = {}
82
- default_headers['Content-Type'] = 'application/binary-octet' if body
83
- default_headers['Accepts'] = '*/*'
84
- default_headers
85
- end
86
78
  end
87
79
 
88
80
  # Parse out some command line goodness
89
81
  class CLI < Thor
90
- desc "benchmark URL", "Run benchmarks against server"
91
- method_option :body,
92
- :type => :string,
93
- :aliases => '-b',
94
- :desc => "Request body"
95
- method_option :file,
96
- :type => :string,
97
- :aliases => '-f',
98
- :desc => "File for request body"
99
- method_option :headers,
100
- :type => :hash,
101
- :aliases => '-h',
102
- :desc => 'HTTP headers'
103
- method_option :method,
104
- :type => :string,
105
- :desc => "Request method",
106
- :aliases => '-m',
107
- :default => 'GET'
108
- method_option :concurrency,
109
- :type => :numeric,
110
- :desc => "Concurrent requests",
111
- :aliases => '-c',
112
- :default => 1
113
-
114
- def benchmark(url)
115
- req = Request.new(url, options[:method], options[:headers], self.class.body(options))
116
- EM.run { Dispatcher.new(req, options[:concurrency]).run }
82
+ %w[post put].each do |meth|
83
+ desc "#{meth} URL", "#{meth.upcase} to a URL"
84
+ method_option :body,
85
+ :type => :string,
86
+ :aliases => '-b',
87
+ :desc => "Request body"
88
+ method_option :headers,
89
+ :type => :hash,
90
+ :aliases => '-h',
91
+ :desc => 'Request headers',
92
+ :default => {
93
+ 'Content-Type' => 'application/binary-octet',
94
+ 'Accepts' => '*/*'
95
+ }
96
+ method_option :concurrency,
97
+ :type => :numeric,
98
+ :desc => "Concurrent requests",
99
+ :aliases => '-c',
100
+ :default => 1
101
+ define_method meth do |url|
102
+ request(url, meth, self.class.body(options))
103
+ end
104
+ end
105
+
106
+ %w[get head delete].each do |meth|
107
+ desc "#{meth} URL", "#{meth.upcase} to a URL"
108
+ method_option :headers,
109
+ :type => :hash,
110
+ :aliases => '-h',
111
+ :desc => 'Request headers',
112
+ :default => {
113
+ 'Content-Type' => 'application/binary-octet',
114
+ 'Accepts' => '*/*'
115
+ }
116
+ method_option :concurrency,
117
+ :type => :numeric,
118
+ :desc => "Concurrent requests",
119
+ :aliases => '-c',
120
+ :default => 1
121
+ define_method meth do |url|
122
+ request(url, meth)
123
+ end
117
124
  end
118
125
 
119
126
  private
120
- # Normalize the request body if its a file or a text string.
127
+ def request(url, method=:get, body=nil)
128
+ req = Request.new(url, method, options[:headers], body)
129
+ EM.run {
130
+ Dispatcher.new(req, options[:concurrency]).run
131
+ }
132
+ end
133
+
134
+ # Normalize the request body if its specified from the CLI or if its piped in from terminal
121
135
  def self.body(options)
122
- options[:file] ? File.read(options[:file]) : options[:body]
136
+ options[:body] || (STDIN.read unless STDIN.tty?)
123
137
  end
124
138
  end
125
139
  end
@@ -1,5 +1,5 @@
1
1
  require 'em-http-request'
2
2
 
3
3
  module Benchy
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: benchy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: em-http-request
16
- requirement: &70165834461960 !ruby/object:Gem::Requirement
16
+ requirement: &70157971397720 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70165834461960
24
+ version_requirements: *70157971397720
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: thor
27
- requirement: &70165834461540 !ruby/object:Gem::Requirement
27
+ requirement: &70157971397300 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70165834461540
35
+ version_requirements: *70157971397300
36
36
  description: A dirty-simple HTTP benchmarking application
37
37
  email:
38
38
  - brad@bradgessler.com