Sha256: a5ffde75f4de4498eb984f718501b5285ae89707b95a926a871f576f80d57ce8

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

#!ruby -I ./lib -I lib
require 'nyny'
require 'ruby-prof'
require 'benchmark'
require 'sinatra'
include Benchmark

set :run, false #do not run sinatra's builtin web server

def build_apps &block
  sinatra = Class.new(Sinatra::Base, &block).new
  nyny    = Class.new(NYNY::App, &block).new
  return [nyny, sinatra].map {|app| Rack::MockRequest.new(app) }
end

def run_test name, apps, &block
  nyny, sinatra = apps
  prc = Proc.new(&block)

  puts "\nTest: #{name}"
  Benchmark.benchmark(CAPTION, 7, FORMAT, "> NYNY/Sinatra:") do |x|
    nyny_time     = x.report("nyny:   ")  { 1000.times { prc.call(nyny) } }
    sinatra_time  = x.report("sinatra:")  { 1000.times { prc.call(sinatra) } }
    puts "NYNY is #{"%.2f" % [sinatra_time.real/nyny_time.real]}x faster than Sinatra in this test"
  end
end

puts "Comparing NYNY #{NYNY::VERSION} with Sinatra #{Sinatra::VERSION}"

#
# Hello World
apps = build_apps do
  get '/' do
    'Hello World'
  end
end
run_test 'hello world', apps do |app|
  app.get '/'
end

#
# Filters
apps = build_apps do
  before do
    request
  end

  after do
    response
  end

  get '/' do
    'Hello World!'
  end
end
run_test 'filters', apps do |app|
  app.get '/'
end

#
# Helpers
apps = build_apps do
  helpers do
    def da_request
      request
    end
  end

  get '/' do
    da_request
  end
end
run_test 'helpers', apps do |app|
  app.get '/'
end

#
# Url patterns
apps = build_apps do
  get '/:name' do
    params[:name]
  end
end
run_test 'Url patterns', apps do |app|
  app.get '/foo'
end

# Plain routes
apps = build_apps do
  [:get, :post, :put].each do |method|
    10.times do |i|
      send(method, "/foo/#{i}") do
        i
      end
    end
  end
end
run_test 'A lot o Plain routes', apps do |app|
  app.get '/foo/5'
end

#
# Pattern routes
apps = build_apps do
  [:get, :post, :put].each do |method|
    10.times do |i|
      send(method, "/foo/#{i}/:action") do
        params[:action]
      end
    end
  end
end
run_test 'A lot of Pattern routes', apps do |app|
  app.get '/foo/5/edit'
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nyny-3.0.1 benchmark.rb