Preload only uncached messages and reduce rendering overhead (#292)

* Preload only uncached messages and reduce rendering overhead

* Keep benchmark summaries without raw JSON results

* Use Ruby benchmark drivers and keep generated results out of the repo
This commit is contained in:
David Heinemeier Hansson
2026-10-04 12:36:36 -04:00
committed by GitHub
parent 90b330024d
commit 659f95748a
17 changed files with 572 additions and 24 deletions
+48
View File
@@ -0,0 +1,48 @@
require "test_helper"
require "active_record/testing/query_assertions"
class MessagesCachingTest < ActionDispatch::IntegrationTest
include ActiveRecord::Assertions::QueryAssertions
setup do
sign_in :david
end
test "cached pages skip presentation queries and refresh edited messages" do
with_memory_cache do
get room_messages_url(rooms(:watercooler))
assert_response :success
original = response.body
assert_no_queries_match(/action_text_rich_texts|active_storage_attachments|boosts/) do
get room_messages_url(rooms(:watercooler))
end
assert_response :success
assert_equal original, response.body
messages(:fourth).update! body: "Updated cached message"
get room_messages_url(rooms(:watercooler))
assert_response :success
assert_select "#" + dom_id(messages(:fourth)), text: /Updated cached message/
end
end
private
def with_memory_cache
old_cache = Rails.cache
old_collection_cache = ActionView::PartialRenderer.collection_cache
old_controller_cache = MessagesController.cache_store
old_caching = MessagesController.perform_caching
Rails.cache = ActiveSupport::Cache::MemoryStore.new
ActionView::PartialRenderer.collection_cache = Rails.cache
MessagesController.cache_store = Rails.cache
MessagesController.perform_caching = true
yield
ensure
Rails.cache = old_cache
ActionView::PartialRenderer.collection_cache = old_collection_cache
MessagesController.cache_store = old_controller_cache
MessagesController.perform_caching = old_caching
end
end
+39 -1
View File
@@ -1,7 +1,8 @@
require "test_helper"
require "active_record/testing/query_assertions"
class MessageTest < ActiveSupport::TestCase
include ActionCable::TestHelper, ActiveJob::TestHelper
include ActionCable::TestHelper, ActiveJob::TestHelper, ActiveRecord::Assertions::QueryAssertions
test "creating a message enqueues to push later" do
assert_enqueued_jobs 1, only: [ Room::PushMessageJob ] do
@@ -27,6 +28,43 @@ class MessageTest < ActiveSupport::TestCase
assert_equal [], message_mentioning_a_non_member.mentionees
end
test "presentation associations load together" do
messages(:first).attachment.attach io: StringIO.new("hello"), filename: "hello.txt", content_type: "text/plain"
presented = Message.where(id: [ messages(:first).id, messages(:thirteenth).id ]).with_presentation.to_a
assert_no_queries do
presented.each do |message|
message.room.name
message.creator.avatar.attached?
message.body.body.to_html
message.body.embeds.each(&:filename)
message.attachment.blob&.filename
message.boosts.each { |boost| boost.booster.avatar.attached? }
end
end
end
test "presentation pages preload only the rendered messages" do
page = rooms(:watercooler).messages.with_presentation.last_page
assert_kind_of Array, page
assert page.all? { |message| !message.association(:rich_text_body).loaded? }
rendered = page.first(2)
page.preload_associations(rendered)
assert_no_queries { rendered.each { |message| message.body.to_plain_text } }
assert page.drop(2).all? { |message| !message.association(:rich_text_body).loaded? }
end
test "pagination retains the original ordering even when timestamps tie" do
room = rooms(:watercooler)
room.messages.update_all(created_at: Time.current)
expected = room.messages.ordered.last(Message::Pagination::PAGE_SIZE).map(&:id)
assert_equal expected, room.messages.last_page.map(&:id)
assert_equal room.messages.ordered.first(Message::Pagination::PAGE_SIZE).map(&:id), room.messages.first_page.map(&:id)
end
private
def create_new_message_in(room)
room.messages.create!(creator: users(:jason), body: "Hello", client_message_id: "123")