Skip to content

Commit 93d67c9

Browse files
committed
Add gems and tests to test performance
Add ruby-prof gem, benchmark-ips for performance testing Add 2 tests, create and index. Run only 1 at a time for testing. Created a script that runs tests with Benchmark.ips so I can tell if changes I'm making are having performance improvements.
1 parent 3e20bc7 commit 93d67c9

File tree

6 files changed

+77
-3
lines changed

6 files changed

+77
-3
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ group :test do
4848
gem 'ruby-prof'
4949
gem 'mocha'
5050
gem 'capybara'
51+
gem 'benchmark-ips'
5152
end

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ GEM
5959
remote: https://rubygems.org/
6060
specs:
6161
arel (6.0.0)
62+
benchmark-ips (2.1.0)
6263
binding_of_caller (0.7.2)
6364
debug_inspector (>= 0.0.1)
6465
builder (3.2.2)
@@ -168,6 +169,7 @@ PLATFORMS
168169
ruby
169170

170171
DEPENDENCIES
172+
benchmark-ips
171173
byebug
172174
capybara
173175
coffee-rails (~> 4.1.0)

app/controllers/documents_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
class DocumentsController < ApplicationController
2+
def index
3+
@documents = Document.all
4+
render text: "yeehaw"
5+
end
6+
27
def show
3-
fresh_when etag: @recording
8+
@document = Document.find(params[:id])
49
end
510

611
def new

test/controllers/documents_controller_test.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@
22

33
class DocumentsControllerTest < ActionController::TestCase
44
test "creating a new document" do
5-
post :create, document: { title: "New things", content: "Doing them" }
5+
result = RubyProf.profile do
6+
post :create, document: { title: "New things", content: "Doing them" }
7+
end
8+
printer = RubyProf::GraphPrinter.new(result)
9+
printer.print(STDOUT, {})
10+
#post :create, document: { title: "New things", content: "Doing them" }
611

712
document = Document.last
813
assert_equal 'New things', document.title
914
assert_equal 'Doing them', document.content
1015
end
16+
17+
test "index" do
18+
result = RubyProf.profile do
19+
get :index
20+
end
21+
printer = RubyProf::GraphPrinter.new(result)
22+
printer.print(STDOUT, {})
23+
24+
assert_equal 200, response.status
25+
end
1126
end

test/controllers/documents_integration_test.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@
22

33
class DocumentsIntegrationTest < ActionDispatch::IntegrationTest
44
test "creating a new document" do
5-
post documents_url, document: { title: "New things", content: "Doing them" }
5+
result = RubyProf.profile do
6+
post documents_url, document: { title: "New things", content: "Doing them" }
7+
end
8+
printer = RubyProf::GraphPrinter.new(result)
9+
printer.print(STDOUT, {})
610

711
document = Document.last
812
assert_equal 'New things', document.title
913
assert_equal 'Doing them', document.content
1014
end
15+
16+
test "index" do
17+
result = RubyProf.profile do
18+
get documents_url
19+
end
20+
printer = RubyProf::GraphPrinter.new(result)
21+
printer.print(STDOUT, {})
22+
23+
assert_equal 200, response.status
24+
end
1125
end

test/controllers/documents_test.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'test_helper'
2+
require 'benchmark/ips'
3+
4+
class DocumentsControllerTest < ActionController::TestCase
5+
test "index" do
6+
get :index
7+
assert_equal 200, response.status
8+
end
9+
end
10+
11+
class DocumentsIntegrationTest < ActionDispatch::IntegrationTest
12+
test "index" do
13+
get documents_url
14+
assert_equal 200, response.status
15+
end
16+
17+
test "index other" do
18+
get 'http://www.example.com/documents'
19+
assert_equal 200, response.status
20+
end
21+
end
22+
23+
Benchmark.ips(5) do |bm|
24+
bm.report 'Integration Test' do
25+
Minitest.run_one_method(DocumentsIntegrationTest, 'test_index')
26+
end
27+
28+
bm.report 'Integration Test 2' do
29+
Minitest.run_one_method(DocumentsIntegrationTest, 'test_index_other')
30+
end
31+
32+
bm.report 'Functional Test' do
33+
Minitest.run_one_method(DocumentsControllerTest, 'test_index')
34+
end
35+
36+
bm.compare!
37+
end

0 commit comments

Comments
 (0)