Optimizing build time
Performance running tests
Jônatas Paganini @jonatasdp
test suite
RSpec
12K
assertions
~25’
Build time / 3 machines
>100’
in localhost
MONOLITHIC
APP
NOW
~13’
Build time / 4 machines
?
Little Changes on
factories
girl ?factory
Avoid callbacks
Callbacks can be lazy and
unuseful running all the time
Model#skip_callback
To avoid run the the hook each factory
Coverage?
after(:create)
To run it in a specific factory
Profiling Associations
Specializing
factories
Specialized factories
Associations
Results
Use lazy Associations
Associations naturally invokes their own factories.
Avoid factory: for associations
Associations naturally invokes their own factories.
association &block
association &block
quick wins
Use build_stubbed
Instead of create
hack sign_in
Avoid touch database each login
ConfigureRSpec
sign_inbuild_stubbed
Use stub to avoid
inflateassociations
build Instead of create
CI Improvements
We use CircleCI
● Split specs by context
● Hack postgres.conf
● Postgresql on Memory
Splitspecs by
Context
CircleCi divide tests
by time NOT
context
SplitSpecs by
DIRECTORY
postgres.conf
fsync = ’off’
full_page_writes = ’off’
checkpoint_segments = ’100’
checkpoint_timeout = ’45 min’
data_directory = ’/tmp/postgres-on-memory’
dir=/tmp/postgresql-on-memory
dir=/tmp/postgresql-on-memory
mkdir $dir
dir=/tmp/postgresql-on-memory
mkdir $dir
mount -o size=200M,mode=0666 tmpfs $dir
dir=/tmp/postgresql-on-memory
mkdir $dir
mount -o size=200M,mode=0666 tmpfs $dir
chown postgres:postgres $dir
dir=/tmp/postgresql-on-memory
mkdir $dir
mount -o size=200M,mode=0666 tmpfs $dir
chown postgres:postgres $dir
sudo su - postgres -c 
'/usr/lib/postgresql/9.4/bin/initdb 
--encoding=UTF8 --pgdata=$dir
NEXT LEVEL
<what> run <when>
hardcore scenarios
Split Specs
Nightly Build
if [ -n "${RUN_NIGHTLY_BUILD}" ];
rspec spec/features --tag "~smoke"
if [ -n "${RUN_NIGHTLY_BUILD}" ];
rspec spec/features --tag "~smoke"
if [ -z "${RUN_NIGHTLY_BUILD}" ];
rspec spec/features --tag "smoke"
http://shipit.resultadosdigitais.com.br/blog
● /ruby-e-rspec-melhorando-a-legibilidade-de-seus-testes/
● /rspec-performance-tips/
● /otimizando-testes-factory-girl-e-rspec/
Conclusion
● Avoid touch database
● Avoid callbacks no sense
● Avoid things you don’t need
● Write specialized factories
● Rate (classify) your test
● Benchmark all isolated steps
It’s all about responsibilities
Thanks!
@jonatasdp
http://ideia.me
http://shipit.resultadosdigitais.com.br

Otimizando tempo de build: performance da suíte de testes