Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Click to add title Click to add subtitle

Slide 4

Slide 4 text

Why Windows?

Slide 5

Slide 5 text

Kent Beck!!!

Slide 6

Slide 6 text

W IN DO W S

Slide 7

Slide 7 text

No emoji

Slide 8

Slide 8 text

!"#$

Slide 9

Slide 9 text

Welcome To Rails Conf!!!

Slide 10

Slide 10 text

Aaron Patterson

Slide 11

Slide 11 text

@tenderlove

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Thanks!!!

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Ruby Central

Slide 17

Slide 17 text

@eileencodes

Slide 18

Slide 18 text

@_ko1

Slide 19

Slide 19 text

Thank You!!! <3

Slide 20

Slide 20 text

Cat photos

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Extreme Programming

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Last Year: Thought Leader in Training

Slide 27

Slide 27 text

I Graduated!!

Slide 28

Slide 28 text

Degree in Thought Leadering from Online University

Slide 29

Slide 29 text

Ruby

Slide 30

Slide 30 text

Rails

Slide 31

Slide 31 text

Technoligy

Slide 32

Slide 32 text

Rails

Slide 33

Slide 33 text

Computers

Slide 34

Slide 34 text

Rails

Slide 35

Slide 35 text

Synergy

Slide 36

Slide 36 text

Rails

Slide 37

Slide 37 text

Innovation

Slide 38

Slide 38 text

Rails

Slide 39

Slide 39 text

Future

Slide 40

Slide 40 text

Rails

Slide 41

Slide 41 text

Words

Slide 42

Slide 42 text

Trolling DHH

Slide 43

Slide 43 text

"TDD is dead"

Slide 44

Slide 44 text

SOA is dead

Slide 45

Slide 45 text

Turbo Links 3: RJS

Slide 46

Slide 46 text

I should announce something.

Slide 47

Slide 47 text

Backpack

Slide 48

Slide 48 text

Lightweight

Slide 49

Slide 49 text

Action Fanny Pack

Slide 50

Slide 50 text

Pink

Slide 51

Slide 51 text

Performance! vroom!!!

Slide 52

Slide 52 text

Rails 5?

Slide 53

Slide 53 text

Adequate Development

Slide 54

Slide 54 text

Automatic Parallel Testing

Slide 55

Slide 55 text

Data Base Test Process Test Process Test Process

Slide 56

Slide 56 text

Data Base Test Process Test Process Test Process Data Base Data Base Data Base

Slide 57

Slide 57 text

Automatic

Slide 58

Slide 58 text

Transparent

Slide 59

Slide 59 text

Caching Compiled Views

Slide 60

Slide 60 text

Today: lazy compile

Slide 61

Slide 61 text

Get lock Template Compiled? Compile Save Unlock Writes to memory

Slide 62

Slide 62 text

Threaded Servers: lock

Slide 63

Slide 63 text

Forking servers: waste memory

Slide 64

Slide 64 text

Challenges

Slide 65

Slide 65 text

Too many templates!

Slide 66

Slide 66 text

Rendering with Locals

Slide 67

Slide 67 text

Template source eval(" local_variable = variables_hash[:local_variable] # rest of the template ")

Slide 68

Slide 68 text

Integration Tests

Slide 69

Slide 69 text

Controller Test class ControllerTest < ActionController::TestCase test "index" do get :index assert_equal 200, response.status end end

Slide 70

Slide 70 text

Integration Test class IntegrationTest < ActionDispatch::IntegrationTest test "index" do get '/documents' assert_equal 200, response.status end end

Slide 71

Slide 71 text

Why do we write controller tests?

Slide 72

Slide 72 text

Because integration tests are slow?

Slide 73

Slide 73 text

If tests are slow, is the website slow?

Slide 74

Slide 74 text

Rails 5?: Fast Tests

Slide 75

Slide 75 text

Controller / Integration: What’s the difference?

Slide 76

Slide 76 text

Controller: no middleware

Slide 77

Slide 77 text

Integration: has middleware

Slide 78

Slide 78 text

Rails 5?: Delete controller tests *We won’t actually delete them

Slide 79

Slide 79 text

Why So Slow?

Slide 80

Slide 80 text

How slow?

Slide 81

Slide 81 text

Benchmark Benchmark.ips do |bm| bm.report 'INDEX: Integration Test' do Minitest.run_one_method(IntegrationTest, 'test_index') end bm.report 'INDEX: Functional Test' do Minitest.run_one_method(ControllerTest, 'test_index') end bm.compare! end Controller Integration Compare

Slide 82

Slide 82 text

