-
Notifications
You must be signed in to change notification settings - Fork 0
[BAC-1484] Speed up Entity.represent for large nested payloads (~3-4x) #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
taleh007
wants to merge
2
commits into
master
Choose a base branch
from
feature/BAC-1484
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Entities used by the representation performance spec. | ||
| # | ||
| # Excluded from the default suite — run with: | ||
| # PERFORMANCE=1 bundle exec rspec spec/performance | ||
| # | ||
| # The graph mirrors a realistically "wide" payload: | ||
| # Catalog (root entity) | ||
| # └── products: array of 5000 Product entities, each with | ||
| # ├── regular fields (integer, string, float, boolean, datetime, array of strings) | ||
| # └── dimensions: a nested Dimensions entity with a few fields | ||
| module RepresentPerformanceTest | ||
| # How many nested entities the root entity carries. | ||
| NESTED_ENTITIES_COUNT = 5_000 | ||
|
|
||
| # Generous per-call budget for representing the whole catalog. A healthy run is | ||
| # ~200ms on a laptop; the budget only guards against catastrophic regressions | ||
| # (e.g. accidentally quadratic serialization) without flaking on slow hardware. | ||
| TIME_BUDGET_SECONDS = 2.0 | ||
|
|
||
| class Dimensions | ||
| include ActiveModel::Entity | ||
|
|
||
| attribute :width, :float | ||
| attribute :height, :float | ||
| attribute :unit, :string | ||
| end | ||
|
|
||
| class Product | ||
| include ActiveModel::Entity | ||
|
|
||
| attribute :id, :integer | ||
| attribute :name, :string | ||
| attribute :price, :float | ||
| attribute :in_stock, :boolean | ||
| attribute :created_at, :datetime | ||
| attribute :tags, :array, of: :string | ||
| attribute :dimensions, :entity, class_name: "RepresentPerformanceTest::Dimensions" | ||
| end | ||
|
|
||
| class Catalog | ||
| include ActiveModel::Entity | ||
|
|
||
| attribute :id, :integer | ||
| attribute :name, :string | ||
| attribute :products, :array, of: "RepresentPerformanceTest::Product" | ||
| end | ||
| end | ||
|
|
||
| RSpec.describe "ActiveModel::Entity representation performance", :performance do | ||
| let(:items_count) { RepresentPerformanceTest::NESTED_ENTITIES_COUNT } | ||
| let(:created_at) { Time.utc(2024, 6, 1, 12, 30) } | ||
|
|
||
| def represent_catalog(source) | ||
| RepresentPerformanceTest::Catalog.represent(source) | ||
| end | ||
|
|
||
| def measure_represent(source, iterations: 5, warmup: 2) | ||
| warmup.times { represent_catalog(source) } | ||
|
|
||
| Array.new(iterations) do | ||
| started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
| represent_catalog(source) | ||
| Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at | ||
| end | ||
| end | ||
|
|
||
| def report_timings(label, timings) | ||
| milliseconds = timings.map { _1 * 1000 } | ||
| average = milliseconds.sum / milliseconds.size | ||
| stats = "min #{milliseconds.min.round(1)}ms / avg #{average.round(1)}ms / max #{milliseconds.max.round(1)}ms over #{milliseconds.size} runs" | ||
|
|
||
| RSpec.configuration.reporter.message("Catalog.represent (#{label}, #{items_count} products): #{stats}") | ||
| end | ||
|
|
||
| shared_examples "a large catalog representation" do |label| | ||
| it "represents every nested entity" do | ||
| json = represent_catalog(source) | ||
|
|
||
| expect(json["products"].length).to eq(items_count) | ||
| expect(json["products"].first).to eq( | ||
| "id" => 0, | ||
| "name" => "Product 0", | ||
| "price" => 0.0, | ||
| "inStock" => true, | ||
| "createdAt" => created_at, | ||
| "tags" => %w[tag-0 featured], | ||
| "dimensions" => { "width" => 10.0, "height" => 20.0, "unit" => "cm" } | ||
| ) | ||
| end | ||
|
|
||
| it "stays within the time budget" do | ||
| timings = measure_represent(source) | ||
|
|
||
| report_timings(label, timings) | ||
|
|
||
| # The fastest run is the least noisy statistic on shared CI hardware. | ||
| expect(timings.min).to be < RepresentPerformanceTest::TIME_BUDGET_SECONDS | ||
| end | ||
| end | ||
|
|
||
| context "when representing a hash payload" do | ||
| let(:source) do | ||
| { | ||
| id: 1, | ||
| name: "Catalog", | ||
| products: Array.new(items_count) do |index| | ||
| { | ||
| id: index, | ||
| name: "Product #{index}", | ||
| price: index * 1.5, | ||
| in_stock: index.even?, | ||
| created_at:, | ||
| tags: ["tag-#{index % 10}", "featured"], | ||
| dimensions: { width: 10.0 + index, height: 20.0 + index, unit: "cm" } | ||
| } | ||
| end | ||
| } | ||
| end | ||
|
|
||
| include_examples "a large catalog representation", "hash payload" | ||
| end | ||
|
|
||
| context "when representing an entity object graph" do | ||
| let(:source) do | ||
| RepresentPerformanceTest::Catalog.new( | ||
| id: 1, | ||
| name: "Catalog", | ||
| products: Array.new(items_count) do |index| | ||
| RepresentPerformanceTest::Product.new( | ||
| id: index, | ||
| name: "Product #{index}", | ||
| price: index * 1.5, | ||
| in_stock: index.even?, | ||
| created_at:, | ||
| tags: ["tag-#{index % 10}", "featured"], | ||
| dimensions: RepresentPerformanceTest::Dimensions.new(width: 10.0 + index, height: 20.0 + index, unit: "cm") | ||
| ) | ||
| end | ||
| ) | ||
| end | ||
|
|
||
| include_examples "a large catalog representation", "entity object graph" | ||
| end | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,9 @@ | |
| # Enable flags like --only-failures and --next-failure | ||
| config.example_status_persistence_file_path = ".rspec_status" | ||
|
|
||
| # Performance specs are opt-in: PERFORMANCE=1 bundle exec rspec spec/performance | ||
| config.filter_run_excluding :performance unless ENV["PERFORMANCE"] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it take much to run? maybe make them on by default? |
||
|
|
||
| # Disable RSpec exposing methods globally on `Module` and `main` | ||
| config.disable_monkey_patching! | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are talking optimizations here, shall this “try this, then try that” be gone too?