visitbench 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+ *.swo
4
+ *.swp
5
+ ## TEXTMATE
6
+ *.tmproj
7
+ tmtags
8
+
9
+ ## EMACS
10
+ *~
11
+ \#*
12
+ .\#*
13
+
14
+ ## VIM
15
+ *.swp
16
+
17
+ ## PROJECT::GENERAL
18
+ coverage
19
+ rdoc
20
+ pkg
21
+
22
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jaap van der Meer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = visitbench
2
+
3
+ Visitbench stresstests your server by simulating real user sessions.
4
+
5
+
6
+ == Note on Patches/Pull Requests
7
+
8
+ * Fork the project.
9
+ * Make your feature addition or bug fix.
10
+ * Add tests for it. This is important so I don't break it in a
11
+ future version unintentionally.
12
+ * Commit, do not mess with rakefile, version, or history.
13
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2010 Jaap van der Meer. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "visitbench"
8
+ gem.summary = %Q{Visitbench stress tests your server by manipulating real user sessions}
9
+ gem.description = %Q{}
10
+ gem.email = "jaapvandermeer@gmail.com"
11
+ gem.homepage = "http://github.com/japetheape/visitbench"
12
+ gem.authors = ["Jaap van der Meer"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency "mechanize"
15
+ gem.bindir = 'bin'
16
+ gem.executables = ["visitbench"]
17
+
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
23
+ end
24
+
25
+ require 'spec/rake/spectask'
26
+ Spec::Rake::SpecTask.new(:spec) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.spec_files = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ task :spec => :check_dependencies
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "visitbench #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/bin/visitbench ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'visit_bench')
3
+
4
+ if ARGV.empty?
5
+ files_to_run = Dir["visitbench_tests/*.rb"]
6
+ else
7
+ files_to_run = ARGV
8
+ end
9
+
10
+
11
+ if files_to_run.empty?
12
+ puts "No tests to run."
13
+ puts "Invoke visitbench with files to run or place them in ./visitbench_tests."
14
+ end
15
+
16
+
17
+ files_to_run.each { |file|
18
+ 100.times { print "-" }
19
+ puts ""
20
+ puts "Running: " + file
21
+ 100.times { print "-" }
22
+ puts ""
23
+ require file
24
+ }
25
+
@@ -0,0 +1,25 @@
1
+ require File.join(File.dirname(__FILE__), '../lib/visitbench.rb')
2
+
3
+ host = "http://example.com"
4
+
5
+ visit_bench = VisitBench.new(:host => host) do |vb|
6
+ vb.add_session do |session|
7
+ session.add_request '/', :time_on_page => 3
8
+ session.add_request '/somepage', :time_on_page => 2
9
+ session.add_request '/otherpage'
10
+ session.add_request '/otherpage', :method => :post, :params => 100
11
+ end
12
+ end
13
+
14
+ benchmark = performance_engine.run(:amt_times => 2, :concurrent_users => 3)
15
+ puts "Done benchmarking."
16
+ puts ""
17
+ puts ""
18
+ 100.times { print "-"}
19
+ puts ""
20
+ puts "Performance Report"
21
+ 100.times { print "-"}
22
+ puts ""
23
+
24
+ puts Report.new(benchmark)
25
+
@@ -0,0 +1,27 @@
1
+ # Add methods to Enumerable, which makes them available to Array
2
+ module Enumerable
3
+
4
+ # sum of an array of numbers
5
+ def sum
6
+ return self.inject(0){|acc,i|acc +i}
7
+ end
8
+
9
+ # average of an array of numbers
10
+ def average
11
+ return self.sum/self.length.to_f
12
+ end
13
+
14
+ # variance of an array of numbers
15
+ def sample_variance
16
+ avg=self.average
17
+ sum=self.inject(0){|acc,i|acc +(i-avg)**2}
18
+ return(1/self.length.to_f*sum)
19
+ end
20
+
21
+ # standard deviation of an array of numbers
22
+ def standard_deviation
23
+ return Math.sqrt(self.sample_variance)
24
+ end
25
+
26
+ end
27
+
data/lib/report.rb ADDED
@@ -0,0 +1,56 @@
1
+ require File.join(File.dirname(__FILE__), 'report.rb')
2
+
3
+ require File.join(File.dirname(__FILE__), 'enumeration.rb')
4
+
5
+ class Report
6
+ def initialize(run_times)
7
+ @run_times = run_times
8
+ end
9
+
10
+ def to_s
11
+ out = []
12
+ out << "Run times per user: "
13
+ out << "\t - values: " + run_times_per_user.inspect
14
+ out << "\t - averages per user: " + run_times_per_user.map(&:average).inspect
15
+ out << "\t - total average: " + run_times_per_user.flatten.average.to_s
16
+ out << ""
17
+ out << "Run times per request: "
18
+ out << "\t - values: "
19
+ run_times_per_request.each do |k,v|
20
+ out << "\t\t -" + k + " \t\t" + v.inspect
21
+ end
22
+ out << "\t - averages per request:"
23
+
24
+ run_times_per_request.each do |k,v|
25
+ out << "\t\t-" + k
26
+ out << "\t\t avg: " + v.average.to_s + " \tstd:" + v.standard_deviation.to_s
27
+ out << ""
28
+ end
29
+ out.join("\n")
30
+ end
31
+
32
+
33
+ def run_times_per_user
34
+ all = []
35
+ @run_times.each do |session|
36
+ all << session.map { |x| x[:time] }
37
+ end
38
+ all
39
+ end
40
+
41
+ def run_times_per_request
42
+ request_times = {}
43
+ @run_times.flatten.each do |rt|
44
+ key = rt[:request].url + "(" + rt[:request].method.to_s + ")"
45
+ request_times[key] ||= []
46
+ request_times[key] << rt[:time]
47
+ end
48
+ request_times
49
+ end
50
+
51
+
52
+
53
+
54
+ end
55
+
56
+
data/lib/request.rb ADDED
@@ -0,0 +1,51 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'mechanize'
4
+ class WWW::Mechanize
5
+ def set_headers(uri, request, cur_page)
6
+ super
7
+ raise "JO"
8
+ request.add_field('Foo', 'bar')
9
+ end
10
+ end
11
+
12
+
13
+ class Request
14
+ attr_reader :time_on_page, :url, :method
15
+
16
+
17
+ def initialize(url, options = {})
18
+ @time_on_page = options[:time_on_page] || 4
19
+ @url = (options[:host] || "") + url
20
+ @method = options[:method] || :get
21
+ @params = options[:params] || {}
22
+ @browser = options[:browser] #|| Mechanize.new
23
+ end
24
+
25
+
26
+ def do_request!
27
+ uri = URI.parse(@url)
28
+ if @method == :post
29
+ Net::HTTP.post_form uri, @params
30
+ else
31
+ #@browser.get(:url => @url, :headers => {'Authorization' => 'Basic ZGV2X3RlYW06RW5lcmd5Mi4w'})
32
+ @browser.get(@url)
33
+ #http = Net::HTTP.new("testing.et-model.com")
34
+
35
+
36
+ #@req = req = Net::HTTP::Get.new(
37
+ #Net::HTTP.get_print uri
38
+ #req = Net::HTTP::Get.new(uri)
39
+ #req.basic_auth "dev_team", "Energy2.0"
40
+ #response = http.request(req)
41
+ #puts response.body
42
+
43
+ end
44
+
45
+ end
46
+
47
+
48
+
49
+ end
50
+
51
+
data/lib/session.rb ADDED
@@ -0,0 +1,30 @@
1
+ # Simulation of a user that browses a site
2
+ class Session
3
+
4
+ # requests is an array of requests that will be run
5
+ def initialize(options = {})
6
+ @requests = []
7
+ @options = options
8
+ end
9
+
10
+ def add_request(*args)
11
+ args[1] ||= {}
12
+ args[1].merge!(@options[:request_options] || {})
13
+ @requests << Request.new(*args)
14
+ end
15
+
16
+ def run
17
+ puts "Started session."
18
+ run_times = []
19
+ @requests.each do |r|
20
+ puts "Doing request to %s with a time on page of %0.02f..." % [r.url, r.time_on_page]
21
+ run_times << { :request => r, :time => VisitBench.benchmark { r.do_request! } }
22
+ sleep r.time_on_page
23
+ end
24
+ return run_times
25
+ end
26
+
27
+
28
+
29
+
30
+ end
@@ -0,0 +1,61 @@
1
+ require File.join(File.dirname(__FILE__), 'request.rb')
2
+ require File.join(File.dirname(__FILE__), 'session.rb')
3
+ require File.join(File.dirname(__FILE__), 'report.rb')
4
+ require "rubygems"
5
+ require "mechanize"
6
+ require "logger"
7
+
8
+ # Visitbench stresstests your server
9
+ # by simulation real user sessions.
10
+ class VisitBench
11
+ def initialize(options = {})
12
+ @host = options[:host] || 'localhost'
13
+ @browser = Mechanize.new
14
+ @browser.log = Logger.new(STDOUT) if options[:debug]
15
+ options[:headers] ||= []
16
+ options[:headers].each do |k,v|
17
+ @browser.request_headers[k] = v
18
+ end
19
+
20
+ @browser.basic_auth(options[:username], options[:password]) if options[:username]
21
+ @sessions = options[:sessions] || []
22
+ yield self
23
+ end
24
+
25
+ # add a session with preinitialized values
26
+ def add_session
27
+ s = Session.new(:request_options => {:host => @host, :browser => @browser})
28
+ yield s
29
+ @sessions << s
30
+ end
31
+
32
+ # run the sessions multiple times and run them in parallel
33
+ def run(options = {})
34
+ amt_times = options[:amt_times] || 10
35
+ concurrent_users = options[:concurrent_users] || 3
36
+ run_times = []
37
+ amt_times.times do |i|
38
+ trs = []
39
+ concurrent_users.times do
40
+ puts "Starting thread"
41
+ trs << Thread.new do
42
+ session = @sessions[(@sessions.length - 1) * rand]
43
+ run_times << session.run
44
+ end
45
+ end
46
+ trs.each {|x| x.join }
47
+ end
48
+ run_times
49
+ end
50
+
51
+
52
+ class << self
53
+ def benchmark(&block)
54
+ begin_time = Time.now
55
+ yield
56
+ return Time.now - begin_time
57
+ end
58
+ end
59
+
60
+
61
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'visitbench'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Visitbench" do
4
+ it "fails" do
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ namespace :visitbench do
2
+ task :run do
3
+
4
+ end
5
+ end
6
+
7
+
8
+
9
+
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{visitbench}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jaap van der Meer"]
12
+ s.date = %q{2010-09-23}
13
+ s.default_executable = %q{visitbench}
14
+ s.description = %q{}
15
+ s.email = %q{jaapvandermeer@gmail.com}
16
+ s.executables = ["visitbench"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/visitbench",
29
+ "examples/request_example.rb",
30
+ "lib/enumeration.rb",
31
+ "lib/report.rb",
32
+ "lib/request.rb",
33
+ "lib/session.rb",
34
+ "lib/visit_bench.rb",
35
+ "spec/spec.opts",
36
+ "spec/spec_helper.rb",
37
+ "spec/visitbench_spec.rb",
38
+ "tasks/visit_bench.rake",
39
+ "visitbench.gemspec"
40
+ ]
41
+ s.homepage = %q{http://github.com/japetheape/visitbench}
42
+ s.rdoc_options = ["--charset=UTF-8"]
43
+ s.require_paths = ["lib"]
44
+ s.rubygems_version = %q{1.3.7}
45
+ s.summary = %q{Visitbench stress tests your server by manipulating real user sessions}
46
+ s.test_files = [
47
+ "spec/spec_helper.rb",
48
+ "spec/visitbench_spec.rb",
49
+ "examples/request_example.rb"
50
+ ]
51
+
52
+ if s.respond_to? :specification_version then
53
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
57
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
58
+ s.add_runtime_dependency(%q<mechanize>, [">= 0"])
59
+ else
60
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
61
+ s.add_dependency(%q<mechanize>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
65
+ s.add_dependency(%q<mechanize>, [">= 0"])
66
+ end
67
+ end
68
+
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: visitbench
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Jaap van der Meer
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-23 00:00:00 +02:00
19
+ default_executable: visitbench
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: mechanize
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ description: ""
52
+ email: jaapvandermeer@gmail.com
53
+ executables:
54
+ - visitbench
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.rdoc
60
+ files:
61
+ - .document
62
+ - .gitignore
63
+ - LICENSE
64
+ - README.rdoc
65
+ - Rakefile
66
+ - VERSION
67
+ - bin/visitbench
68
+ - examples/request_example.rb
69
+ - lib/enumeration.rb
70
+ - lib/report.rb
71
+ - lib/request.rb
72
+ - lib/session.rb
73
+ - lib/visit_bench.rb
74
+ - spec/spec.opts
75
+ - spec/spec_helper.rb
76
+ - spec/visitbench_spec.rb
77
+ - tasks/visit_bench.rake
78
+ - visitbench.gemspec
79
+ has_rdoc: true
80
+ homepage: http://github.com/japetheape/visitbench
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options:
85
+ - --charset=UTF-8
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ requirements: []
107
+
108
+ rubyforge_project:
109
+ rubygems_version: 1.3.7
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: Visitbench stress tests your server by manipulating real user sessions
113
+ test_files:
114
+ - spec/spec_helper.rb
115
+ - spec/visitbench_spec.rb
116
+ - examples/request_example.rb