celluloid-benchmark 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a8b43cb7f1b3f835c28eef1361299b99b8188a1e
4
- data.tar.gz: 2025783e8cc49d76300263cc7df9ba991af36120
3
+ metadata.gz: 5d6ca6a307e6c743b6024e66193242e30daebf58
4
+ data.tar.gz: 13a15f647b8b7fb852f92930e6f16ed3d23019fb
5
5
  SHA512:
6
- metadata.gz: 1e1bf981e517e15d3c6698c42b7bd5bc388266203bc7a82f09e2824bef6c95d67fef09d0b213113577c6100528c0e7ed107b43c1b9993c65f0c79d65109ba3ad
7
- data.tar.gz: b970a27dba6d16cb152c486077cc879de84d6827444ddf968abbb9cd981a5b6c4a8f056510a6a774efd7bce91bbb36f8978b025fa897f9fdf0037ff0d6864eef
6
+ metadata.gz: fafc822aa34fbde4808ec16a08d3752621bdb5d8c10eb2333c78e2414a00b04a303a3981c14c129ce956f3e4cd12950f114fc0cba90d01792aa3f462536a5cf1
7
+ data.tar.gz: 03fd8a7cc021d5a1ef7f77c9abd647b752b762a5a7c8c98b6b1e3ac47e250263e71615e7c1c28e0dddd2d4a5f819d7b97c0597d400fc1c110d6fad0d2c2133e2
data/README.md CHANGED
@@ -5,7 +5,10 @@ for high concurrency. Use [Mechanize](http://mechanize.rubyforge.org) for a real
5
5
 
6
6
  Getting Started
7
7
  ===============
8
- celluloid-benchmark test/files/runner_test_session.rb
8
+ gem install celluloid-benchmark
9
+ echo "benchmark :home_page, 1
10
+ get 'https://github.com/scottwillson/celluloid-benchmark'" > session.rb
11
+ celluloid-benchmark session.rb 1
9
12
 
10
13
  Congrats! You just load-tested the project's Github page.
11
14
 
@@ -0,0 +1,50 @@
1
+ module CelluloidBenchmark
2
+ # List of responses for a benchmark defined in test scenario.
3
+ #
4
+ # For example, requests for /offers/1001, /offers/1001-burgerville-deal, /offers/2200
5
+ # might all be grouped under the "offer_show" label
6
+ #
7
+ # Call #ok? to check that responses were OK and fast enough.
8
+ class Benchmark
9
+ attr_reader :label
10
+ attr_reader :response_codes
11
+ attr_reader :response_times
12
+ attr_reader :threshold
13
+
14
+ def initialize(label, threshold, response_times, response_codes)
15
+ raise(ArgumentError, "label cannot be blank") if label.nil? || label == ""
16
+
17
+ @label = label
18
+ @response_times = response_times || []
19
+ @threshold = threshold || 3.0
20
+ @response_codes = response_codes || []
21
+
22
+ raise(ArgumentError, "threshold must be greater than zero") if self.threshold <= 0
23
+ end
24
+
25
+ def ok?
26
+ response_times_ok? && response_codes_ok?
27
+ end
28
+
29
+ # Consider average response time. Do not check for outlying slow times.
30
+ def response_times_ok?
31
+ if response_times.size > 0
32
+ average_response_time <= threshold
33
+ else
34
+ true
35
+ end
36
+ end
37
+
38
+ # 200 OK is ... OK, as is a redirect, not modified, or auth required
39
+ def response_codes_ok?
40
+ response_codes.all? { |code| code == 200 || code == 302 || code == 304 || code == 401 }
41
+ end
42
+
43
+
44
+ private
45
+
46
+ def average_response_time
47
+ response_times.reduce(:+) / response_times.size
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,82 @@
1
+ require "logger"
2
+
3
+ require "celluloid"
4
+ require_relative "benchmark"
5
+
6
+ module CelluloidBenchmark
7
+ # A test run of a scenario. Holds response times, codes, and requests. Reports results as an array of Benchmarks
8
+ class BenchmarkRun
9
+ include Celluloid
10
+
11
+ attr_accessor :ended_at
12
+ attr_accessor :logger
13
+ attr_accessor :started_at
14
+ attr_reader :thresholds
15
+
16
+ def initialize
17
+ if !Dir.exists?("log")
18
+ FileUtils.mkdir "log"
19
+ end
20
+
21
+ # Could replace with Celluloid.logger
22
+ @logger = ::Logger.new("log/benchmark.log")
23
+ @thresholds = Hash.new
24
+ end
25
+
26
+ def log(http_status_code, start_time, end_time, label, threshold)
27
+ time = end_time - start_time
28
+ response_times[label] << time
29
+ response_codes[label] << http_status_code.to_i
30
+
31
+ if threshold
32
+ thresholds[label] = threshold
33
+ end
34
+
35
+ logger.info "#{http_status_code} #{time} #{label}"
36
+ end
37
+
38
+ def response_times
39
+ @response_times ||= Hash.new { |hash, value| hash[value] = [] }
40
+ end
41
+
42
+ def response_codes
43
+ @response_codes ||= Hash.new { |hash, value| hash[value] = [] }
44
+ end
45
+
46
+ def requests
47
+ response_times.values.compact.map(&:size).reduce(&:+) || 0
48
+ end
49
+
50
+ def benchmarks
51
+ response_times.map do |label, response_times|
52
+ CelluloidBenchmark::Benchmark.new label, thresholds[label], response_times, response_codes[label]
53
+ end
54
+ end
55
+
56
+ def mark_start
57
+ @started_at = Time.now
58
+ end
59
+
60
+ def mark_end
61
+ @ended_at = Time.now
62
+ end
63
+
64
+ def elapsed_time
65
+ if started_at && ended_at
66
+ (ended_at - started_at).to_f
67
+ else
68
+ 0
69
+ end
70
+ end
71
+
72
+ def ok?
73
+ benchmarks.all?(&:ok?)
74
+ end
75
+
76
+ def inspect
77
+ response_times.map do |label, response_times|
78
+ "#{label} #{response_times.reduce(:+) / response_times.size} #{response_times.min} #{response_times.max} #{response_times.size}"
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,34 @@
1
+ require "celluloid"
2
+ require_relative "visitor_group"
3
+
4
+ Celluloid.logger = nil
5
+
6
+ module CelluloidBenchmark
7
+ # Run a scenario in several Visitors and return a BenchmarkRun
8
+ class Runner
9
+ def self.run(session_path, duration = 10)
10
+ raise("session_path is required") unless session_path
11
+ raise("'#{session_path}' does not exist") unless File.exists?(session_path)
12
+
13
+ visitor_group = VisitorGroup.run!
14
+ benchmark_run = Celluloid::Actor[:benchmark_run]
15
+
16
+ benchmark_run.mark_start
17
+ futures = run_sessions(session_path, benchmark_run, duration)
18
+ futures.map(&:value)
19
+ benchmark_run.mark_end
20
+
21
+ benchmark_run
22
+ end
23
+
24
+ def self.run_sessions(session_path, benchmark_run, duration)
25
+ session = File.read(session_path)
26
+ visitors = Celluloid::Actor[:visitor_pool]
27
+ futures = []
28
+ (visitors.size - 2).times do
29
+ futures << visitors.future.run_session(session, benchmark_run, duration)
30
+ end
31
+ futures
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module CelluloidBenchmark
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,79 @@
1
+ require "mechanize"
2
+
3
+ module CelluloidBenchmark
4
+ # Actor that models a person using a web browser. Runs a test scenario. Delegates web browsing to
5
+ # instance of a Mechanize Agent.
6
+ class Visitor
7
+ include Celluloid
8
+ extend Forwardable
9
+
10
+ def_delegators :@browser, :add_auth, :get, :submit, :transact
11
+
12
+ attr_reader :benchmark_run
13
+ attr_reader :browser
14
+ attr_accessor :current_request_label
15
+ attr_accessor :current_request_threshold
16
+ attr_accessor :request_start_time
17
+ attr_accessor :request_end_time
18
+
19
+ def initialize(browser = Mechanize.new)
20
+ @browser = browser
21
+
22
+ browser.pre_connect_hooks << proc do |agent, request|
23
+ self.request_start_time = Time.now
24
+ end
25
+
26
+ browser.post_connect_hooks << proc do |agent, uri, response, body|
27
+ self.request_end_time = Time.now
28
+ benchmark_run.async.log response.code, request_start_time, request_end_time, current_request_label, current_request_threshold
29
+ end
30
+ end
31
+
32
+ def run_session(session, benchmark_run, duration)
33
+ @benchmark_run = benchmark_run
34
+
35
+ elapsed_time = 0
36
+ started_at = Time.now
37
+ until elapsed_time >= duration
38
+ begin
39
+ instance_eval session
40
+ rescue Mechanize::ResponseCodeError => e
41
+ self.request_end_time = Time.now
42
+ benchmark_run.async.log(
43
+ e.response_code,
44
+ request_start_time,
45
+ request_end_time,
46
+ current_request_label,
47
+ current_request_threshold
48
+ )
49
+ end
50
+
51
+ elapsed_time = Time.now - started_at
52
+ end
53
+ elapsed_time
54
+ end
55
+
56
+ def benchmark(label, threshold = 0.5)
57
+ self.current_request_label = label
58
+ self.current_request_threshold = threshold
59
+ end
60
+
61
+ def random_data(key)
62
+ data_source(key).sample
63
+ end
64
+
65
+ def data_sources
66
+ @data_sources ||= Hash.new do |hash, key|
67
+ hash[key] = File.readlines("tmp/data/#{key}s.csv")
68
+ end
69
+ end
70
+
71
+ def data_sources=(hash)
72
+ @data_sources = hash
73
+ end
74
+
75
+ def data_source(key)
76
+ data_sources[key]
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,11 @@
1
+ require "celluloid"
2
+ require_relative "benchmark_run"
3
+ require_relative "visitor"
4
+
5
+ module CelluloidBenchmark
6
+ # Supervised Actor pool of Visitors
7
+ class VisitorGroup < Celluloid::SupervisionGroup
8
+ supervise BenchmarkRun, as: :benchmark_run
9
+ pool Visitor, as: :visitor_pool, size: 8
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: celluloid-benchmark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Willson
@@ -56,16 +56,12 @@ files:
56
56
  - README.md
57
57
  - bin/celluloid-benchmark
58
58
  - lib/celluloid_benchmark.rb
59
- - test/files/runner_test_session.rb
60
- - test/integration/config.ru
61
- - test/integration/html/404.html
62
- - test/integration/html/index.html
63
- - test/integration/integration_test.rb
64
- - test/integration/test_session.rb
65
- - test/unit/benchmark_run_test.rb
66
- - test/unit/benchmark_test.rb
67
- - test/unit/runner_test.rb
68
- - test/unit/visitor_test.rb
59
+ - lib/celluloid_benchmark/benchmark.rb
60
+ - lib/celluloid_benchmark/benchmark_run.rb
61
+ - lib/celluloid_benchmark/runner.rb
62
+ - lib/celluloid_benchmark/version.rb
63
+ - lib/celluloid_benchmark/visitor.rb
64
+ - lib/celluloid_benchmark/visitor_group.rb
69
65
  homepage: https://github.com/scottwillson/celluloid-benchmark
70
66
  licenses:
71
67
  - MIT
@@ -92,14 +88,5 @@ rubygems_version: 2.2.1
92
88
  signing_key:
93
89
  specification_version: 4
94
90
  summary: Pure Ruby, realistic, website load test tool
95
- test_files:
96
- - test/files/runner_test_session.rb
97
- - test/integration/config.ru
98
- - test/integration/integration_test.rb
99
- - test/integration/test_session.rb
100
- - test/unit/benchmark_run_test.rb
101
- - test/unit/benchmark_test.rb
102
- - test/unit/runner_test.rb
103
- - test/unit/visitor_test.rb
104
- - test/integration/html/404.html
105
- - test/integration/html/index.html
91
+ test_files: []
92
+
@@ -1,2 +0,0 @@
1
- benchmark :home_page, 1
2
- get "https://github.com/scottwillson/celluloid-benchmark"
@@ -1,9 +0,0 @@
1
- require 'rack/contrib/try_static'
2
- require 'rack/contrib/not_found'
3
-
4
- use Rack::TryStatic,
5
- :root => "test/integration/html",
6
- :urls => %w[/],
7
- :try => ['index.html', '/index.html']
8
-
9
- run Rack::NotFound.new('test/integration/html/404.html')
@@ -1,10 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>404</title>
5
- </head>
6
-
7
- <body>
8
- File not found
9
- </body>
10
- </html>
@@ -1,10 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>Test page</title>
5
- </head>
6
-
7
- <body>
8
- Body
9
- </body>
10
- </html>
@@ -1,55 +0,0 @@
1
- require "minitest/autorun"
2
- require_relative "../../lib/celluloid_benchmark"
3
-
4
- module CelluloidBenchmark
5
- class IntegrationTest < Minitest::Test
6
- def setup
7
- # minitest and Celluloid both use at_exit
8
- Celluloid.boot
9
-
10
- start_target_webserver
11
- end
12
-
13
- def teardown
14
- stop_target_webserver
15
- end
16
-
17
- def test_happy_path
18
- assert target_webserver_responsive?, "Test web server did not respond OK"
19
-
20
- session_path = File.expand_path(File.dirname(__FILE__) + "/test_session.rb")
21
- duration = 5
22
- benchmark_run = CelluloidBenchmark::Runner.run(session_path, duration)
23
-
24
- assert benchmark_run.ok?, "Run should be OK"
25
- end
26
-
27
- def start_target_webserver
28
- `thin --threaded --rackup test/integration/config.ru --daemonize --port 8000 start`
29
- end
30
-
31
- def stop_target_webserver
32
- `thin stop`
33
- end
34
-
35
- def target_webserver_responsive?
36
- require "net/http"
37
- require "uri"
38
-
39
- uri = URI.parse("http://localhost:8000/")
40
- http = Net::HTTP.new(uri.host, uri.port)
41
- Timeout::timeout(5) do
42
- request = Net::HTTP::Get.new(uri.request_uri)
43
- begin
44
- response = http.request(request)
45
- if response.code.to_i == 200
46
- return true
47
- end
48
- rescue Errno::ECONNREFUSED, Net::HTTP::Persistent::Error
49
- # Ignore. Server might be starting up.
50
- end
51
- sleep 1
52
- end
53
- end
54
- end
55
- end
@@ -1,2 +0,0 @@
1
- benchmark :home_page, 1
2
- get "http://localhost:8000/"
@@ -1,130 +0,0 @@
1
- require "minitest/autorun"
2
- require "timecop"
3
- require_relative "../../lib/celluloid_benchmark/benchmark_run"
4
-
5
- module CelluloidBenchmark
6
- class BenchmarkRunTest < Minitest::Test
7
- def setup
8
- # minitest and Celluloid both use at_exit
9
- Celluloid.boot
10
- end
11
-
12
- def test_new
13
- BenchmarkRun.new
14
- end
15
-
16
- def test_inspect
17
- BenchmarkRun.new.inspect
18
- end
19
-
20
- def test_log
21
- benchmark_run = BenchmarkRun.new
22
- logger = Minitest::Mock.new
23
- logger.expect :info, true, [ "200 1 search"]
24
- benchmark_run.logger = logger
25
- benchmark_run.log 200, 1, 2, "search", 3
26
- end
27
-
28
- def test_response_times
29
- benchmark_run = BenchmarkRun.new
30
- assert_equal Hash.new, benchmark_run.response_times
31
-
32
- benchmark_run.log 200, 3, 4, "search", 0
33
- assert_equal({ "search" => [ 1 ] }, benchmark_run.response_times)
34
-
35
- benchmark_run.log 200, 40, 100, "search", 3
36
- search_times = benchmark_run.response_times[ "search" ]
37
- assert_equal [ 1, 60 ], search_times.sort
38
-
39
- benchmark_run.log 404, 1000, 2000, "home", 3
40
- search_times = benchmark_run.response_times[ "search" ]
41
- assert_equal [ 1, 60 ], search_times.sort
42
- assert_equal [ 1000 ], benchmark_run.response_times[ "home" ]
43
- end
44
-
45
- def test_response_codes
46
- benchmark_run = BenchmarkRun.new
47
- assert_equal Hash.new, benchmark_run.response_codes
48
-
49
- benchmark_run.log 200, 3, 4, "search", 0
50
- assert_equal({ "search" => [ 200 ] }, benchmark_run.response_codes)
51
-
52
- benchmark_run.log 302, 40, 100, "search", 3
53
- search_codes = benchmark_run.response_codes[ "search" ]
54
- assert_equal [ 200, 302 ], search_codes.sort
55
-
56
- benchmark_run.log 404, 1000, 2000, "home", 3
57
- search_codes = benchmark_run.response_codes[ "search" ]
58
- assert_equal [ 200, 302 ], search_codes.sort
59
- assert_equal [ 404 ], benchmark_run.response_codes[ "home" ]
60
- end
61
-
62
- def test_requests
63
- benchmark_run = BenchmarkRun.new
64
- assert_equal 0, benchmark_run.requests
65
-
66
- benchmark_run.log 200, 3, 4, "search", 0
67
- assert_equal 1, benchmark_run.requests
68
-
69
- benchmark_run.log 200, 3, 4, "search", 0
70
- assert_equal 2, benchmark_run.requests
71
- end
72
-
73
- def test_benchmarks
74
- benchmark_run = BenchmarkRun.new
75
- assert_equal 0, benchmark_run.benchmarks.size
76
- assert benchmark_run.benchmarks.empty?
77
-
78
- benchmark_run.log 200, 3, 4, "search", 0.01
79
- assert_equal 1, benchmark_run.benchmarks.size
80
- benchmark = benchmark_run.benchmarks.first
81
- assert_equal "search", benchmark.label, "benchmark label"
82
- assert_equal 0.01, benchmark.threshold, "benchmark threshold"
83
- assert_equal [ 1 ], benchmark.response_times, "benchmark response_times"
84
- assert_equal [ 200 ], benchmark.response_codes, "benchmark response_codes"
85
- end
86
-
87
- def test_mark_start
88
- benchmark_run = BenchmarkRun.new
89
- assert_equal nil, benchmark_run.started_at, "started_at"
90
-
91
- Timecop.freeze(Time.new(2001, 4, 5, 18, 30)) do
92
- benchmark_run.mark_start
93
- end
94
-
95
- assert_equal Time.new(2001, 4, 5, 18, 30), benchmark_run.started_at
96
- end
97
-
98
- def test_mark_end
99
- benchmark_run = BenchmarkRun.new
100
- assert_equal nil, benchmark_run.ended_at, "ended_at"
101
-
102
- Timecop.freeze(Time.new(2001, 4, 5, 18, 30)) do
103
- benchmark_run.mark_end
104
- end
105
-
106
- assert_equal Time.new(2001, 4, 5, 18, 30), benchmark_run.ended_at
107
- end
108
-
109
- def test_ok
110
- benchmark_run = BenchmarkRun.new
111
- assert benchmark_run.ok?
112
-
113
- benchmark_run.stub :benchmarks, [ Minitest::Mock.new.expect(:ok?, true) ] do
114
- assert benchmark_run.ok?
115
- end
116
-
117
- benchmark_run.stub :benchmarks, [ Minitest::Mock.new.expect(:ok?, true), Minitest::Mock.new.expect(:ok?, true) ] do
118
- assert benchmark_run.ok?
119
- end
120
-
121
- benchmark_run.stub :benchmarks, [ Minitest::Mock.new.expect(:ok?, false), Minitest::Mock.new.expect(:ok?, true) ] do
122
- assert !benchmark_run.ok?
123
- end
124
-
125
- benchmark_run.stub :benchmarks, [ Minitest::Mock.new.expect(:ok?, false), Minitest::Mock.new.expect(:ok?, false) ] do
126
- assert !benchmark_run.ok?
127
- end
128
- end
129
- end
130
- end
@@ -1,107 +0,0 @@
1
- require "minitest/autorun"
2
- require_relative "../../lib/celluloid_benchmark/benchmark"
3
-
4
- module CelluloidBenchmark
5
- class BenchmarkTest < Minitest::Test
6
- def test_require_label
7
- assert_raises ArgumentError do
8
- Benchmark.new(nil, 1, [], [])
9
- end
10
- end
11
-
12
- def test_require_threshold_above_zero
13
- assert_raises ArgumentError do
14
- Benchmark.new("homepage", 0, nil, nil)
15
- end
16
- end
17
-
18
- def test_defaults
19
- benchmark = Benchmark.new("homepage", nil, nil, nil)
20
-
21
- assert_equal 3, benchmark.threshold, "default threshold"
22
-
23
- assert !benchmark.response_codes.nil?, "response_codes default"
24
- assert benchmark.response_codes.empty?, "response_codes default"
25
-
26
- assert !benchmark.response_times.nil?, "response_times default"
27
- assert benchmark.response_times.empty?, "response_times default"
28
- end
29
-
30
- def test_response_times_ok_for_empty_benchmark
31
- benchmark = Benchmark.new("homepage", nil, nil, nil)
32
- assert benchmark.ok?, "Empty Benchmark should be OK"
33
- end
34
-
35
- def test_ok_if_response_codes_and_times_ok
36
- benchmark = Benchmark.new("homepage", nil, nil, nil)
37
-
38
- benchmark.stub(:response_times_ok?, true) do
39
- benchmark.stub(:response_codes_ok?, true) do
40
- assert benchmark.ok?, "Benchmark.ok? times OK and codes OK"
41
- end
42
- end
43
-
44
- benchmark.stub(:response_times_ok?, false) do
45
- benchmark.stub(:response_codes_ok?, true) do
46
- assert !benchmark.ok?, "Benchmark.ok? times not OK and codes OK"
47
- end
48
- end
49
-
50
- benchmark.stub(:response_times_ok?, true) do
51
- benchmark.stub(:response_codes_ok?, false) do
52
- assert !benchmark.ok?, "Benchmark.ok? times OK and codes not OK"
53
- end
54
- end
55
-
56
- benchmark.stub(:response_times_ok?, false) do
57
- benchmark.stub(:response_codes_ok?, false) do
58
- assert !benchmark.ok?, "Benchmark.ok? times not OK and codes not OK"
59
- end
60
- end
61
- end
62
-
63
- def test_blank_response_time_ok
64
- benchmark = Benchmark.new("homepage", nil, nil, nil)
65
- assert benchmark.response_times_ok?
66
- end
67
-
68
- def test_blank_response_codes_ok
69
- benchmark = Benchmark.new("homepage", nil, nil, nil)
70
- assert benchmark.response_codes_ok?
71
- end
72
-
73
- def test_response_times
74
- benchmark = Benchmark.new("test", 1, [ 0.99, 0.9, 1.01 ], nil)
75
- assert benchmark.response_times_ok?
76
-
77
- benchmark = Benchmark.new("test", 1, [ 1.000001, 1, 1 ], nil)
78
- assert !benchmark.response_times_ok?
79
- end
80
-
81
- def test_response_codes
82
- benchmark = Benchmark.new("test", nil, [], [])
83
- assert benchmark.response_codes_ok?
84
-
85
- benchmark = Benchmark.new("test", nil, [], [ 500 ])
86
- assert !benchmark.response_codes_ok?
87
-
88
- benchmark = Benchmark.new("test", nil, [], [ 200 ])
89
- assert benchmark.response_codes_ok?
90
-
91
- benchmark = Benchmark.new("test", nil, [], [ 200, 500 ])
92
- assert !benchmark.response_codes_ok?
93
-
94
- benchmark = Benchmark.new("test", nil, [], [ 200, 302, 304, 401 ])
95
- assert benchmark.response_codes_ok?
96
-
97
- benchmark = Benchmark.new("test", nil, [], [ 403, 200, 302, 401 ])
98
- assert !benchmark.response_codes_ok?
99
-
100
- benchmark = Benchmark.new("test", nil, [], [ 410, 200, 302, 401 ])
101
- assert !benchmark.response_codes_ok?
102
-
103
- benchmark = Benchmark.new("test", nil, [], [ 309, 200, 302, 401 ])
104
- assert !benchmark.response_codes_ok?
105
- end
106
- end
107
- end
@@ -1,24 +0,0 @@
1
- require "minitest/autorun"
2
- require_relative "../../lib/celluloid_benchmark/runner"
3
- require "fakeweb"
4
-
5
- FakeWeb.allow_net_connect = false
6
-
7
- module CelluloidBenchmark
8
- class BenchmarkRunnerTest < Minitest::Test
9
- def setup
10
- # minitest and Celluloid both use at_exit
11
- Celluloid.boot
12
- end
13
-
14
- def test_run
15
- FakeWeb.register_uri(:get, "https://github.com/scottwillson/celluloid-benchmark", :body => "<html>OK</html>")
16
- benchmark_run = Runner.run(File.dirname(__FILE__) + "/../files/runner_test_session.rb", 0.1)
17
-
18
- benchmarks = benchmark_run.benchmarks
19
- assert_equal 1, benchmarks.size
20
- benchmark = benchmarks.first
21
- assert benchmark.ok?, "benchmark.ok?"
22
- end
23
- end
24
- end
@@ -1,64 +0,0 @@
1
- require "minitest/autorun"
2
- require "celluloid"
3
- require_relative "../../lib/celluloid_benchmark/visitor"
4
-
5
- module CelluloidBenchmark
6
- # Visitor#run_session is the central purpose of this gem, but best tested in an integration test,
7
- # not a unit test
8
- class VisitorTest < Minitest::Test
9
- class MockBrowser
10
- attr_accessor :post_connect_hooks, :pre_connect_hooks, :uris
11
-
12
- def initialize
13
- @pre_connect_hooks = []
14
- @post_connect_hooks = []
15
- @uris = []
16
- end
17
-
18
- def get(uri)
19
- uris << uri
20
- end
21
- end
22
-
23
- def setup
24
- # minitest and Celluloid both use at_exit
25
- Celluloid.boot
26
- end
27
-
28
- def test_run_session
29
- browser = MockBrowser.new
30
- visitor = Visitor.new(browser)
31
- session = File.read(File.dirname(__FILE__) + "/../files/runner_test_session.rb")
32
-
33
- elapsed_time = visitor.run_session(session, nil, 0.01)
34
-
35
- assert !elapsed_time.nil?, "elapsed_time should not be nil"
36
- assert elapsed_time > 0, "elapsed_time should be greater than zero, but was #{elapsed_time}"
37
- end
38
-
39
- def test_benchmark
40
- browser = MockBrowser.new
41
- visitor = Visitor.new(browser)
42
-
43
- visitor.benchmark("purchase_page", 0.25)
44
-
45
- assert_equal "purchase_page", visitor.current_request_label, "current_request_label"
46
- assert_equal 0.25, visitor.current_request_threshold, "current_request_threshold"
47
- end
48
-
49
- def test_random_data
50
- browser = MockBrowser.new
51
- visitor = Visitor.new(browser)
52
-
53
- data_sources = Minitest::Mock.new
54
- data_source = Minitest::Mock.new
55
-
56
- data_source.expect :sample, 3
57
- data_sources.expect :[], data_source, [ String ]
58
-
59
- visitor.data_sources = data_sources
60
-
61
- assert_equal 3, visitor.random_data("ids")
62
- end
63
- end
64
- end