Skip to content

Commit

Permalink
fixed a bug in url regexp and added a test suite to cover it, also ad…
Browse files Browse the repository at this point in the history
…ded a test suite for --follow-links option, changed the way options are parsed - now it's possible to specify both '-n100 -c20' and '-n 100 -c 25', releasing 0.0.3
  • Loading branch information
Dmitrii Samoilov committed Apr 12, 2011
1 parent c1d64a0 commit 39ccb2e
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 31 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,3 +1,5 @@
v0.0.3 fixed a bug in url regexp and added a test suite to cover it, also added a test suite for --follow-links option, changed the way options are parsed - now it's possible to specify both "-n100 -c20" and "-n 100 -c 25"

v0.0.2 deleted trollop gem as a requirement, added nokogiri dependency to gemspec, added MIT license file, fixed small issue with benchmarked site's url, added wrong site address handling [06-04-2011]

v0.0.1. first release [05-04-2011]
8 changes: 8 additions & 0 deletions Manifest
@@ -1,4 +1,5 @@
CHANGELOG
Gemfile
LICENSE
Manifest
README
Expand All @@ -12,4 +13,11 @@ nethttp_ab.gemspec
test/nethttp_ab_test.rb
test/requests_queue_test.rb
test/resources/index.html
test/resources/links.html
test/resources/links1.html
test/resources/links2.html
test/resources/links3.html
test/resources/links4.html
test/resources/links5.html
test/simple_requests_queue_test.rb
test/url_regexp_test.rb
15 changes: 12 additions & 3 deletions README
@@ -1,8 +1,17 @@
Simple tool to benchmark sites (rpm, response time, etc)

SIMPLE USAGE
EXAMPLES:

3 concurrent threads and 100 total requests:
nethttp_ab -n 100 -c 3 http://www.yoursite.com
nethttp_ab -n100 -c3 http://www.yoursite.com

OR simulate user and follow all local links on the page:
OR simulate one user and follow all local links on the page:
nethttp_ab -f http://localhost:3000

simulate 3 users (all local links on the page will be visited once)
nethttp_ab --follow_links -c3 http://localhost:3000

Issue tracker: https://github.com/german/nethttp_ab/issues


Copyright © 2011 Dmitrii Samoilov, released under the MIT license
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -2,7 +2,7 @@ require 'rubygems'
require 'rake'
require 'echoe'

Echoe.new('nethttp_ab', '0.0.2') do |p|
Echoe.new('nethttp_ab', '0.0.3') do |p|
p.description = "Simple tool to test and benchmark sites"
p.url = "http://github.com/german/nethttp_ab"
p.author = "Dmitrii Samoilov"
Expand Down
25 changes: 12 additions & 13 deletions bin/nethttp_ab
Expand Up @@ -4,10 +4,13 @@ require File.dirname(File.expand_path(__FILE__)) + '/../lib/requester.rb'

# SIMPLE USAGE:
# 3 concurrent threads and 100 total requests
# nethttp_ab -n 100 -c 3 http://www.yoursite.com
# nethttp_ab -n100 -c3 http://www.yoursite.com
#
# OR simulate user and follow all local links on the page
# OR simulate one user and follow all local links on the page
# nethttp_ab -f http://localhost:3000
#
# simulate 3 users (all local links on the page will be visited once)
# nethttp_ab --follow_links -c3 http://localhost:3000

opts = {}

Expand All @@ -17,27 +20,23 @@ else
opts[:follow_links] = false
end

if ARGV.include?('-c')
opts[:concurrent_threads] = ARGV[ARGV.index('-c') + 1].to_i
ARGV.delete('-c')
ARGV.delete(opts[:concurrent_threads])
if ARGV.join.match(/-c\s*(\d*)/)
opts[:concurrent_threads] = $1.to_i
else
opts[:concurrent_threads] = 3
opts[:concurrent_threads] = 1
end

if ARGV.include?('-n')
opts[:requests] = ARGV[ARGV.index('-n') + 1].to_i
ARGV.delete('-n')
ARGV.delete(opts[:requests])
if ARGV.join.match(/-n\s*(\d*)/)
opts[:requests] = $1.to_i
else
opts[:requests] = 10
end

url_to_benchmark = "http://localhost:3000/"

# search for the url to perform benchmarking on
# search in ARGV for the url to perform benchmarking on
ARGV.each do |arg|
url_to_benchmark = arg if arg =~ /^(https?:\/\/)?([\w\.]+)\.([a-z]{2,6}\.?)(\/[\w\.]*)*\/?$/
url_to_benchmark = arg if arg =~ NethttpAb::Requester::URL_REGEXP
end

