lib/bigbench/fragment.rb in bigbench-0.0.1 vs lib/bigbench/fragment.rb in bigbench-0.0.2

- old
+ new

@@ -8,97 +8,110 @@ # end # # Possible fragment types are the HTTP verbs, like GET, POST, PUT and DELETE. They look like this: # # get "/" - # post "/login/new", { :name => "a name", :password => "secret" } - # put "/books", { :name => "A Book title", :rating => 30.4 } - # delete "/books/5" + # post "/login/new" + # put "/books" + # delete "/books/5", :params => { :token => '87bas67dfjgbrjbbgbi6ica7s0b3t0' } + # get "/admin", :basic_auth => ['username', 'password'] # + # After any fragment an options hash can be appended. Possible options are: + # + # [:basic_auth] Supply a basic auth user and password for the request. The request then looks like this: + # + # get "/", :basic_auth => ['username', 'password'] + # + # [:params] Supply params for the request body, like e.g. form data: + # + # post "/books/new", :params => { :book => { :title => "Metaprogramming Ruby", :publisher => "O'Reilly" } } + # post "/trackings/add", :params => { :name => "Tommy" } + # module Fragment @fragments = [] @benchmark = nil class Fragment attr_accessor :benchmark attr_accessor :path attr_accessor :method - attr_accessor :params + attr_accessor :options attr_accessor :uri - def initialize benchmark, path, method, params = {} - @benchmark, @path, @method, @params = benchmark, path, method, params + def initialize benchmark, path, method, options = {} + @benchmark, @path, @method, @options, @request_options = benchmark, path, method, options, {} @uri = URI(@benchmark.uri.to_s + @path) + configure_options end # Initiates the request in the context of a <tt>Net::HTTP.start</tt> block - def run!(http) - request = case @method - when :get then Net::HTTP::Get.new(@uri.request_uri) - when :post then Net::HTTP::Post.new(@uri.request_uri) - when :put then Net::HTTP::Put.new(@uri.request_uri) - when :delete then Net::HTTP::Delete.new(@uri.request_uri) - else nil - end - - start = Time.now - response = http.request(request) - stop = Time.now - - track!(start, stop, response) - response + def run! + EventMachine::HttpRequest.new(@uri.to_s).send(@method, @request_options) end # Adds the current tracking result as a hash to the benchmark's tracker - def track!(start, stop, response) + def track!(start, stop, http) @benchmark.tracker.track( { :elapsed => (stop - benchmark.start).to_f, :start => start.to_f, :stop => stop.to_f, :duration => (stop - start).to_milliseconds, :benchmark => @benchmark.name, :url => @uri.to_s, :path => @uri.request_uri, :method => @method, - :status => response.code + :status => http.response_header.status } ) end + + private + + def configure_options + + # Basic Auth + @request_options[:head] = { 'authorization' => BigBench.config.basic_auth } unless BigBench.config.basic_auth.nil? + @request_options[:head] = { 'authorization' => @options[:basic_auth] } unless @options[:basic_auth].nil? + + # Body + @request_options[:body] = @options[:params] unless @options[:params].nil? + + end end # Performs a GET request to the given url, e.g. # # get "/some/page" # - def self.get(path) - @fragments << Fragment.new(@benchmark, path, :get, {}) + def self.get(path, options = {}) + @fragments << Fragment.new(@benchmark, path, :get, options) end # Performs a POST request to the given url, e.g. # - # post "/login", { :user => "some@sample.com", :password => "secret" } + # post "/login" # - def self.post(path, params = {}) - @fragments << Fragment.new(@benchmark, path, :post, params) + def self.post(path, options = {}) + @fragments << Fragment.new(@benchmark, path, :post, options) end # Performs a PUT request to the given url, e.g. # - # put "/books", { :book => "Some book content" } + # put "/books" # - def self.put(path, params = {}) - @fragments << Fragment.new(@benchmark, path, :put, params) + def self.put(path, options = {}) + @fragments << Fragment.new(@benchmark, path, :put, options) end # Performs a DELETE request to the given url, e.g. # - # delete "/books/5", { :user => "some@sample.com", :password => "secret" } + # delete "/books/5" # - def self.delete(path, params = {}) - @fragments << Fragment.new(@benchmark, path, :delete, params) + def self.delete(path, options = {}) + @fragments << Fragment.new(@benchmark, path, :delete, options) end # Evaluates a benchmark block full of puts and gets and returns an array of fully configured fragments for it def self.parse(benchmark, &block) reset! \ No newline at end of file