Result Calculating ------------------------------------- INDEX: Integration Test 15.000 i/100ms INDEX: Functional Test 39.000 i/100ms ------------------------------------------------- INDEX: Integration Test 158.019 (± 8.2%) i/s - 795.000 INDEX: Functional Test 390.853 (± 9.0%) i/s - 1.950k Comparison: INDEX: Functional Test: 390.9 i/s INDEX: Integration Test: 158.0 i/s - 2.47x slower Comparison

Slide 83

Slide 83 text

What is slow?

Slide 84

Slide 84 text

stackprof

Slide 85

Slide 85 text

Controller Bottleneck Minitest.run_one_method(ControllerTest, 'test_index') StackProf.run(mode: :cpu, out: 'stackprof.dump') do 3000.times do Minitest.run_one_method(ControllerTest, 'test_index') end end

Slide 86

Slide 86 text

CPU time vs Wall time

Slide 87

Slide 87 text

`sleep` is slow, but doesn’t use CPU

Slide 88

Slide 88 text

View Stack $ stackprof stackprof.dump

Slide 89

Slide 89 text

Stack ================================== Mode: cpu(1000) Samples: 3280 (0.00% miss rate) GC: 134 (4.09%) ================================== TOTAL (pct) SAMPLES (pct) FRAME 3146 (95.9%) 1740 (53.0%) Minitest::Runnable.on_signal 483 (14.7%) 483 (14.7%) Dependencies#search_for_file 207 (6.3%) 191 (5.8%) SQLite3::Database#prepare 95 (2.9%) 95 (2.9%) Inflector#underscore 47 (1.4%) 46 (1.4%) TestRequest#initialize

Slide 90

Slide 90 text

53.0% in Minitest::Runnable.on_signal

Slide 91

Slide 91 text

on_signal def self.on_signal name, action # :nodoc: supported = SIGNALS[name] old_trap = trap name do old_trap.call if old_trap.respond_to? :call action.call end if supported yield ensure trap name, old_trap if supported end

Slide 92

Slide 92 text

JUST DELETE IT

Slide 93

Slide 93 text

on_signal def self.on_signal name, action yield end

Slide 94

Slide 94 text

Results ================================== Mode: cpu(1000) Samples: 4246 (0.00% miss rate) GC: 386 (9.09%) ================================== TOTAL (pct) SAMPLES (pct) FRAME 220 (5.2%) 185 (4.4%) SQLite3::Database#prepare 148 (3.5%) 131 (3.1%) TestRequest#initialize 113 (2.7%) 113 (2.7%) Event#initialize 105 (2.5%) 105 (2.5%) PerThreadRegistry#instance

Slide 95

Slide 95 text

50% faster!

Slide 96

Slide 96 text

Slide 97

Slide 97 text

Time the process $ time ruby benchmark.rb real 0m12.358s user 0m11.732s sys 0m0.498s $ time ruby benchmark.rb real 0m12.055s user 0m11.462s sys 0m0.475s Before After

Slide 98

Slide 98 text

&

Slide 99

Slide 99 text

CPU bound

Slide 100

Slide 100 text

Wall should be similar to CPU

Slide 101

Slide 101 text

Stack (CPU) ================================== Mode: cpu(1000) Samples: 3280 (0.00% miss rate) GC: 134 (4.09%) ================================== TOTAL (pct) SAMPLES (pct) FRAME 3146 (95.9%) 1740 (53.0%) Minitest::Runnable.on_signal 483 (14.7%) 483 (14.7%) Dependencies#search_for_file 207 (6.3%) 191 (5.8%) SQLite3::Database#prepare 95 (2.9%) 95 (2.9%) Inflector#underscore 47 (1.4%) 46 (1.4%) TestRequest#initialize

Slide 102

Slide 102 text

Stack (WALL) ================================== Mode: wall(1000) Samples: 8797 (0.11% miss rate) GC: 535 (6.08%) ================================== TOTAL (pct) SAMPLES (pct) FRAME 286 (3.3%) 286 (3.3%) PerThreadRegistry#instance 233 (2.6%) 233 (2.6%) Formatter#non_recursive 336 (3.8%) 227 (2.6%) Class#class_attribute 209 (2.4%) 209 (2.4%) NonConcurrentCacheBackend#[] 198 (2.3%) 198 (2.3%) ActiveRecord::Base.logger

Slide 103

Slide 103 text

'

Slide 104

Slide 104 text

Even profilers have bugs!

Slide 105

Slide 105 text

Something useful

Slide 106

Slide 106 text

Integration test profiles.

Slide 107

Slide 107 text

