From 75507fa4590b376ca28e460da1e75c5d3df60383 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Thu, 17 Oct 2024 10:54:55 +0200 Subject: [PATCH 1/3] WIP --- sentry-rails/spec/dummy/test_rails_app/app.rb | 133 ++++++++++++------ 1 file changed, 90 insertions(+), 43 deletions(-) diff --git a/sentry-rails/spec/dummy/test_rails_app/app.rb b/sentry-rails/spec/dummy/test_rails_app/app.rb index 95309fbaa..a8435d2a8 100644 --- a/sentry-rails/spec/dummy/test_rails_app/app.rb +++ b/sentry-rails/spec/dummy/test_rails_app/app.rb @@ -10,6 +10,8 @@ require "action_controller/railtie" require 'sentry/rails' +require 'tempfile' +require 'vernier' ActiveRecord::Base.logger = Logger.new(nil) ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: "db") @@ -43,70 +45,115 @@ class TestApp < Rails::Application require "dummy/test_rails_app/configs/#{FILE_NAME}" def make_basic_app(&block) - run_pre_initialize_cleanup + elapsed("pre_initialize_cleanup") do + run_pre_initialize_cleanup + end + + app = nil - app = Class.new(TestApp) do - def self.name - "RailsTestApp" + elapsed("app_setup") do + app = Class.new(TestApp) do + def self.name + "RailsTestApp" + end end end - app.config.active_support.deprecation = :silence - app.config.action_controller.view_paths = "spec/dummy/test_rails_app" - app.config.hosts = nil - app.config.secret_key_base = "test" - app.config.logger = ActiveSupport::Logger.new(nil) - app.config.eager_load = true - app.config.active_job.queue_adapter = :test + elapsed("app_config") do + app.config.active_support.deprecation = :silence + app.config.action_controller.view_paths = "spec/dummy/test_rails_app" + app.config.hosts = nil + app.config.secret_key_base = "test" + app.config.logger = ActiveSupport::Logger.new(nil) + app.config.eager_load = false + app.config.active_job.queue_adapter = :test + end # Eager load namespaces can be accumulated after repeated initializations and make initialization # slower after each run # This is especially obvious in Rails 7.2, because of https://github.com/rails/rails/pull/49987, but other constants's # accumulation can also cause slowdown # Because this is not necessary for the test, we can simply clear it here - app.config.eager_load_namespaces.clear - - configure_app(app) - - app.routes.append do - get "/exception", to: "hello#exception" - get "/view_exception", to: "hello#view_exception" - get "/view", to: "hello#view" - get "/not_found", to: "hello#not_found" - get "/world", to: "hello#world" - get "/with_custom_instrumentation", to: "hello#with_custom_instrumentation" - resources :posts, only: [:index, :show] do - member do - get :attach + elapsed("eager_load_namespaces_clear") do + app.config.eager_load_namespaces.clear + end + + elapsed("configure_app") do + configure_app(app) + end + + elapsed("routes_append") do + app.routes.append do + get "/exception", to: "hello#exception" + get "/view_exception", to: "hello#view_exception" + get "/view", to: "hello#view" + get "/not_found", to: "hello#not_found" + get "/world", to: "hello#world" + get "/with_custom_instrumentation", to: "hello#with_custom_instrumentation" + resources :posts, only: [:index, :show] do + member do + get :attach + end end + get "500", to: "hello#reporting" + root to: "hello#world" end - get "500", to: "hello#reporting" - root to: "hello#world" end - app.initializer :configure_sentry do - Sentry.init do |config| - config.release = 'beta' - config.dsn = "http://12345:67890@sentry.localdomain:3000/sentry/42" - config.transport.transport_class = Sentry::DummyTransport - # for sending events synchronously - config.background_worker_threads = 0 - config.capture_exception_frame_locals = true - yield(config, app) if block_given? + elapsed("sentry_init") do + app.initializer :configure_sentry do + Sentry.init do |config| + config.release = 'beta' + config.dsn = "http://12345:67890@sentry.localdomain:3000/sentry/42" + config.transport.transport_class = Sentry::DummyTransport + # for sending events synchronously + config.background_worker_threads = 0 + config.capture_exception_frame_locals = true + yield(config, app) if block_given? + end end end - app.initialize! + elapsed("initialize") do + app.initialize! + end - Rails.application = app + elapsed("rails_application_set") do + Rails.application = app + end - # load application code for the Rails version - require "dummy/test_rails_app/apps/#{FILE_NAME}" + elapsed("require_app_code") do + # load application code for the Rails version + require "dummy/test_rails_app/apps/#{FILE_NAME}" + end - Post.all.to_a # to run the sqlte version query first + elapsed("post_all") do + Post.all.to_a # to run the sqlte version query first + end - # and then clear breadcrumbs in case the above query is recorded - Sentry.get_current_scope.clear_breadcrumbs if Sentry.initialized? + elapsed("clear_breadcrumbs") do + # and then clear breadcrumbs in case the above query is recorded + Sentry.get_current_scope.clear_breadcrumbs if Sentry.initialized? + end + + puts "\nElapsed Time Summary:\n\n" + RECORDED_ELAPSED.each do |label, total| + puts "#{label}: %.8f seconds" % total + end + puts "\n" + "*" * 80 + puts "\nTotal Elapsed Time: %.8f seconds\n" % RECORDED_ELAPSED.values.sum + puts "\n" + "*" * 80 app end + +def elapsed(label) + start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) + yield + stop = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) + elapsed = (stop - start) / 1_000_000_000.0 + RECORDED_ELAPSED[label] += elapsed + elapsed +end + +RECORDED_ELAPSED = Hash.new { |h, k| h[k] = 0 } From d8bf4c19ae3d9e4942c7b500b4cc2f68da5cac13 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Thu, 17 Oct 2024 10:56:41 +0200 Subject: [PATCH 2/3] WIP --- sentry-rails/spec/dummy/test_rails_app/app.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/sentry-rails/spec/dummy/test_rails_app/app.rb b/sentry-rails/spec/dummy/test_rails_app/app.rb index a8435d2a8..696507694 100644 --- a/sentry-rails/spec/dummy/test_rails_app/app.rb +++ b/sentry-rails/spec/dummy/test_rails_app/app.rb @@ -11,7 +11,6 @@ require 'sentry/rails' require 'tempfile' -require 'vernier' ActiveRecord::Base.logger = Logger.new(nil) ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: "db") From 45395cb63fd12d699aadd729e2e6f4f8048f344d Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Thu, 17 Oct 2024 10:59:06 +0200 Subject: [PATCH 3/3] WIP --- sentry-rails/spec/dummy/test_rails_app/app.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry-rails/spec/dummy/test_rails_app/app.rb b/sentry-rails/spec/dummy/test_rails_app/app.rb index 696507694..5f4f6a4e3 100644 --- a/sentry-rails/spec/dummy/test_rails_app/app.rb +++ b/sentry-rails/spec/dummy/test_rails_app/app.rb @@ -64,7 +64,7 @@ def self.name app.config.hosts = nil app.config.secret_key_base = "test" app.config.logger = ActiveSupport::Logger.new(nil) - app.config.eager_load = false + app.config.eager_load = true app.config.active_job.queue_adapter = :test end