url_to_benchmark = "http://" + url_to_benchmark if url_to_benchmark !~ /^https?:\/\//
Expand Down
8 changes: 7 additions & 1 deletion lib/requester.rb
Expand Up @@ -12,6 +12,8 @@ class Requester
attr_accessor :successfull_requests
attr_accessor :failed_requests

URL_REGEXP = /^(https?:\/\/)?(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}/

def initialize
@response_length = 0
@total_time = 0.0
Expand Down Expand Up @@ -100,7 +102,7 @@ def start_threads
puts "The url you provided is wrong, please check is it really ssl encrypted"
exit
rescue Errno::ECONNREFUSED => e
puts "Connection error, please check your internet connection or make sure the server is running (it's local)"
puts "Connection error, please check your internet connection or make sure the server is responding"
exit
rescue SocketError => e
puts e.message
Expand All @@ -118,9 +120,13 @@ def start_threads
Net::HTTP::Get.new(@url.path)
end

# TODO
#req.add_field "User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16"

@total_time += Benchmark.realtime do
begin
response = http_opened_session.request(req)

@mutex.synchronize do
@response_length += response.body.length
@successfull_requests += 1
Expand Down
17 changes: 4 additions & 13 deletions nethttp_ab.gemspec
Expand Up @@ -2,33 +2,24 @@

Gem::Specification.new do |s|
s.name = %q{nethttp_ab}
s.version = "0.0.2"
s.version = "0.0.3"

s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["Dmitrii Samoilov"]
s.date = %q{2011-04-06}
s.date = %q{2011-04-12}
s.default_executable = %q{nethttp_ab}
s.description = %q{Simple tool to test and benchmark sites}
s.email = %q{germaninthetown@gmail.com}
s.executables = ["nethttp_ab"]
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "bin/nethttp_ab", "lib/net.rb", "lib/requester.rb", "lib/requests_queue.rb", "lib/simple_requests_queue.rb"]
s.files = ["CHANGELOG", "LICENSE", "Manifest", "README", "Rakefile", "bin/nethttp_ab", "lib/net.rb", "lib/requester.rb", "lib/requests_queue.rb", "lib/simple_requests_queue.rb", "nethttp_ab.gemspec", "test/nethttp_ab_test.rb", "test/requests_queue_test.rb", "test/resources/index.html", "test/simple_requests_queue_test.rb"]
s.files = ["CHANGELOG", "Gemfile", "LICENSE", "Manifest", "README", "Rakefile", "bin/nethttp_ab", "lib/net.rb", "lib/requester.rb", "lib/requests_queue.rb", "lib/simple_requests_queue.rb", "nethttp_ab.gemspec", "test/nethttp_ab_test.rb", "test/requests_queue_test.rb", "test/resources/index.html", "test/resources/links.html", "test/resources/links1.html", "test/resources/links2.html", "test/resources/links3.html", "test/resources/links4.html", "test/resources/links5.html", "test/simple_requests_queue_test.rb", "test/url_regexp_test.rb"]
s.homepage = %q{http://github.com/german/nethttp_ab}
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Nethttp_ab", "--main", "README"]
s.require_paths = ["lib"]
s.rubyforge_project = %q{nethttp_ab}
s.rubygems_version = %q{1.6.2}
s.summary = %q{Simple tool to test and benchmark sites}
s.test_files = ["test/nethttp_ab_test.rb", "test/requests_queue_test.rb", "test/simple_requests_queue_test.rb"]
s.test_files = ["test/url_regexp_test.rb", "test/nethttp_ab_test.rb", "test/requests_queue_test.rb", "test/simple_requests_queue_test.rb"]

s.add_dependency('nokogiri', '>= 1.4.2')

if s.respond_to? :specification_version then
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
else
end
else
end
end
21 changes: 21 additions & 0 deletions test/url_regexp_test.rb
@@ -0,0 +1,21 @@
require 'test/unit'

require File.dirname(File.expand_path(__FILE__)) + '/../lib/requester.rb'

class UrlRegexpTest < Test::Unit::TestCase
def setup
@fail_urls = %w{www./yandex.ru yandex.123ru germaninthetown@gmail.com abcde}

@correct_urls = %w{www.google.com google.com http://google.com http://mail.ya.ru mail.ya.ru/ mail.ya.ru/test http://www.my-site.org/articles/2011/04/12/123-nethttpab-is-great}
end

def test_url_regexp
@correct_urls.each do |correct_url|
assert(correct_url =~ NethttpAb::Requester::URL_REGEXP)
end

@fail_urls.each do |fail_url|
assert(fail_url !~ NethttpAb::Requester::URL_REGEXP)
end
end
end

0 comments on commit 39ccb2e

Please sign in to comment.