Stack (WALL) ================================== Mode: wall(1000) Samples: 20168 (2.95% miss rate) GC: 1247 (6.18%) ================================== TOTAL (pct) SAMPLES (pct) FRAME 6401 (31.7%) 5622 (27.9%) block in Module#delegate 495 (2.5%) 495 (2.5%) PerThreadRegistry#instance 341 (1.7%) 341 (1.7%) NonConcurrentCacheBackend#[] 317 (1.6%) 317 (1.6%) Set#include?

Slide 108

Slide 108 text

Calling `delegate` is slow.

Slide 109

Slide 109 text

Patch - delegate :url_for, :optimize_routes_generation?, to: '@_routes' + def url_for(options) + @_routes.url_for(options) + end + + def optimize_routes_generation? + @_routes.optimize_routes_generation? + end +

Slide 110

Slide 110 text

Check our progress

Slide 111

Slide 111 text

2.47x slower

Slide 112

Slide 112 text

Result Calculating ------------------------------------- INDEX: Integration Test 23.000 i/100ms INDEX: Functional Test 37.000 i/100ms ------------------------------------------------- INDEX: Integration Test 251.487 (±12.7%) i/s - 1.242k INDEX: Functional Test 365.112 (±11.0%) i/s - 1.813k Comparison: INDEX: Functional Test: 365.1 i/s INDEX: Integration Test: 251.5 i/s - 1.45x slower Comparison

Slide 113

Slide 113 text

2.47x to 1.45x

Slide 114

Slide 114 text

Verify $ time ruby test.rb real 0m26.821s user 0m25.277s sys 0m0.903s $ time ruby test.rb real 0m16.632s user 0m15.709s sys 0m0.638s Before After

Slide 115

Slide 115 text

Even profilers have bugs

Slide 116

Slide 116 text

Always measure

Slide 117

Slide 117 text

Verify results

Slide 118

Slide 118 text

Today: 10% slower

Slide 119

Slide 119 text

But faster than controller tests in previous versions

Slide 120

Slide 120 text

Soup two Nuts Performance

Slide 121

Slide 121 text

bundler

Slide 122

Slide 122 text

Binstubs

Slide 123

Slide 123 text

bin stub source code #!/Users/aaron/.rbenv/versions/ruby-trunk/bin/ruby # # This file was generated by RubyGems. # # The application 'bundler' is installed as part of a gem, and # this file is here to facilitate running it. # # some unimportant stuff happens here! gem 'bundler', version load Gem.bin_path('bundler', 'bundle', version)

Slide 124

Slide 124 text

gem

Slide 125

Slide 125 text

require

Slide 126

Slide 126 text

What does `gem` do?

Slide 127

Slide 127 text

Finds a gemspec

Slide 128

Slide 128 text

Puts a path on $LOAD_PATH

Slide 129

Slide 129 text

Load Path Manipulation > x = $LOAD_PATH.dup; nil => nil > gem 'bundler' => true > $LOAD_PATH - x => ["/Users/aaron/.rbenv/versions/ruby-trunk/lib/ruby/gems/2.3.0/gems/ bundler-1.9.2/lib"]

Slide 130

Slide 130 text

What Happens When File is Required?

Slide 131

Slide 131 text

Is File in $LOAD_PATH? Load it! Is file in Gemspec? Add gemspec to $LOAD_PATH

Slide 132

Slide 132 text

RubyGems performance

Slide 133

Slide 133 text

Test Methodology

Slide 134

Slide 134 text

Require One File And Increase Gems

Slide 135

Slide 135 text

Activate one Gem And Increase Gems

Slide 136

Slide 136 text

time to require one file Time in seconds 0 0.08 0.16 0.24 0.32 Number of Gems on the system 0 100 200 300 400 500 600 700 800 900 1000 best worst

Slide 137

Slide 137 text

allocations to require one file Number of allocations 0 22500 45000 67500 90000 Number of Gems on the system 0 100 200 300 400 500 600 700 800 900 1000 best worst

Slide 138

Slide 138 text

time to activate one gem Time in seconds 0 0.06 0.12 0.18 0.24 Number of Gems on the system 0 100 200 300 400 500 600 700 800 900 1000 best worst

Slide 139

Slide 139 text

allocations to activate one gem Number of allocations 0 10000 20000 30000 40000 Number of Gems on the system 0 100 200 300 400 500 600 700 800 900 1000

Slide 140

Slide 140 text

Bundler Performance

Slide 141

Slide 141 text

Test Method

Slide 142

Slide 142 text

One Gem in Gemfile Increase Gems on System

Slide 143

Slide 143 text

Increase Gems in Gemfile Constant Gems on System

Slide 144

Slide 144 text

*NO DEPENDENCIES

Slide 145

Slide 145 text

*ALL LOCAL

Slide 146

Slide 146 text

Vary System Gems

Slide 147

Slide 147 text

