Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add basic benchmarks

  • Loading branch information...
commit c9cf3cfcc2e50f11c1e60e18028412c2c8ba0786 1 parent be22bfe
@jferris jferris authored
View
23 benchmarks/click.rb
@@ -0,0 +1,23 @@
+class ClickBenchmark
+ def app
+ Class.new(Sinatra::Base) do
+ get '/' do
+ <<-HTML
+ <html>
+ <body>
+ <div>
+ <a href="#">Click Me</a>
+ </div>
+ </body>
+ </html>
+ HTML
+ end
+ end
+ end
+
+ def run(session)
+ 500.times do
+ session.click_on 'Click Me'
+ end
+ end
+end
View
32 benchmarks/forms.rb
@@ -0,0 +1,32 @@
+class FormsBenchmark
+ def app
+ Class.new(Sinatra::Base) do
+ get '/' do
+ fields = (1..100).map { |index|
+ <<-HTML
+ <label for="field#{index}">Field #{index} Label</label>
+ <input type="text" name="field#{index}" id="field#{index}" />
+ HTML
+ }.join
+
+ <<-HTML
+ <form action="/" method="post">
+ #{fields}
+ <input type="submit" value="Submit" />
+ </form>
+ HTML
+ end
+
+ post '/' do
+ redirect '/'
+ end
+ end
+ end
+
+ def run(session)
+ (1..100).each do |index|
+ session.fill_in "Field #{index} Label", with: "Value #{index}"
+ end
+ session.click_on "Submit"
+ end
+end
View
44 benchmarks/run
@@ -0,0 +1,44 @@
+#!/usr/bin/env ruby
+
+require 'bundler/setup'
+require 'capybara'
+require 'capybara/webkit'
+require 'benchmark'
+require './spec/support/app_runner'
+
+Capybara.default_driver = :webkit
+
+class Runner
+ RUN_COUNT = 10
+
+ def initialize(name)
+ @name = name
+ Dir.glob('./benchmarks/*.rb').each { |file| require(file) }
+ @benchmark = Object.const_get("#{@name}Benchmark").new
+ AppRunner.app = @benchmark.app
+ @session = Capybara::Session.new(:webkit)
+ @session.driver
+ @runs = []
+ end
+
+ def run
+ RUN_COUNT.times do
+ @session.visit "#{AppRunner.app_host}"
+ @runs << Benchmark.realtime { @benchmark.run(@session) }
+ @session.reset!
+ end
+
+ average = @runs.inject { |total, run| total + run } / @runs.size
+ printf("Ran #{@name} with an average of %.02f seconds\n", average)
+ end
+
+ def self.before(&block)
+ end
+
+ def self.after(&block)
+ end
+
+ include AppRunner
+end
+
+Runner.new(ARGV[0]).run
View
8 spec/support/app_runner.rb
@@ -21,13 +21,13 @@ def self.reset
end
end
- def run_application(app)
+ def run_application(&body)
+ app = Class.new(ExampleApp, &body)
AppRunner.app = app
end
def driver_for_app(&body)
- app = Class.new(ExampleApp, &body)
- run_application app
+ run_application(&body)
build_driver
end
@@ -37,7 +37,7 @@ def driver_for_html(html)
end
def run_application_for_html(html)
- run_application lambda { |env|
+ AppRunner.app = lambda { |env|
[200, { 'Content-Type' => 'text/html', 'Content-Length' => html.size.to_s }, [html]]
}
end

0 comments on commit c9cf3cf

Please sign in to comment.
Something went wrong with that request. Please try again.