diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/Gemfile b/Gemfile index 33017fd..279b45e 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,15 @@ gem 'rails', '~> 5.2.3' gem 'pg', '>= 0.18', '< 2.0' gem 'puma', '~> 3.11' gem 'bootsnap', '>= 1.1.0', require: false +gem 'skylight' +gem 'pghero' +gem 'activerecord-import' +gem 'oj' +gem 'progress_bar' +gem 'strong_migrations' +gem 'newrelic_rpm' + + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console @@ -17,9 +26,19 @@ 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 'bullet' end group :test do + gem 'rspec' + gem 'rspec-rails' + gem 'rspec-sqlimit' + gem 'test-prof' + gem 'database_cleaner' + gem 'n_plus_one_control' + gem 'factory_bot_rails' + gem 'faker', :git => 'https://github.com/stympy/faker.git', :branch => 'master' + gem 'pry-byebug' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem diff --git a/Gemfile.lock b/Gemfile.lock index eb22e16..0e5913e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,16 @@ +GIT + remote: https://github.com/stympy/faker.git + revision: 59a92644d5583828add1c68351074f6e52c6deb6 + branch: master + specs: + faker (1.9.3) + i18n (>= 0.7) + pastel (~> 0.7.2) + thor (~> 0.20.0) + tty-pager (~> 0.12.0) + tty-screen (~> 0.6.5) + tty-tree (~> 0.2.0) + GEM remote: https://rubygems.org/ specs: @@ -33,6 +46,8 @@ GEM activemodel (= 5.2.3) activesupport (= 5.2.3) arel (>= 9.0) + activerecord-import (1.0.1) + activerecord (>= 3.2) activestorage (5.2.3) actionpack (= 5.2.3) activerecord (= 5.2.3) @@ -47,13 +62,26 @@ GEM bootsnap (1.4.2) msgpack (~> 1.0) builder (3.2.3) + bullet (5.9.0) + activesupport (>= 3.0.0) + uniform_notifier (~> 1.11) byebug (11.0.1) + coderay (1.1.2) concurrent-ruby (1.1.5) crass (1.0.4) + database_cleaner (1.7.0) + diff-lcs (1.3) + equatable (0.5.0) erubi (1.8.0) + factory_bot (5.0.2) + activesupport (>= 4.2.0) + factory_bot_rails (5.0.2) + factory_bot (~> 5.0.2) + railties (>= 4.2.0) ffi (1.10.0) globalid (0.4.2) activesupport (>= 4.2.0) + highline (2.0.2) i18n (1.6.0) concurrent-ruby (~> 1.0) listen (3.1.5) @@ -73,12 +101,30 @@ GEM mini_portile2 (2.4.0) minitest (5.11.3) msgpack (1.2.9) + n_plus_one_control (0.3.1) + newrelic_rpm (6.2.0.354) nio4r (2.3.1) nokogiri (1.10.2) mini_portile2 (~> 2.4.0) + oj (3.7.12) + options (2.3.2) + pastel (0.7.2) + equatable (~> 0.5.0) + tty-color (~> 0.4.0) pg (1.1.4) + pghero (2.2.0) + activerecord + progress_bar (1.3.0) + highline (>= 1.6, < 3) + options (~> 2.3.0) + pry (0.12.2) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + pry-byebug (3.7.0) + byebug (~> 11.0) + pry (~> 0.10) puma (3.12.1) - rack (2.0.6) + rack (2.0.7) rack-test (1.1.0) rack (>= 1.0, < 3) rails (5.2.3) @@ -109,7 +155,35 @@ GEM rb-fsevent (0.10.3) rb-inotify (0.10.0) ffi (~> 1.0) + rspec (3.8.0) + rspec-core (~> 3.8.0) + rspec-expectations (~> 3.8.0) + rspec-mocks (~> 3.8.0) + rspec-core (3.8.0) + rspec-support (~> 3.8.0) + rspec-expectations (3.8.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.8.0) + rspec-mocks (3.8.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.8.0) + rspec-rails (3.8.2) + actionpack (>= 3.0) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-core (~> 3.8.0) + rspec-expectations (~> 3.8.0) + rspec-mocks (~> 3.8.0) + rspec-support (~> 3.8.0) + rspec-sqlimit (0.0.2) + rails (> 4.0, < 6.0) + rspec (~> 3.0) + rspec-support (3.8.0) ruby_dep (1.5.0) + skylight (3.1.5) + skylight-core (= 3.1.5) + skylight-core (3.1.5) + activesupport (>= 4.2.0) sprockets (3.7.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -117,10 +191,29 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) + strings (0.1.5) + strings-ansi (~> 0.1) + unicode-display_width (~> 1.5) + unicode_utils (~> 1.4) + strings-ansi (0.1.0) + strong_migrations (0.3.1) + activerecord (>= 3.2.0) + test-prof (0.8.0) thor (0.20.3) thread_safe (0.3.6) + tty-color (0.4.3) + tty-pager (0.12.1) + strings (~> 0.1.4) + tty-screen (~> 0.6) + tty-which (~> 0.4) + tty-screen (0.6.5) + tty-tree (0.2.0) + tty-which (0.4.0) tzinfo (1.2.5) thread_safe (~> 0.1) + unicode-display_width (1.5.0) + unicode_utils (1.4.0) + uniform_notifier (1.12.1) web-console (3.7.0) actionview (>= 5.0) activemodel (>= 5.0) @@ -134,12 +227,29 @@ PLATFORMS ruby DEPENDENCIES + activerecord-import bootsnap (>= 1.1.0) + bullet byebug + database_cleaner + factory_bot_rails + faker! listen (>= 3.0.5, < 3.2) + n_plus_one_control + newrelic_rpm + oj pg (>= 0.18, < 2.0) + pghero + progress_bar + pry-byebug puma (~> 3.11) rails (~> 5.2.3) + rspec + rspec-rails + rspec-sqlimit + skylight + strong_migrations + test-prof tzinfo-data web-console (>= 3.3.0) diff --git a/app/controllers/trips_controller.rb b/app/controllers/trips_controller.rb index acb38be..36481d8 100644 --- a/app/controllers/trips_controller.rb +++ b/app/controllers/trips_controller.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + 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.preload(bus: :services).where(from: @from, to: @to).order(:start_time) end end diff --git a/app/models/bus.rb b/app/models/bus.rb index 1dcc54c..d224860 100644 --- a/app/models/bus.rb +++ b/app/models/bus.rb @@ -1,19 +1,22 @@ +# frozen_string_literal: true + class Bus < ApplicationRecord - MODELS = [ - 'Икарус', - 'Мерседес', - 'Сканиа', - 'Буханка', - 'УАЗ', - 'Спринтер', - 'ГАЗ', - 'ПАЗ', - 'Вольво', - 'Газель', + MODELS = %w[ + Икарус + Мерседес + Сканиа + Буханка + УАЗ + Спринтер + ГАЗ + ПАЗ + Вольво + Газель ].freeze has_many :trips - has_and_belongs_to_many :services, join_table: :buses_services + has_many :buses_services + has_many :services, through: :buses_services validates :number, presence: true, uniqueness: true validates :model, inclusion: { in: MODELS } diff --git a/app/models/buses_service.rb b/app/models/buses_service.rb new file mode 100644 index 0000000..976a0c1 --- /dev/null +++ b/app/models/buses_service.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class BusesService < ApplicationRecord + belongs_to :bus + belongs_to :service +end diff --git a/app/models/service.rb b/app/models/service.rb index 9cbb2a3..81d092f 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -1,18 +1,21 @@ +# frozen_string_literal: true + class Service < ApplicationRecord - SERVICES = [ - 'WiFi', - 'Туалет', - 'Работающий туалет', - 'Ремни безопасности', - 'Кондиционер общий', - 'Кондиционер Индивидуальный', - 'Телевизор общий', - 'Телевизор индивидуальный', - 'Стюардесса', - 'Можно не печатать билет', + SERVICES = %w[ + WiFi + Туалет + Работающий туалет + Ремни безопасности + Кондиционер общий + Кондиционер Индивидуальный + Телевизор общий + Телевизор индивидуальный + Стюардесса + Можно не печатать билет ].freeze - has_and_belongs_to_many :buses, join_table: :buses_services + has_many :buses_services + has_many :buses, through: :buses_services validates :name, presence: true validates :name, inclusion: { in: SERVICES } diff --git a/app/services/import_trips_service.rb b/app/services/import_trips_service.rb new file mode 100644 index 0000000..4e433a5 --- /dev/null +++ b/app/services/import_trips_service.rb @@ -0,0 +1,115 @@ +# frozen_string_literal: true + +class ImportTripsService + BATCH_SIZE = 1000 + + def self.load(file_name) + new(file_name).load + end + + attr_accessor :json, :sities, :buses, :services + + def initialize(file_name) + @json = Oj.load(File.read(file_name)) + @sities = {} + @buses = {} + @services = {} + end + + def load + ActiveRecord::Base.transaction do + clean_db! + load_cities + load_buses + load_services + load_buses_services + load_trips + end + end + + def clean_db! + BusesService.delete_all + Trip.delete_all + City.delete_all + Bus.delete_all + Service.delete_all + end + + def load_cities + sities_data = [] + json.each do |trip| + sities_data << { name: trip['from'] } + sities_data << { name: trip['to'] } + end + + sities_data.uniq! + result = City.import(sities_data, returning: :name, **options) + processing_result result, sities + end + + def load_buses + buses_data = [] + json.each do |trip| + buses_data << { + number: trip['bus']['number'], + model: trip['bus']['model'] + } + end + buses_data.uniq! + result = Bus.import(buses_data, returning: :number, **options) + processing_result result, buses + end + + def load_services + services_data = [] + json.each do |trip| + trip['bus']['services'].each do |service| + services_data << { name: service } + end + end + services_data.uniq! + result = Service.import(services_data, returning: :name, **options) + processing_result result, services + end + + def load_buses_services + buses_services = [] + json.each do |trip| + trip['bus']['services'].each do |service| + buses_services << { + bus_id: buses[trip['bus']['number']], + service_id: services[service] + } + end + end + buses_services.uniq! + BusesService.import(buses_services, options) + end + + def load_trips + trips = [] + json.each do |trip| + trips << { + from_id: sities[trip['from']], + to_id: sities[trip['to']], + bus_id: buses[trip['bus']['number']], + start_time: trip['start_time'], + duration_minutes: trip['duration_minutes'], + price_cents: trip['price_cents'] + } + end + + Trip.import(trips, options) + end + + def options + { + batch_size: BATCH_SIZE + } + end + + def processing_result(selection, collection = {}) + ids = selection.ids + selection.results.each.with_index { |attr, i| collection[attr] = ids[i].to_i } + end +end diff --git a/app/views/trips/_delimiter.html.erb b/app/views/trips/_delimiter.html.erb deleted file mode 100644 index 3f845ad..0000000 --- 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 178ea8c..0000000 --- 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 2de639f..0000000 --- a/app/views/trips/_services.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -
  • Сервисы в автобусе:
  • - diff --git a/app/views/trips/_trip.html.erb b/app/views/trips/_trip.html.erb index fa1de9a..acba0e2 100644 --- a/app/views/trips/_trip.html.erb +++ b/app/views/trips/_trip.html.erb @@ -1,5 +1,21 @@ -
  • <%= "Отправление: #{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}" %>
  • +<% cache [trip, trip.bus] do %> + +==================================================== +<% end %> diff --git a/app/views/trips/index.html.erb b/app/views/trips/index.html.erb index a60bce4..40f35c2 100644 --- a/app/views/trips/index.html.erb +++ b/app/views/trips/index.html.erb @@ -2,15 +2,7 @@ <%= "Автобусы #{@from.name} – #{@to.name}" %>

    - <%= "В расписании #{@trips.count} рейсов" %> + <%= "В расписании #{@trips.length} рейсов" %>

    -<% @trips.each do |trip| %> - - <%= render "delimiter" %> -<% end %> +<%= render @trips, cached: true %> diff --git a/config/application.rb b/config/application.rb index 9c33109..fe3da3b 100644 --- a/config/application.rb +++ b/config/application.rb @@ -15,5 +15,6 @@ class Application < Rails::Application # Application configuration can go into files in config/initializers # -- all .rb files in that directory are automatically loaded after loading # the framework and any gems in your application. + config.skylight.environments += ["development"] end end diff --git a/config/environments/development.rb b/config/environments/development.rb index 1311e3e..115845c 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. @@ -58,4 +60,12 @@ # 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.alert = true + Bullet.bullet_logger = true + Bullet.console = true + Bullet.add_footer = true + end end diff --git a/config/routes.rb b/config/routes.rb index a2da6a7..75e4856 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,8 @@ +# frozen_string_literal: true + 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" + get '/' => 'statistics#index' + get 'автобусы/:from/:to' => 'trips#index' + mount PgHero::Engine, at: 'pghero' end diff --git a/db/migrate/20190420191017_add_index_to_bus.rb b/db/migrate/20190420191017_add_index_to_bus.rb new file mode 100644 index 0000000..e04c863 --- /dev/null +++ b/db/migrate/20190420191017_add_index_to_bus.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class AddIndexToBus < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + safety_assured { change_column_null :buses, :number, false } + add_index :buses, :number, algorithm: :concurrently, unique: true + end +end diff --git a/db/migrate/20190420204751_add_index_to_buses_service.rb b/db/migrate/20190420204751_add_index_to_buses_service.rb new file mode 100644 index 0000000..6e6a55c --- /dev/null +++ b/db/migrate/20190420204751_add_index_to_buses_service.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AddIndexToBusesService < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + add_index :buses_services, %i[bus_id service_id], algorithm: :concurrently, unique: true + end +end diff --git a/db/migrate/20190420215325_add_index_to_services.rb b/db/migrate/20190420215325_add_index_to_services.rb new file mode 100644 index 0000000..dc5af49 --- /dev/null +++ b/db/migrate/20190420215325_add_index_to_services.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class AddIndexToServices < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + safety_assured { change_column_null :services, :name, false } + add_index :services, :name, algorithm: :concurrently, unique: true + end +end diff --git a/db/migrate/20190421185932_add_foreign_keys_to_trips.rb b/db/migrate/20190421185932_add_foreign_keys_to_trips.rb new file mode 100644 index 0000000..76ce860 --- /dev/null +++ b/db/migrate/20190421185932_add_foreign_keys_to_trips.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class AddForeignKeysToTrips < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + add_foreign_key :trips, :cities, column: :to_id + add_foreign_key :trips, :cities, column: :from_id + end +end diff --git a/db/migrate/20190421211613_add_index_to_trips.rb b/db/migrate/20190421211613_add_index_to_trips.rb new file mode 100644 index 0000000..72a7e63 --- /dev/null +++ b/db/migrate/20190421211613_add_index_to_trips.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class AddIndexToTrips < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + add_foreign_key :trips, :buses + add_index :trips, :start_time, algorithm: :concurrently, order: { start_time: :asc } + add_index :trips, :bus_id, algorithm: :concurrently + add_index :trips, :from_id, algorithm: :concurrently + add_index :trips, :to_id, algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index f6921e4..64f508b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,19 +10,21 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_03_30_193044) do +ActiveRecord::Schema.define(version: 2019_04_21_211613) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" create_table "buses", force: :cascade do |t| - t.string "number" + t.string "number", null: false t.string "model" + t.index ["number"], name: "index_buses_on_number", unique: true end create_table "buses_services", force: :cascade do |t| t.integer "bus_id" t.integer "service_id" + t.index ["bus_id", "service_id"], name: "index_buses_services_on_bus_id_and_service_id", unique: true end create_table "cities", force: :cascade do |t| @@ -30,7 +32,8 @@ end create_table "services", force: :cascade do |t| - t.string "name" + t.string "name", null: false + t.index ["name"], name: "index_services_on_name", unique: true end create_table "trips", force: :cascade do |t| @@ -40,6 +43,13 @@ t.integer "duration_minutes" t.integer "price_cents" t.integer "bus_id" + t.index ["bus_id"], name: "index_trips_on_bus_id" + t.index ["from_id"], name: "index_trips_on_from_id" + t.index ["start_time"], name: "index_trips_on_start_time" + t.index ["to_id"], name: "index_trips_on_to_id" end + add_foreign_key "trips", "buses" + add_foreign_key "trips", "cities", column: "from_id" + add_foreign_key "trips", "cities", column: "to_id" end diff --git a/lib/tasks/utils.rake b/lib/tasks/utils.rake index 540fe87..1f23da9 100644 --- a/lib/tasks/utils.rake +++ b/lib/tasks/utils.rake @@ -1,34 +1,6 @@ -# Наивная загрузка данных из json-файла в БД +# frozen_string_literal: true + # rake reload_json[fixtures/small.json] task :reload_json, [:file_name] => :environment do |_task, args| - json = JSON.parse(File.read(args.file_name)) - - ActiveRecord::Base.transaction do - City.delete_all - Bus.delete_all - Service.delete_all - Trip.delete_all - ActiveRecord::Base.connection.execute('delete from buses_services;') - - json.each do |trip| - from = City.find_or_create_by(name: trip['from']) - to = City.find_or_create_by(name: trip['to']) - services = [] - trip['bus']['services'].each do |service| - s = Service.find_or_create_by(name: service) - services << s - 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, - start_time: trip['start_time'], - duration_minutes: trip['duration_minutes'], - price_cents: trip['price_cents'], - ) - end - end + ImportTripsService.load(args[:file_name]) end diff --git a/optimization/case-study.md b/optimization/case-study.md new file mode 100644 index 0000000..7c1a29a --- /dev/null +++ b/optimization/case-study.md @@ -0,0 +1,108 @@ +# Case-study оптимизации + +## Оптимизация 1 +Цель: Необходимо оптимизировать механизм перезагрузки расписания из файла так, чтобы он обрабатывал файл в пределах минуты. + +Метрика: Время загрузки файла расписания + +Время загрузки файла `large.json` до оптимизации составило почти **9 минут**. +``` +Warming up -------------------------------------- + small 1.000 i/100ms + medium 1.000 i/100ms + large 1.000 i/100ms +Calculating ------------------------------------- + small 0.123 (± 0.0%) i/s - 1.000 in 8.114570s + medium 0.020 (± 0.0%) i/s - 1.000 in 50.703538s + large 0.002 (± 0.0%) i/s - 1.000 in 516.493682s +``` + +Написан тест проверщий работоспособность сервиса импорта. + +Написан `feedback-loop.rb` выполнящий замер производительности. + +Для чистоты эксперимента вынес загрузку расписания в отдельный сервис `TripsService`: + - Создал индексы для таблиц `Bus`, `Service`, `BusesService` + - Переделал чтение json через `oj` + - Сделал пакетное добавление данных в БД с `activerecord-import` + +В результате: + +Удалось уменьшить время загрузки до **15 сек**. + +Сократил число запросов к базе с **96** к константным **12**. + +``` +Warming up -------------------------------------- + small 1.000 i/100ms + medium 1.000 i/100ms + large 1.000 i/100ms +Calculating ------------------------------------- + small 3.859 (± 0.0%) i/s - 19.000 in 5.025707s + medium 0.572 (± 0.0%) i/s - 3.000 in 5.248226s + large 0.065 (± 0.0%) i/s - 1.000 in 15.374852s + +Comparison: + small: 3.9 i/s + medium: 0.6 i/s - 6.75x slower + large: 0.1 i/s - 59.33x slower +``` + +## Оптимизация 2 +По графику из new relic видно, что много времени занимает рендеринг паршалов и поиск сервисов и автобусов в БД. + +![NewRelic](new_relic.png) + +### Оптимизация +- Перенес паршалы в один паршал `_trips`. +- Добавил кеширование к этому паршалу. +- Избавился от `N+1` с помощью `bullet` и написал тест c `n_plus_one_control` добавил предзагрузку автобусов с сервисами. +- Добавил индексы к полям `trips`. + +В результате проделанной оптимизации время рендеринга страницы составило **824 ms** для файла `large.json` +``` +➜ ab -n 100 -c 10 http://localhost:3000/автобусы/Самара/Москва/ +This is ApacheBench, Version 2.3 <$Revision: 1826891 $> +Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ +Licensed to The Apache Software Foundation, http://www.apache.org/ + +Benchmarking localhost (be patient).....done + + +Server Software: +Server Hostname: localhost +Server Port: 3000 + +Document Path: /автобусы/Самара/Москва/ +Document Length: 71470 bytes + +Concurrency Level: 10 +Time taken for tests: 8.246 seconds +Complete requests: 100 +Failed requests: 0 +Non-2xx responses: 100 +Total transferred: 7171200 bytes +HTML transferred: 7147000 bytes +Requests per second: 12.13 [#/sec] (mean) +Time per request: 824.596 [ms] (mean) +Time per request: 82.460 [ms] (mean, across all concurrent requests) +Transfer rate: 849.28 [Kbytes/sec] received + +Connection Times (ms) + min mean[+/-sd] median max +Connect: 0 0 0.0 0 0 +Processing: 91 791 133.8 817 1120 +Waiting: 91 790 133.8 815 1111 +Total: 91 791 133.8 817 1120 + +Percentage of the requests served within a certain time (ms) + 50% 817 + 66% 834 + 75% 844 + 80% 856 + 90% 892 + 95% 912 + 98% 962 + 99% 1120 + 100% 1120 (longest request) +``` diff --git a/optimization/feedback-loop.rb b/optimization/feedback-loop.rb new file mode 100644 index 0000000..289c83b --- /dev/null +++ b/optimization/feedback-loop.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require 'benchmark/ips' +require 'ruby-prof' +require 'memory_profiler' +require 'active_record' +require './app/models/application_record' +require './app/services/import_trips_service' +require './app/models/city' +require './app/models/bus' +require './app/models/service' +require './app/models/trip' +require './app/models/buses_service' +require 'oj' +require 'progress_bar' +require 'activerecord-import' + +config = YAML.load_file('config/database.yml')['development'] +ActiveRecord::Base.establish_connection(config) + +def reevaluate_metric + Benchmark.ips do |bench| + bench.report('small') { ImportTripsService.load('fixtures/small.json') } + bench.report('medium') { ImportTripsService.load('fixtures/medium.json') } + bench.report('large') { ImportTripsService.load('fixtures/large.json') } + bench.compare! + end +end + +reevaluate_metric diff --git a/optimization/new_relic.png b/optimization/new_relic.png new file mode 100644 index 0000000..7f8e738 Binary files /dev/null and b/optimization/new_relic.png differ diff --git a/spec/controllers/trips_controller_spec.rb b/spec/controllers/trips_controller_spec.rb new file mode 100644 index 0000000..7aec54d --- /dev/null +++ b/spec/controllers/trips_controller_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe TripsController do + describe 'GET index' do + let_it_be(:moscow) { create(:city, name: 'Москва') } + let_it_be(:samara) { create(:city, name: 'Самара') } + + context 'N+1', :n_plus_one do + populate { |n| create_list(:trip, n) } + + specify do + expect { get :index, params: { from: moscow.name, to: samara.name } } + .to perform_constant_number_of_queries + end + end + end +end diff --git a/spec/factories.rb b/spec/factories.rb new file mode 100644 index 0000000..aca9b9e --- /dev/null +++ b/spec/factories.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :trip do + from + to + bus + duration_minutes { 200 } + start_time { '11:45' } + price_cents { 100 } + end + + factory :city, aliases: %i[to from] do + name { Faker::Address.city.tr(' ', '_') } + end + + factory :bus do + number { Faker::Number.number(5) } + model { Bus::MODELS[rand(0..9)] } + + after(:create) do |bus, _evaluator| + create_list(:buses_service, 5, bus: bus) + end + end + + factory :service do + name { Service::SERVICES[rand(0..9)] } + end + + factory :buses_service do + bus + service + end +end diff --git a/spec/fixtures/example.json b/spec/fixtures/example.json new file mode 100644 index 0000000..510b4c2 --- /dev/null +++ b/spec/fixtures/example.json @@ -0,0 +1,152 @@ +[ + { + "bus": { + "model": "Икарус", + "number": "123", + "services": [ + "Туалет", + "WiFi" + ] + }, + "duration_minutes": 168, + "from": "Москва", + "price_cents": 474, + "start_time": "11:00", + "to": "Самара" + }, + { + "bus": { + "model": "Икарус", + "number": "123", + "services": [ + "Туалет", + "WiFi" + ] + }, + "duration_minutes": 37, + "from": "Самара", + "price_cents": 173, + "start_time": "17:30", + "to": "Москва" + }, + { + "bus": { + "model": "Икарус", + "number": "123", + "services": [ + "Туалет", + "WiFi" + ] + }, + "duration_minutes": 323, + "from": "Москва", + "price_cents": 672, + "start_time": "12:00", + "to": "Самара" + }, + { + "bus": { + "model": "Икарус", + "number": "123", + "services": [ + "Туалет", + "WiFi" + ] + }, + "duration_minutes": 315, + "from": "Самара", + "price_cents": 969, + "start_time": "18:30", + "to": "Москва" + }, + { + "bus": { + "model": "Икарус", + "number": "123", + "services": [ + "Туалет", + "WiFi" + ] + }, + "duration_minutes": 304, + "from": "Москва", + "price_cents": 641, + "start_time": "13:00", + "to": "Самара" + }, + { + "bus": { + "model": "Икарус", + "number": "123", + "services": [ + "Туалет", + "WiFi" + ] + }, + "duration_minutes": 21, + "from": "Самара", + "price_cents": 663, + "start_time": "19:30", + "to": "Москва" + }, + { + "bus": { + "model": "Икарус", + "number": "123", + "services": [ + "Туалет", + "WiFi" + ] + }, + "duration_minutes": 598, + "from": "Москва", + "price_cents": 629, + "start_time": "14:00", + "to": "Самара" + }, + { + "bus": { + "model": "Икарус", + "number": "123", + "services": [ + "Туалет", + "WiFi" + ] + }, + "duration_minutes": 292, + "from": "Самара", + "price_cents": 22, + "start_time": "20:30", + "to": "Москва" + }, + { + "bus": { + "model": "Икарус", + "number": "123", + "services": [ + "Туалет", + "WiFi" + ] + }, + "duration_minutes": 127, + "from": "Москва", + "price_cents": 795, + "start_time": "15:00", + "to": "Самара" + }, + { + "bus": { + "model": "Икарус", + "number": "123", + "services": [ + "Туалет", + "WiFi" + ] + }, + "duration_minutes": 183, + "from": "Самара", + "price_cents": 846, + "start_time": "21:30", + "to": "Москва" + } +] diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..bc5fa38 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +# 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' +require 'rspec-sqlimit' +require 'test_prof/recipes/rspec/let_it_be' +require 'test_prof/recipes/rspec/before_all' +require 'test_prof/recipes/rspec/any_fixture' +require 'n_plus_one_control/rspec' +# 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| + config.include FactoryBot::Syntax::Methods + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/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") + config.before(:suite) { DatabaseCleaner.clean_with :truncation } + config.before(:each) do + DatabaseCleaner[:active_record].strategy = :transaction + DatabaseCleaner.start + end + config.after(:each) { DatabaseCleaner.clean } +end diff --git a/spec/services/import_trips_service_spec.rb b/spec/services/import_trips_service_spec.rb new file mode 100644 index 0000000..5c92113 --- /dev/null +++ b/spec/services/import_trips_service_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe ImportTripsService do + subject { ImportTripsService.load('fixtures/example.json') } + + it 'corrects import trips', :aggregate_failures do + subject + expect(City.count).to eq 2 + expect(Trip.count).to eq 10 + expect(Service.count).to eq 2 + expect(Bus.count).to eq 1 + expect(BusesService.count).to eq 2 + end + + it 'has constant number of requests to DB' do + expect { subject }.not_to exceed_query_limit(12) + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..ce33d66 --- /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/test/application_system_test_case.rb b/test/application_system_test_case.rb deleted file mode 100644 index d19212a..0000000 --- a/test/application_system_test_case.rb +++ /dev/null @@ -1,5 +0,0 @@ -require "test_helper" - -class ApplicationSystemTestCase < ActionDispatch::SystemTestCase - driven_by :selenium, using: :chrome, screen_size: [1400, 1400] -end diff --git a/test/controllers/.keep b/test/controllers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/fixtures/.keep b/test/fixtures/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/fixtures/files/.keep b/test/fixtures/files/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/helpers/.keep b/test/helpers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/integration/.keep b/test/integration/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/mailers/.keep b/test/mailers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/models/.keep b/test/models/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/system/.keep b/test/system/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/test_helper.rb b/test/test_helper.rb deleted file mode 100644 index 3ab84e3..0000000 --- a/test/test_helper.rb +++ /dev/null @@ -1,10 +0,0 @@ -ENV['RAILS_ENV'] ||= 'test' -require_relative '../config/environment' -require 'rails/test_help' - -class ActiveSupport::TestCase - # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. - fixtures :all - - # Add more helper methods to be used by all tests here... -end