`bundle update` with one gem Time in Seconds 0 0.2 0.4 0.6 0.8 Number of Gems on the system 100 200 300 400 500 600 700 800 900 1000

Slide 148

Slide 148 text

`bundle exec` with one gem Time in Seconds 0 0.2 0.4 0.6 0.8 Number of Gems on the system 100 200 300 400 500 600 700 800 900 1000

Slide 149

Slide 149 text

Vary Gems in Gemfile

Slide 150

Slide 150 text

`bundle exec` time Time in seconds 1.44 1.45 1.46 1.47 1.48 Number of gems in the gemfile 0 100 200 300 400 500 600 700 800 900 1000 y = 0.0008x + 1.4603 R² = 0.1323

Slide 151

Slide 151 text

Bundle Exec Time Breakdown (ms) Other? 750 Spec Load 710

Slide 152

Slide 152 text

`bundle update` time Time in Seconds 0 17.5 35 52.5 70 Number of Gems in the Gemfile 0 100 200 300 400 500 600 700 800 900 1000 y = 0.6984x2 - 2.9466x + 13.856 R² = 0.999

Slide 153

Slide 153 text

Thanks Excel

Slide 154

Slide 154 text

No content

Slide 155

Slide 155 text

No content

Slide 156

Slide 156 text

No content

Slide 157

Slide 157 text

R Code - Fit curve > bundler <- read.csv(file="~/bundle_test/ run_bundle_updats.csv",head=TRUE,sep=",") > fit2 <- lm(bundler$time ~ poly(bundler$gems, 2, raw = TRUE)) > xx <- seq(0, 1000, length=11) > plot(bundler$gems, bundler$time) > lines(xx, predict(fit2, data.frame(x = xx)), col="red")

Slide 158

Slide 158 text

No content

Slide 159

Slide 159 text

R Code - Coefficients > coefficients(fit2) (Intercept) poly(bundler$gems, 2, raw = TRUE)1 poly(bundler$gems, 2, raw = TRUE)2 1.160827e+01 -1.549806e-02 6.984136e-05 > summary(fit2)$r.squared [1] 0.9989536 >

Slide 160

Slide 160 text

No content

Slide 161

Slide 161 text

Make Predictions > second_order <- function(newdist, model) { + coefs <- coef(model) + res <- coefs[1] + (coefs[2] * newdist) + (coefs[3] * newdist^2) + return(res) + } > second_order(0, fit2) (Intercept) 11.60827 > second_order(10000, fit2) (Intercept) 6840.763 empty gemfile 10k gemfile

Slide 162

Slide 162 text

How to Fix?

Slide 163

Slide 163 text

What do we know?

Slide 164

Slide 164 text

bin stub source code #!/Users/aaron/.rbenv/versions/ruby-trunk/bin/ruby # # This file was generated by RubyGems. # # The application 'bundler' is installed as part of a gem, and # this file is here to facilitate running it. # # some unimportant stuff happens here! gem 'bundler', version load Gem.bin_path('bundler', 'bundle', version) gem name

Slide 165

Slide 165 text

Gemfile.lock GEM specs: pkg0 (1) PLATFORMS ruby DEPENDENCIES pkg0 gem name and version!

Slide 166

Slide 166 text

Spec loading code today def get_spec name spec_cache = Dir[File.join(dir, "*.gemspec")] spec_cache.find { |spec| spec.name == name } end

Slide 167

Slide 167 text

bundle -v against 100k gems [aaron@TC rubygems (master)]$ time GEM_HOME=`pwd`/100k_gems GEM_PATH=`pwd`/100k_gems ruby -I lib 100k_gems/bin/bundle -v Bundler version 1.9.4 real 0m50.045s user 0m15.815s sys 0m11.967s

Slide 168

Slide 168 text

Spec loading code I want def get_spec name spec_cache = Dir[File.join(dir, "#{name}-*.gemspec")] spec_cache.find { |spec| spec.name == name } end

Slide 169

Slide 169 text

bundle -v against 100k gems [aaron@TC rubygems (specs)]$ time GEM_HOME=`pwd`/100k_gems GEM_PATH=`pwd`/100k_gems ruby -I lib 100k_gems/bin/bundle -v Bundler version 1.9.4 real 0m0.632s user 0m0.479s sys 0m0.124s

Slide 170

Slide 170 text

Still O(n)

Slide 171

Slide 171 text

I think we can make it O(1)

Slide 172

Slide 172 text

Future Work: Make `require` O(1)

Slide 173

Slide 173 text

Don’t recalculate static data

Slide 174

Slide 174 text

Take measurements

Slide 175

Slide 175 text

Question intuitions

Slide 176

Slide 176 text

I have stickers!

Slide 177

Slide 177 text

THANK YOU!!!