Created
July 10, 2024 14:34
-
-
Save iurev/cfb58955c03a31e830ad0794419d576d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [{"role":"user","content":"You are an experienced Ruby on Rails developer, very accurate for details who loves optimizing the performance of the tests.\nThe last 10 years you've spent optimizing performance of _spec.rb files using TestProf library.\n\nYou must keep your answers very short, concise, simple and informative.\n\nYou have been asked to refactor a test file to improve its performance.\nYou should use TestProf's let_it_be, before_all features\nYou must be very careful and introduce changes only when they are absolutely neccessary and will bring real performance improvements (don't over-optimize).\nAgain, make only minimal and essential changes to the existing code.\nYou MUST always send me back the whole file even if some parts of it didn't change.\n\nUse the following example refactoring as a guide:\n\nGit diff with refactoring:\n\ndiff --git a/spec/controllers/statuses_controller_spec.rb b/spec/controllers/statuses_controller_spec.rb\nindex fe40ee6..122de41 100644\n--- a/spec/controllers/statuses_controller_spec.rb\n+++ b/spec/controllers/statuses_controller_spec.rb\n@@ -2,13 +2,12 @@\n \n require 'rails_helper'\n \n-describe StatusesController do\n+describe StatusesController, :account do\n render_views\n \n- describe 'GET #show' do\n- let(:account) { Fabricate(:account) }\n- let(:status) { Fabricate(:status, account: account) }\n+ let_it_be(:status) { Fabricate(:status, account: account) }\n \n+ describe 'GET #show' do\n context 'when account is permanently suspended' do\n before do\n account.suspend!\n@@ -89,7 +88,7 @@ describe StatusesController do\n end\n \n context 'when status is private' do\n- let(:status) { Fabricate(:status, account: account, visibility: :private) }\n+ before { status.update!(visibility: :private) }\n \n before do\n get :show, params: { account_username: status.account.username, id: status.id, format: format }\n@@ -113,7 +112,7 @@ describe StatusesController do\n end\n \n context 'when status is direct' do\n- let(:status) { Fabricate(:status, account: account, visibility: :direct) }\n+ before { status.update!(visibility: :direct) }\n \n before do\n get :show, params: { account_username: status.account.username, id: status.id, format: format }\n@@ -136,9 +135,7 @@ describe StatusesController do\n end\n end\n \n- context 'when signed-in' do\n- let(:user) { Fabricate(:user) }\n-\n+ context 'when signed-in', :user do\n before do\n sign_in(user)\n end\n@@ -194,7 +191,7 @@ describe StatusesController do\n end\n \n context 'when status is private' do\n- let(:status) { Fabricate(:status, account: account, visibility: :private) }\n+ before { status.update!(visibility: :private) }\n \n context 'when user is authorized to see it' do\n before do\n@@ -261,7 +258,7 @@ describe StatusesController do\n end\n \n context 'when status is direct' do\n- let(:status) { Fabricate(:status, account: account, visibility: :direct) }\n+ before { status.update!(visibility: :direct) }\n \n context 'when user is authorized to see it' do\n before do\n@@ -328,7 +325,7 @@ describe StatusesController do\n end\n \n context 'with signature' do\n- let(:remote_account) { Fabricate(:account, domain: 'example.com') }\n+ let_it_be(:remote_account) { Fabricate(:account, domain: 'example.com') }\n \n before do\n allow(controller).to receive(:signed_request_actor).and_return(remote_account)\n@@ -397,7 +394,7 @@ describe StatusesController do\n end\n \n context 'when status is private' do\n- let(:status) { Fabricate(:status, account: account, visibility: :private) }\n+ before { status.update!(visibility: :private) }\n \n context 'when user is authorized to see it' do\n before do\n@@ -464,7 +461,7 @@ describe StatusesController do\n end\n \n context 'when status is direct' do\n- let(:status) { Fabricate(:status, account: account, visibility: :direct) }\n+ before { status.update!(visibility: :direct) }\n \n context 'when user is authorized to see it' do\n before do\n@@ -531,9 +528,8 @@ describe StatusesController do\n end\n end\n \n- describe 'GET #activity' do\n- let(:account) { Fabricate(:account) }\n- let(:status) { Fabricate(:status, account: account) }\n+ describe 'GET #activity', :account do\n+ let_it_be(:status) { Fabricate(:status, account: account) }\n \n context 'when account is permanently suspended' do\n before do\n@@ -593,9 +589,7 @@ describe StatusesController do\n end\n end\n \n- context 'when signed-in' do\n- let(:user) { Fabricate(:user) }\n-\n+ context 'when signed-in', :user do\n before do\n sign_in(user)\n end\n\n\nInitial performance data of the file you have to optimize:\n\n[TEST PROF INFO] FactoryProf enabled (simple mode)\n[TEST PROF INFO] RSpecDissect enabled\n\nRun options: exclude {:search=>true, :type=>#<Proc: ./spec/rails_helper.rb:82>}\n\nRandomized with seed 1865\n\nProgress: |================================================================================================================================|\n[TEST PROF INFO] RSpecDissect report\n\nTotal time: 00:00.500\n\nTotal `let` time: 00:00.475\nTotal `before(:each)` time: 00:00.494\n\nTop 5 slowest suites (by `let` time):\n\nVacuum::AccessTokensVacuum (./spec/lib/vacuum/access_tokens_vacuum_spec.rb:5) – 00:00.475 of 00:00.500 (6)\n ↳ revoked_access_token – 7\n ↳ expired_access_token – 7\n ↳ active_access_token – 7\n\nTop 5 slowest suites (by `before(:each)` time):\n\nVacuum::AccessTokensVacuum (./spec/lib/vacuum/access_tokens_vacuum_spec.rb:5) – 00:00.494 of 00:00.500 (6)\n\n\nFinished in 0.5508 seconds (files took 1.06 seconds to load)\n6 examples, 0 failures\n\nRandomized with seed 1865\n[TEST PROF INFO] Factories usage\n\n Total: 72\n Total top-level: 36\n Total time: 00:00.474 (out of 00:01.009)\n Total uniq factories: 4\n\n total top-level total time time per call top-level time name\n\n 18 18 0.0471s 0.0026s 0.0471s access_token\n 18 18 0.4274s 0.0237s 0.4274s access_grant\n 18 0 0.0152s 0.0008s 0.0000s application\n 18 0 0.3935s 0.0219s 0.0000s user\n\n\nYou run in a loop of Thought, Action, PAUSE, Observation.\nAt the end of the loop you may output an Answer or re-enter the loop if you're not satisifed with the Observation.\nUse Thought to describe your thoughts about the question you have been asked.\nUse Action to run one of the actions available to you - then return PAUSE and stop.\nObservation will be the result of running those actions.\n\nEvery step of the loop MUST start with the corresponding keyword (Question, Thought, Action, PAUSE, Observation, Answer) followed by a colon and a space.\n\nThe Action keyword is only followed by the action name; the action payload goes on the next lines.\nThe action payload MUST end with the __END__ keyword.\n\nYour available actions are:\n\nrun_rspec:\n\nExample (it's a multiline action):\n\nAction: run_rspec\n<Ruby RSpec code>\n__END__\n\nRuns the given test contents and returns the output containing RSpec run information (including failures) and TestProf profiling information (FactoryProf, RSpecDissect, etc.).\nThe test contents MUST be the entire file contents, including the test class and any required files; all original scenarios MUST be preserved.\nAgain, you MUST always send me back the whole file even if some parts of it didn't change.\n\nExample session (the format must be exactly like the one below):\n\n Question: Optimize this test file:\n <file contents>\n\n Thought: This file is slow because it uses a lot of let!. I should replace them with let_it_be.\n\n Action: run_rspec\n <new file contents>\n __END__\n\n PAUSE\n\nStop and wait for the next request. You will be called again with this:\n\n Observation:\n\n [TEST PROF INFO] Factories usage\n\n Total: 663\n Total top-level: 319\n Total time: 00:01.612 (out of 00:01.773)\n Total uniq factories: 13\n\n Finished in 1.73 seconds (files took 9.27 seconds to load)\n 33 examples, 0 failures\n\nYou then output:\n\n Answer: It's a 4x speedup! I'm happy with the results. \n\nOptimize this test file:\n\n # frozen_string_literal: true\n\nrequire 'rails_helper'\n\nRSpec.describe Vacuum::AccessTokensVacuum do\n subject { described_class.new }\n\n describe '#perform' do\n let!(:revoked_access_token) { Fabricate(:access_token, revoked_at: 1.minute.ago) }\n let!(:expired_access_token) { Fabricate(:access_token, expires_in: 59.minutes.to_i, created_at: 1.hour.ago) }\n let!(:active_access_token) { Fabricate(:access_token) }\n\n let!(:revoked_access_grant) { Fabricate(:access_grant, revoked_at: 1.minute.ago) }\n let!(:expired_access_grant) { Fabricate(:access_grant, expires_in: 59.minutes.to_i, created_at: 1.hour.ago) }\n let!(:active_access_grant) { Fabricate(:access_grant) }\n\n before do\n subject.perform\n end\n\n it 'deletes revoked access tokens' do\n expect { revoked_access_token.reload }.to raise_error ActiveRecord::RecordNotFound\n end\n\n it 'deletes expired access tokens' do\n expect { expired_access_token.reload }.to raise_error ActiveRecord::RecordNotFound\n end\n\n it 'deletes revoked access grants' do\n expect { revoked_access_grant.reload }.to raise_error ActiveRecord::RecordNotFound\n end\n\n it 'deletes expired access grants' do\n expect { expired_access_grant.reload }.to raise_error ActiveRecord::RecordNotFound\n end\n\n it 'does not delete active access tokens' do\n expect { active_access_token.reload }.to_not raise_error\n end\n\n it 'does not delete active access grants' do\n expect { active_access_grant.reload }.to_not raise_error\n end\n end\nend\n"},{"role":"assistant","content":"Thought: The file uses let! for creating test data, which can be slow. We can optimize it by using let_it_be for objects that don't change across examples.\n\nAction: run_rspec\n# frozen_string_literal: true\n\nrequire 'rails_helper'\n\nRSpec.describe Vacuum::AccessTokensVacuum do\n subject { described_class.new }\n\n describe '#perform' do\n let_it_be(:revoked_access_token) { Fabricate(:access_token, revoked_at: 1.minute.ago) }\n let_it_be(:expired_access_token) { Fabricate(:access_token, expires_in: 59.minutes.to_i, created_at: 1.hour.ago) }\n let_it_be(:active_access_token) { Fabricate(:access_token) }\n\n let_it_be(:revoked_access_grant) { Fabricate(:access_grant, revoked_at: 1.minute.ago) }\n let_it_be(:expired_access_grant) { Fabricate(:access_grant, expires_in: 59.minutes.to_i, created_at: 1.hour.ago) }\n let_it_be(:active_access_grant) { Fabricate(:access_grant) }\n\n before do\n subject.perform\n end\n\n it 'deletes revoked access tokens' do\n expect { revoked_access_token.reload }.to raise_error ActiveRecord::RecordNotFound\n end\n\n it 'deletes expired access tokens' do\n expect { expired_access_token.reload }.to raise_error ActiveRecord::RecordNotFound\n end\n\n it 'deletes revoked access grants' do\n expect { revoked_access_grant.reload }.to raise_error ActiveRecord::RecordNotFound\n end\n\n it 'deletes expired access grants' do\n expect { expired_access_grant.reload }.to raise_error ActiveRecord::RecordNotFound\n end\n\n it 'does not delete active access tokens' do\n expect { active_access_token.reload }.to_not raise_error\n end\n\n it 'does not delete active access grants' do\n expect { active_access_grant.reload }.to_not raise_error\n end\n end\nend\n__END__\n\nPAUSE"},{"role":"user","content":"Observation:\n\n[TEST PROF INFO] FactoryProf enabled (simple mode)\n[TEST PROF INFO] RSpecDissect enabled\n\nRun options: exclude {:search=>true, :type=>#<Proc: ./spec/rails_helper.rb:82>}\n\nRandomized with seed 40993\n\nProgress: |================================================================================================================================|\n[TEST PROF INFO] RSpecDissect report\n\nTotal time: 00:00.020\n\nTotal `let` time: 00:00.000\nTotal `before(:each)` time: 00:00.014\n\nTop 5 slowest suites (by `let` time):\n\nVacuum::AccessTokensVacuum (./spec/lib/vacuum/access_tokens_vacuum_ai_suggest_1_spec.rb:5) – 00:00.000 of 00:00.020 (6)\n ↳ subject – 6\n ↳ active_access_grant – 1\n ↳ active_access_token – 1\n\nTop 5 slowest suites (by `before(:each)` time):\n\nVacuum::AccessTokensVacuum (./spec/lib/vacuum/access_tokens_vacuum_ai_suggest_1_spec.rb:5) – 00:00.014 of 00:00.020 (6)\n\n\nFinished in 0.40068 seconds (files took 1.08 seconds to load)\n6 examples, 0 failures\n\nRandomized with seed 40993\n[TEST PROF INFO] Factories usage\n\n Total: 12\n Total top-level: 6\n Total time: 00:00.317 (out of 00:00.931)\n Total uniq factories: 4\n\n total top-level total time time per call top-level time name\n\n 3 3 0.0727s 0.0242s 0.0727s access_token\n 3 3 0.2447s 0.0816s 0.2447s access_grant\n 3 0 0.0036s 0.0012s 0.0000s application\n 3 0 0.2345s 0.0782s 0.0000s user\n"},{"role":"assistant","content":"Answer: The optimization has significantly improved the test performance. The total time decreased from 0.5508 seconds to 0.40068 seconds, and the number of total factories created reduced from 72 to 12. This is a substantial improvement in both speed and resource usage. The `let_it_be` change has effectively reduced the number of times factories are called, leading to faster test execution. I'm satisfied with these results as they show a clear performance improvement without changing the test's behavior."}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment