diff --git a/.rspec b/.rspec
new file mode 100644
index 00000000..c99d2e73
--- /dev/null
+++ b/.rspec
@@ -0,0 +1 @@
+--require spec_helper
diff --git a/Gemfile b/Gemfile
index e20b1260..63579dc1 100644
--- a/Gemfile
+++ b/Gemfile
@@ -7,16 +7,20 @@ gem 'rails', '~> 5.2.3'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.11'
gem 'bootsnap', '>= 1.1.0', require: false
+gem 'activerecord-import'
group :development, :test do
- # Call 'byebug' anywhere in the code to stop execution and get a debugger console
- gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
+ gem 'pry'
+ gem 'rspec-rails'
+ gem 'rspec-benchmark'
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
+ gem 'rack-mini-profiler'
+ gem 'bullet'
end
group :test do
diff --git a/Gemfile.lock b/Gemfile.lock
index fccf6f5f..323c0a91 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -33,6 +33,8 @@ GEM
activemodel (= 5.2.3)
activesupport (= 5.2.3)
arel (>= 9.0)
+ activerecord-import (1.0.4)
+ activerecord (>= 3.2)
activestorage (5.2.3)
actionpack (= 5.2.3)
activerecord (= 5.2.3)
@@ -43,13 +45,20 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (9.0.0)
+ benchmark-malloc (0.1.0)
+ benchmark-perf (0.5.0)
+ benchmark-trend (0.3.0)
bindex (0.6.0)
bootsnap (1.4.2)
msgpack (~> 1.0)
builder (3.2.3)
- byebug (11.0.1)
+ bullet (6.1.0)
+ activesupport (>= 3.0.0)
+ uniform_notifier (~> 1.11)
+ coderay (1.1.2)
concurrent-ruby (1.1.5)
crass (1.0.4)
+ diff-lcs (1.3)
erubi (1.8.0)
ffi (1.10.0)
globalid (0.4.2)
@@ -77,8 +86,13 @@ GEM
nokogiri (1.10.2)
mini_portile2 (~> 2.4.0)
pg (1.1.4)
+ pry (0.12.2)
+ coderay (~> 1.1.0)
+ method_source (~> 0.9.0)
puma (3.12.1)
rack (2.0.6)
+ rack-mini-profiler (1.1.6)
+ rack (>= 1.2.0)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (5.2.3)
@@ -109,6 +123,32 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
+ rspec (3.9.0)
+ rspec-core (~> 3.9.0)
+ rspec-expectations (~> 3.9.0)
+ rspec-mocks (~> 3.9.0)
+ rspec-benchmark (0.5.1)
+ benchmark-malloc (~> 0.1.0)
+ benchmark-perf (~> 0.5.0)
+ benchmark-trend (~> 0.3.0)
+ rspec (>= 3.0.0, < 4.0.0)
+ rspec-core (3.9.1)
+ rspec-support (~> 3.9.1)
+ rspec-expectations (3.9.0)
+ diff-lcs (>= 1.2.0, < 2.0)
+ rspec-support (~> 3.9.0)
+ rspec-mocks (3.9.1)
+ diff-lcs (>= 1.2.0, < 2.0)
+ rspec-support (~> 3.9.0)
+ rspec-rails (3.9.0)
+ actionpack (>= 3.0)
+ activesupport (>= 3.0)
+ railties (>= 3.0)
+ rspec-core (~> 3.9.0)
+ rspec-expectations (~> 3.9.0)
+ rspec-mocks (~> 3.9.0)
+ rspec-support (~> 3.9.0)
+ rspec-support (3.9.2)
ruby_dep (1.5.0)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
@@ -121,6 +161,7 @@ GEM
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
+ uniform_notifier (1.13.0)
web-console (3.7.0)
actionview (>= 5.0)
activemodel (>= 5.0)
@@ -134,12 +175,17 @@ PLATFORMS
ruby
DEPENDENCIES
+ activerecord-import
bootsnap (>= 1.1.0)
- byebug
+ bullet
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
+ pry
puma (~> 3.11)
+ rack-mini-profiler
rails (~> 5.2.3)
+ rspec-benchmark
+ rspec-rails
tzinfo-data
web-console (>= 3.3.0)
diff --git a/app/controllers/trips_controller.rb b/app/controllers/trips_controller.rb
index acb38be2..1431925c 100644
--- a/app/controllers/trips_controller.rb
+++ b/app/controllers/trips_controller.rb
@@ -2,6 +2,6 @@ class TripsController < ApplicationController
def index
@from = City.find_by_name!(params[:from])
@to = City.find_by_name!(params[:to])
- @trips = Trip.where(from: @from, to: @to).order(:start_time)
+ @trips = Trip.includes(bus: :services).where(from: @from, to: @to).order(:start_time)
end
end
diff --git a/app/models/buses_services.rb b/app/models/buses_services.rb
new file mode 100644
index 00000000..9f4bd614
--- /dev/null
+++ b/app/models/buses_services.rb
@@ -0,0 +1,4 @@
+class BusesServices < ApplicationRecord
+ belongs_to :bus
+ belongs_to :service
+end
diff --git a/app/views/trips/_delimiter.html.erb b/app/views/trips/_delimiter.html.erb
deleted file mode 100644
index 3f845ad0..00000000
--- a/app/views/trips/_delimiter.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-====================================================
diff --git a/app/views/trips/_service.html.erb b/app/views/trips/_service.html.erb
deleted file mode 100644
index 178ea8c0..00000000
--- a/app/views/trips/_service.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-
<%= "#{service.name}" %>
diff --git a/app/views/trips/_services.html.erb b/app/views/trips/_services.html.erb
deleted file mode 100644
index 2de639fc..00000000
--- a/app/views/trips/_services.html.erb
+++ /dev/null
@@ -1,6 +0,0 @@
-Сервисы в автобусе:
-
- <% services.each do |service| %>
- <%= render "service", service: service %>
- <% end %>
-
diff --git a/app/views/trips/_trip.html.erb b/app/views/trips/_trip.html.erb
deleted file mode 100644
index fa1de9aa..00000000
--- a/app/views/trips/_trip.html.erb
+++ /dev/null
@@ -1,5 +0,0 @@
-<%= "Отправление: #{trip.start_time}" %>
-<%= "Прибытие: #{(Time.parse(trip.start_time) + trip.duration_minutes.minutes).strftime('%H:%M')}" %>
-<%= "В пути: #{trip.duration_minutes / 60}ч. #{trip.duration_minutes % 60}мин." %>
-<%= "Цена: #{trip.price_cents / 100}р. #{trip.price_cents % 100}коп." %>
-<%= "Автобус: #{trip.bus.model} №#{trip.bus.number}" %>
diff --git a/app/views/trips/index.html.erb b/app/views/trips/index.html.erb
index a60bce41..781e1d0c 100644
--- a/app/views/trips/index.html.erb
+++ b/app/views/trips/index.html.erb
@@ -7,10 +7,19 @@
<% @trips.each do |trip| %>
- <%= render "trip", trip: trip %>
+ - <%= "Отправление: #{trip.start_time}" %>
+ - <%= "Прибытие: #{(Time.parse(trip.start_time) + trip.duration_minutes.minutes).strftime('%H:%M')}" %>
+ - <%= "В пути: #{trip.duration_minutes / 60}ч. #{trip.duration_minutes % 60}мин." %>
+ - <%= "Цена: #{trip.price_cents / 100}р. #{trip.price_cents % 100}коп." %>
+ - <%= "Автобус: #{trip.bus.model} №#{trip.bus.number}" %>
<% if trip.bus.services.present? %>
- <%= render "services", services: trip.bus.services %>
+ - Сервисы в автобусе:
+
+ <% trip.bus.services.each do |service| %>
+ - <%= "#{service.name}" %>
+ <% end %>
+
<% end %>
- <%= render "delimiter" %>
+ <%= '====================================================' %>
<% end %>
diff --git a/case-study.md b/case-study.md
new file mode 100644
index 00000000..6874a564
--- /dev/null
+++ b/case-study.md
@@ -0,0 +1,155 @@
+# Case-study оптимизации
+
+## Актуальная проблема
+Дано веб-приложение для поиска рейсовых междугородних автобусов. В нем есть две основные проблемы.
+1. Для наполнения базы данными по рейсам используется рейк-таска, которая импортрует информацию о маршрутах из
+json файла. Операция импорта на больших файлах занимает слишком много времени. Необходимо снизить время этой операции.
+2. Необходимо оптимизировать рендер страницы со списком маршрутов. Сейчас она загружается слишком долго.
+
+## Формирование метрики
+В обоих проблема ключевой метрикой является время работы. Для поставленых задач определим для себя такие цели:
+1. Загрузка файла со 100_000 рейсов (large.json) в пределах 1 минуты.
+2. Устранить освновные проблемы при рендере индекса рейсов, загруженных из `large.json`.
+
+## Гарантия корректности работы оптимизированной программы
+Перед тем, как дербанить программу, напишем простой тест, чтобы убедиться, что выдача ручки с индексом рейсов
+не изменилась.
+```
+describe 'reload json task' do
+ ...
+ it 'creates all instances' do
+ expect(City.count).to eq 2
+ expect(Service.count).to eq 2
+ expect(Bus.count).to eq 1
+ expect(Trip.count).to eq 10
+ end
+
+ it 'creates correct instances' do
+ bus_attrs.each do |k, v|
+ expect(Bus.first.attributes[k]).to eq v
+ end
+ expect((Service.pluck(:name) & service_names).size).to eq 2
+ expect((City.pluck(:name) & city_names).size).to eq 2
+ first_trip_attrs.each do |k, v|
+ expect(Trip.first.attributes[k]).to eq v
+ end
+ end
+end
+```
+
+# Проблема 1
+## Feedback loop
+Для эффективного фидбек лупа напишем бенчмарк тест импорта данных.
+```
+describe 'large data import' do
+ it 'works under 1 minute' do
+ expect do
+ `rake "reload_json[fixtures/large.json]"`
+ end.to perform_under(60).sec
+ end
+end
+```
+
+При первом прогоне он ожидаемо не проходит.
+
+## Вникаем в детали системы, чтобы найти главные точки роста
+### Итерация 1
+Посмотрим на код таски импорта. Почти вся работа происходит в итерации по трипам.
+Попробуем включить ActiveRecord логгер и записать вывод в файл.
+```ruby
+ActiveRecord::Base.logger = Logger.new(STDOUT)
+```
+
+```
+$ touch asdf
+$ bundle exec rake 'reload_json[fixtures/small.json]' > asdf
+```
+
+Посчитаем количество инсертов и селектов, которые делаются скриптом при импорте `small.json` (1000 трипов).
+```
+$ grep INSERT asdf | wc -l
+4265
+$ grep SELECT asdf | wc -l
+9239
+```
+
+Проделаем то же самое с файлом `medium.json` (10_000 трипов).
+```
+$ grep INSERT asdf | wc -l
+15705
+$ grep SELECT asdf | wc -l
+96639
+```
+
+Как видим, количество инсертов и селектов огромно, и растет вместе с количеством трипов. И хотя каждый
+запрос достаточно быстр, в основном это считаные миллисекунды, все же огромное количество запросов
+сильно замедляет импорт. Воспользуемся библиотечкой `activerecord-import`, чтобы драматически сократить
+количество обращений в базу.
+
+Activerecord-import умеет возвращать информацию о количестве инсертов - сразу спросим его, скольо инсертов он
+сделал на те же инпуты, что в примерах выше.
+```
+$ bundle exec rake 'reload_json[fixtures/small.json]'
+5
+$ bundle exec rake 'reload_json[fixtures/medium.json]'
+5
+```
+
+Чудно! количество инсертов не растет с инпутом.
+Наш тест на корректность зеленый, поэтому попробуем тест на время импорта большого файла.
+```
+$ bundle exec rspec spec/tasks/reload_json_performance_spec.rb
+.
+
+Finished in 34.96 seconds (files took 0.51447 seconds to load)
+ 1 example, 0 failures
+```
+
+Отлично! Импорт проходит за 35 секунд. Если попробовать утилиту `time`, то она покажет еще более
+оптимистичный результат в 18 секунд:
+```
+$ time bundle exec rake 'reload_json[fixtures/large.json]'
+bundle exec rake 'reload_json[fixtures/large.json]' 17.34s user 0.24s system 94% cpu 18.621 total
+```
+
+Воспользуемся в таске полезной фичей гема и сразу добавим вывод варнингов если есть зафейленые инсерты:
+```
+fails = []
+...
+fails += Service.import(services).failed_instances
+fails += City.import(cities.to_a).failed_instances
+fails += Bus.import(buses.to_a).failed_instances
+...
+if fails.any?
+ puts "Failed instances: #{fails}"
+else
+ puts 'Everything is fine.'
+end
+```
+И перейдем ко второй проблеме.
+
+# Проблема 2
+## Feedback loop
+Поставим `rack-mini-profiler`, чтобы отслеживать время рендера. Первый результат - больше 6 секунд.
+
+## Вникаем в детали системы, чтобы найти главные точки роста
+Попробуем посмотреть детальные отчеты мини профайлера, а так же поставим `bullet` чтобы избежать лишних запросов.
+
+### Итерация 1
+Сразу же видим подсказки от буллета - добавим к трипам `includes(bus: :services)`
+Время пребывания в базе сразу сократилось с примерно полутора секунд до 60мс.
+Теперь основное время занимает рендеринг паршиалов.
+
+### Итерация 2
+Попробуем избавиться от них полностью, перенеся все их содержимое прямо в `index.html.erb`.
+Время рендера вьюх сократилось почти вдвое!
+```
+Completed 200 OK in 3959ms (Views: 3890.6ms | ActiveRecord: 52.4ms)
+```
+
+Улучшать рендеринг далее, без вмешательства в работу рендера в самой рельсе скорее всего не удастся,
+а улучшать время работы в базе на фоне времени вьюхи не имеет смысла, поэтому остановимся на таком результате.
+
+# Результаты
+1. Время импорта `large.json` сократилось до 19 секунд.
+2. Время рендера индекса для этих данных сократилось до 4 секунд.
diff --git a/config/environments/development.rb b/config/environments/development.rb
index 1311e3e4..948e5224 100644
--- a/config/environments/development.rb
+++ b/config/environments/development.rb
@@ -58,4 +58,10 @@
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
+
+ config.after_initialize do
+ Bullet.enable = true
+ Bullet.add_footer = true
+ end
end
+
diff --git a/config/routes.rb b/config/routes.rb
index a2da6a7b..27feb596 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,5 +1,4 @@
Rails.application.routes.draw do
- # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get "/" => "statistics#index"
get "автобусы/:from/:to" => "trips#index"
end
diff --git a/lib/tasks/utils.rake b/lib/tasks/utils.rake
index 540fe871..18cb8277 100644
--- a/lib/tasks/utils.rake
+++ b/lib/tasks/utils.rake
@@ -1,6 +1,8 @@
# Наивная загрузка данных из json-файла в БД
# rake reload_json[fixtures/small.json]
task :reload_json, [:file_name] => :environment do |_task, args|
+ # ActiveRecord::Base.logger = Logger.new(STDOUT)
+ fails = []
json = JSON.parse(File.read(args.file_name))
ActiveRecord::Base.transaction do
@@ -8,27 +10,52 @@ task :reload_json, [:file_name] => :environment do |_task, args|
Bus.delete_all
Service.delete_all
Trip.delete_all
- ActiveRecord::Base.connection.execute('delete from buses_services;')
+ BusesServices.delete_all
+
+ cities = Set.new
+ buses = Set.new
+ trips = Set.new
+ buses_services = Set.new
+
+ services = Service::SERVICES.map { |name| Service.new(name: name) }
json.each do |trip|
- from = City.find_or_create_by(name: trip['from'])
- to = City.find_or_create_by(name: trip['to'])
- services = []
+ cities << { name: trip['from'] }
+ cities << { name: trip['to'] }
+ buses << { number: trip['bus']['number'], model: trip['bus']['model'] }
+ end
+
+ fails += Service.import(services).failed_instances
+ fails += City.import(cities.to_a).failed_instances
+ fails += Bus.import(buses.to_a).failed_instances
+
+ city_id_by_name = City.all.map { |city| [city.name, city.id] }.to_h
+ bus_id_by_number = Bus.all.map { |bus| [bus.number, bus.id] }.to_h
+ service_id_by_name = Service.all.map { |ser| [ser.name, ser.id] }.to_h
+
+ json.each do |trip|
+ bus_id = bus_id_by_number[trip['bus']['number']]
trip['bus']['services'].each do |service|
- s = Service.find_or_create_by(name: service)
- services << s
+ service_id = service_id_by_name[service]
+ buses_services << { bus_id: bus_id, service_id: service_id }
end
- bus = Bus.find_or_create_by(number: trip['bus']['number'])
- bus.update(model: trip['bus']['model'], services: services)
- Trip.create!(
- from: from,
- to: to,
- bus: bus,
+ trips << {
+ from_id: city_id_by_name[trip['from']],
+ to_id: city_id_by_name[trip['to']],
+ bus_id: bus_id_by_number[trip['bus']['number']],
start_time: trip['start_time'],
duration_minutes: trip['duration_minutes'],
- price_cents: trip['price_cents'],
- )
+ price_cents: trip['price_cents']
+ }
+ end
+
+ fails += Trip.import(trips.to_a).failed_instances
+ fails += BusesServices.import(buses_services.to_a).failed_instances
+ if fails.any?
+ puts "Failed instances: #{fails}"
+ else
+ puts 'Everything is fine.'
end
end
end
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
new file mode 100644
index 00000000..2d561e85
--- /dev/null
+++ b/spec/rails_helper.rb
@@ -0,0 +1,63 @@
+# This file is copied to spec/ when you run 'rails generate rspec:install'
+require 'spec_helper'
+ENV['RAILS_ENV'] ||= 'test'
+
+require File.expand_path('../config/environment', __dir__)
+
+# Prevent database truncation if the environment is production
+abort("The Rails environment is running in production mode!") if Rails.env.production?
+require 'rspec/rails'
+# Add additional requires below this line. Rails is not loaded until this point!
+
+# Requires supporting ruby files with custom matchers and macros, etc, in
+# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
+# run as spec files by default. This means that files in spec/support that end
+# in _spec.rb will both be required and run as specs, causing the specs to be
+# run twice. It is recommended that you do not name files matching this glob to
+# end with _spec.rb. You can configure this pattern with the --pattern
+# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
+#
+# The following line is provided for convenience purposes. It has the downside
+# of increasing the boot-up time by auto-requiring all files in the support
+# directory. Alternatively, in the individual `*_spec.rb` files, manually
+# require only the support files necessary.
+#
+# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
+
+# Checks for pending migrations and applies them before tests are run.
+# If you are not using ActiveRecord, you can remove these lines.
+begin
+ ActiveRecord::Migration.maintain_test_schema!
+rescue ActiveRecord::PendingMigrationError => e
+ puts e.to_s.strip
+ exit 1
+end
+RSpec.configure do |config|
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
+ config.fixture_path = "#{::Rails.root}/fixtures"
+
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
+ # examples within a transaction, remove the following line or assign false
+ # instead of true.
+ config.use_transactional_fixtures = true
+
+ # RSpec Rails can automatically mix in different behaviours to your tests
+ # based on their file location, for example enabling you to call `get` and
+ # `post` in specs under `spec/controllers`.
+ #
+ # You can disable this behaviour by removing the line below, and instead
+ # explicitly tag your specs with their type, e.g.:
+ #
+ # RSpec.describe UsersController, :type => :controller do
+ # # ...
+ # end
+ #
+ # The different available types are documented in the features, such as in
+ # https://relishapp.com/rspec/rspec-rails/docs
+ config.infer_spec_type_from_file_location!
+
+ # Filter lines from Rails gems in backtraces.
+ config.filter_rails_from_backtrace!
+ # arbitrary gems may also be filtered via:
+ # config.filter_gems_from_backtrace("gem name")
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644
index 00000000..ce33d66d
--- /dev/null
+++ b/spec/spec_helper.rb
@@ -0,0 +1,96 @@
+# This file was generated by the `rails generate rspec:install` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# The generated `.rspec` file contains `--require spec_helper` which will cause
+# this file to always be loaded, without a need to explicitly require it in any
+# files.
+#
+# Given that it is always loaded, you are encouraged to keep this file as
+# light-weight as possible. Requiring heavyweight dependencies from this file
+# will add to the boot time of your test suite on EVERY test run, even for an
+# individual file that may not need all of that loaded. Instead, consider making
+# a separate helper file that requires the additional dependencies and performs
+# the additional setup, and require it from the spec files that actually need
+# it.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+RSpec.configure do |config|
+ # rspec-expectations config goes here. You can use an alternate
+ # assertion/expectation library such as wrong or the stdlib/minitest
+ # assertions if you prefer.
+ config.expect_with :rspec do |expectations|
+ # This option will default to `true` in RSpec 4. It makes the `description`
+ # and `failure_message` of custom matchers include text for helper methods
+ # defined using `chain`, e.g.:
+ # be_bigger_than(2).and_smaller_than(4).description
+ # # => "be bigger than 2 and smaller than 4"
+ # ...rather than:
+ # # => "be bigger than 2"
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
+ end
+
+ # rspec-mocks config goes here. You can use an alternate test double
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
+ config.mock_with :rspec do |mocks|
+ # Prevents you from mocking or stubbing a method that does not exist on
+ # a real object. This is generally recommended, and will default to
+ # `true` in RSpec 4.
+ mocks.verify_partial_doubles = true
+ end
+
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
+ # have no way to turn it off -- the option exists only for backwards
+ # compatibility in RSpec 3). It causes shared context metadata to be
+ # inherited by the metadata hash of host groups and examples, rather than
+ # triggering implicit auto-inclusion in groups with matching metadata.
+ config.shared_context_metadata_behavior = :apply_to_host_groups
+
+# The settings below are suggested to provide a good initial experience
+# with RSpec, but feel free to customize to your heart's content.
+=begin
+ # This allows you to limit a spec run to individual examples or groups
+ # you care about by tagging them with `:focus` metadata. When nothing
+ # is tagged with `:focus`, all examples get run. RSpec also provides
+ # aliases for `it`, `describe`, and `context` that include `:focus`
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
+ config.filter_run_when_matching :focus
+
+ # Allows RSpec to persist some state between runs in order to support
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
+ # you configure your source control system to ignore this file.
+ config.example_status_persistence_file_path = "spec/examples.txt"
+
+ # Limits the available syntax to the non-monkey patched syntax that is
+ # recommended. For more details, see:
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
+ config.disable_monkey_patching!
+
+ # Many RSpec users commonly either run the entire suite or an individual
+ # file, and it's useful to allow more verbose output when running an
+ # individual spec file.
+ if config.files_to_run.one?
+ # Use the documentation formatter for detailed output,
+ # unless a formatter has already been configured
+ # (e.g. via a command-line flag).
+ config.default_formatter = "doc"
+ end
+
+ # Print the 10 slowest examples and example groups at the
+ # end of the spec run, to help surface which specs are running
+ # particularly slow.
+ config.profile_examples = 10
+
+ # Run specs in random order to surface order dependencies. If you find an
+ # order dependency and want to debug it, you can fix the order by providing
+ # the seed, which is printed after each run.
+ # --seed 1234
+ config.order = :random
+
+ # Seed global randomization in this process using the `--seed` CLI option.
+ # Setting this allows you to use `--seed` to deterministically reproduce
+ # test failures related to randomization by passing the same `--seed` value
+ # as the one that triggered the failure.
+ Kernel.srand config.seed
+=end
+end
diff --git a/spec/tasks/reload_json_performance_spec.rb b/spec/tasks/reload_json_performance_spec.rb
new file mode 100644
index 00000000..5be729bd
--- /dev/null
+++ b/spec/tasks/reload_json_performance_spec.rb
@@ -0,0 +1,14 @@
+require 'rails_helper'
+require 'rspec-benchmark'
+
+RSpec.configure do |config|
+ config.include RSpec::Benchmark::Matchers
+end
+
+describe 'large data import' do
+ it 'works under 1 minute' do
+ expect do
+ `rake "reload_json[fixtures/large.json]"`
+ end.to perform_under(60).sec
+ end
+end
diff --git a/spec/tasks/reload_json_spec.rb b/spec/tasks/reload_json_spec.rb
new file mode 100644
index 00000000..f5aa39c6
--- /dev/null
+++ b/spec/tasks/reload_json_spec.rb
@@ -0,0 +1,42 @@
+require 'rails_helper'
+
+describe 'reload json task' do
+ let(:bus_attrs) { {"id"=>1, "number"=>"123", "model"=>"Икарус"} }
+ let(:service_names) { ["Туалет", "WiFi"] }
+ let(:city_names) { ["Москва", "Самара"] }
+ let(:first_trip_attrs) do
+ {
+ "id"=>1,
+ "from_id"=>1,
+ "to_id"=>2,
+ "start_time"=>"11:00",
+ "duration_minutes"=>168,
+ "price_cents"=>474,
+ "bus_id"=>1,
+ }
+ end
+
+ before do
+ `rake db:setup`
+ `rake "reload_json[fixtures/example.json]"`
+ end
+
+ it 'creates all instances' do
+ expect(City.count).to eq 2
+ expect(Service.count).to eq 10
+ expect(Bus.count).to eq 1
+ expect(BusesServices.count).to eq 2
+ expect(Trip.count).to eq 10
+ end
+
+ it 'creates correct instances' do
+ bus_attrs.each do |k, v|
+ expect(Bus.first.attributes[k]).to eq v
+ end
+ expect((Service.pluck(:name) & service_names).size).to eq 2
+ expect((City.pluck(:name) & city_names).size).to eq 2
+ first_trip_attrs.each do |k, v|
+ expect(Trip.first.attributes[k]).to eq v
+ end
+ end
+end