1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/test/cases/cache_key_test.rb

132 lines
4.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "cases/helper"
module ActiveRecord
class CacheKeyTest < ActiveRecord::TestCase
self.use_transactional_tests = false
2017-05-18 12:12:32 -04:00
class CacheMe < ActiveRecord::Base
self.cache_versioning = false
end
class CacheMeWithVersion < ActiveRecord::Base
self.cache_versioning = true
end
setup do
@connection = ActiveRecord::Base.connection
2017-05-18 12:12:32 -04:00
@connection.create_table(:cache_mes, force: true) { |t| t.timestamps }
@connection.create_table(:cache_me_with_versions, force: true) { |t| t.timestamps }
end
teardown do
@connection.drop_table :cache_mes, if_exists: true
2017-05-18 12:12:32 -04:00
@connection.drop_table :cache_me_with_versions, if_exists: true
end
2017-04-06 19:40:52 -04:00
test "cache_key format is not too precise" do
record = CacheMe.create
key = record.cache_key
assert_equal key, record.reload.cache_key
end
2017-05-18 12:12:32 -04:00
test "cache_key has no version when versioning is on" do
record = CacheMeWithVersion.create
assert_equal "active_record/cache_key_test/cache_me_with_versions/#{record.id}", record.cache_key
end
test "cache_version is only there when versioning is on" do
assert_predicate CacheMeWithVersion.create.cache_version, :present?
assert_not_predicate CacheMe.create.cache_version, :present?
2017-05-18 12:12:32 -04:00
end
test "cache_key_with_version always has both key and version" do
r1 = CacheMeWithVersion.create
Use raw time string from DB to generate ActiveRecord#cache_version Currently, the `updated_at` field is used to generate a `cache_version`. Some database adapters return this timestamp value as a string that must then be converted to a Time value. This process requires a lot of memory and even more CPU time. In the case where this value is only being used for a cache version, we can skip the Time conversion by using the string value directly. - This PR preserves existing cache format by converting a UTC string from the database to `:usec` format. - Some databases return an already converted Time object, in those instances, we can directly use `created_at`. - The `updated_at_before_type_cast` can be a value that comes from either the database or the user. We only want to optimize the case where it is from the database. - If the format of the cache version has been changed, we cannot apply this optimization, and it is skipped. - If the format of the time in the database is not UTC, then we cannot use this optimization, and it is skipped. Some databases (notably PostgreSQL) returns a variable length nanosecond value in the time string. If the value ends in a zero, then it is truncated For instance instead of `2018-10-12 05:00:00.000000` the value `2018-10-12 05:00:00` is returned. We detect this case and pad the remaining zeros to ensure consistent cache version generation. Before: Total allocated: 743842 bytes (6626 objects) After: Total allocated: 702955 bytes (6063 objects) (743842 - 702955) / 743842.0 # => 5.4% ⚡️⚡️⚡️⚡️⚡️ Using the CodeTriage application and derailed benchmarks this PR shows between 9-11% (statistically significant) performance improvement versus the commit before it. Special thanks to @lsylvester for helping to figure out a way to preserve the usec format and for helping with many implementation details.
2018-09-07 22:41:55 -04:00
assert_equal "active_record/cache_key_test/cache_me_with_versions/#{r1.id}-#{r1.updated_at.utc.to_s(:usec)}", r1.cache_key_with_version
r2 = CacheMe.create
Use raw time string from DB to generate ActiveRecord#cache_version Currently, the `updated_at` field is used to generate a `cache_version`. Some database adapters return this timestamp value as a string that must then be converted to a Time value. This process requires a lot of memory and even more CPU time. In the case where this value is only being used for a cache version, we can skip the Time conversion by using the string value directly. - This PR preserves existing cache format by converting a UTC string from the database to `:usec` format. - Some databases return an already converted Time object, in those instances, we can directly use `created_at`. - The `updated_at_before_type_cast` can be a value that comes from either the database or the user. We only want to optimize the case where it is from the database. - If the format of the cache version has been changed, we cannot apply this optimization, and it is skipped. - If the format of the time in the database is not UTC, then we cannot use this optimization, and it is skipped. Some databases (notably PostgreSQL) returns a variable length nanosecond value in the time string. If the value ends in a zero, then it is truncated For instance instead of `2018-10-12 05:00:00.000000` the value `2018-10-12 05:00:00` is returned. We detect this case and pad the remaining zeros to ensure consistent cache version generation. Before: Total allocated: 743842 bytes (6626 objects) After: Total allocated: 702955 bytes (6063 objects) (743842 - 702955) / 743842.0 # => 5.4% ⚡️⚡️⚡️⚡️⚡️ Using the CodeTriage application and derailed benchmarks this PR shows between 9-11% (statistically significant) performance improvement versus the commit before it. Special thanks to @lsylvester for helping to figure out a way to preserve the usec format and for helping with many implementation details.
2018-09-07 22:41:55 -04:00
assert_equal "active_record/cache_key_test/cache_mes/#{r2.id}-#{r2.updated_at.utc.to_s(:usec)}", r2.cache_key_with_version
end
test "cache_version is the same when it comes from the DB or from the user" do
skip("Mysql2 and PostgreSQL don't return a string value for updated_at") if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter)
Use raw time string from DB to generate ActiveRecord#cache_version Currently, the `updated_at` field is used to generate a `cache_version`. Some database adapters return this timestamp value as a string that must then be converted to a Time value. This process requires a lot of memory and even more CPU time. In the case where this value is only being used for a cache version, we can skip the Time conversion by using the string value directly. - This PR preserves existing cache format by converting a UTC string from the database to `:usec` format. - Some databases return an already converted Time object, in those instances, we can directly use `created_at`. - The `updated_at_before_type_cast` can be a value that comes from either the database or the user. We only want to optimize the case where it is from the database. - If the format of the cache version has been changed, we cannot apply this optimization, and it is skipped. - If the format of the time in the database is not UTC, then we cannot use this optimization, and it is skipped. Some databases (notably PostgreSQL) returns a variable length nanosecond value in the time string. If the value ends in a zero, then it is truncated For instance instead of `2018-10-12 05:00:00.000000` the value `2018-10-12 05:00:00` is returned. We detect this case and pad the remaining zeros to ensure consistent cache version generation. Before: Total allocated: 743842 bytes (6626 objects) After: Total allocated: 702955 bytes (6063 objects) (743842 - 702955) / 743842.0 # => 5.4% ⚡️⚡️⚡️⚡️⚡️ Using the CodeTriage application and derailed benchmarks this PR shows between 9-11% (statistically significant) performance improvement versus the commit before it. Special thanks to @lsylvester for helping to figure out a way to preserve the usec format and for helping with many implementation details.
2018-09-07 22:41:55 -04:00
record = CacheMeWithVersion.create
record_from_db = CacheMeWithVersion.find(record.id)
assert_not_called(record_from_db, :updated_at) do
record_from_db.cache_version
end
assert_equal record.cache_version, record_from_db.cache_version
end
test "cache_version does not truncate zeros when timestamp ends in zeros" do
skip("Mysql2 and PostgreSQL don't return a string value for updated_at") if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter)
Use raw time string from DB to generate ActiveRecord#cache_version Currently, the `updated_at` field is used to generate a `cache_version`. Some database adapters return this timestamp value as a string that must then be converted to a Time value. This process requires a lot of memory and even more CPU time. In the case where this value is only being used for a cache version, we can skip the Time conversion by using the string value directly. - This PR preserves existing cache format by converting a UTC string from the database to `:usec` format. - Some databases return an already converted Time object, in those instances, we can directly use `created_at`. - The `updated_at_before_type_cast` can be a value that comes from either the database or the user. We only want to optimize the case where it is from the database. - If the format of the cache version has been changed, we cannot apply this optimization, and it is skipped. - If the format of the time in the database is not UTC, then we cannot use this optimization, and it is skipped. Some databases (notably PostgreSQL) returns a variable length nanosecond value in the time string. If the value ends in a zero, then it is truncated For instance instead of `2018-10-12 05:00:00.000000` the value `2018-10-12 05:00:00` is returned. We detect this case and pad the remaining zeros to ensure consistent cache version generation. Before: Total allocated: 743842 bytes (6626 objects) After: Total allocated: 702955 bytes (6063 objects) (743842 - 702955) / 743842.0 # => 5.4% ⚡️⚡️⚡️⚡️⚡️ Using the CodeTriage application and derailed benchmarks this PR shows between 9-11% (statistically significant) performance improvement versus the commit before it. Special thanks to @lsylvester for helping to figure out a way to preserve the usec format and for helping with many implementation details.
2018-09-07 22:41:55 -04:00
travel_to Time.now.beginning_of_day do
record = CacheMeWithVersion.create
record_from_db = CacheMeWithVersion.find(record.id)
assert_not_called(record_from_db, :updated_at) do
record_from_db.cache_version
end
assert_equal record.cache_version, record_from_db.cache_version
end
end
test "cache_version calls updated_at when the value is generated at create time" do
record = CacheMeWithVersion.create
assert_called(record, :updated_at) do
record.cache_version
end
end
test "cache_version does NOT call updated_at when value is from the database" do
skip("Mysql2 and PostgreSQL don't return a string value for updated_at") if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter)
Use raw time string from DB to generate ActiveRecord#cache_version Currently, the `updated_at` field is used to generate a `cache_version`. Some database adapters return this timestamp value as a string that must then be converted to a Time value. This process requires a lot of memory and even more CPU time. In the case where this value is only being used for a cache version, we can skip the Time conversion by using the string value directly. - This PR preserves existing cache format by converting a UTC string from the database to `:usec` format. - Some databases return an already converted Time object, in those instances, we can directly use `created_at`. - The `updated_at_before_type_cast` can be a value that comes from either the database or the user. We only want to optimize the case where it is from the database. - If the format of the cache version has been changed, we cannot apply this optimization, and it is skipped. - If the format of the time in the database is not UTC, then we cannot use this optimization, and it is skipped. Some databases (notably PostgreSQL) returns a variable length nanosecond value in the time string. If the value ends in a zero, then it is truncated For instance instead of `2018-10-12 05:00:00.000000` the value `2018-10-12 05:00:00` is returned. We detect this case and pad the remaining zeros to ensure consistent cache version generation. Before: Total allocated: 743842 bytes (6626 objects) After: Total allocated: 702955 bytes (6063 objects) (743842 - 702955) / 743842.0 # => 5.4% ⚡️⚡️⚡️⚡️⚡️ Using the CodeTriage application and derailed benchmarks this PR shows between 9-11% (statistically significant) performance improvement versus the commit before it. Special thanks to @lsylvester for helping to figure out a way to preserve the usec format and for helping with many implementation details.
2018-09-07 22:41:55 -04:00
record = CacheMeWithVersion.create
record_from_db = CacheMeWithVersion.find(record.id)
assert_not_called(record_from_db, :updated_at) do
record_from_db.cache_version
end
end
test "cache_version does call updated_at when it is assigned via a Time object" do
record = CacheMeWithVersion.create
record_from_db = CacheMeWithVersion.find(record.id)
assert_called(record_from_db, :updated_at) do
record_from_db.updated_at = Time.now
record_from_db.cache_version
end
end
test "cache_version does call updated_at when it is assigned via a string" do
record = CacheMeWithVersion.create
record_from_db = CacheMeWithVersion.find(record.id)
assert_called(record_from_db, :updated_at) do
record_from_db.updated_at = Time.now.to_s
record_from_db.cache_version
end
end
test "cache_version does call updated_at when it is assigned via a hash" do
record = CacheMeWithVersion.create
record_from_db = CacheMeWithVersion.find(record.id)
assert_called(record_from_db, :updated_at) do
record_from_db.updated_at = { 1 => 2016, 2 => 11, 3 => 12, 4 => 1, 5 => 2, 6 => 3, 7 => 22 }
record_from_db.cache_version
end
end
test "updated_at on class but not on instance raises an error" do
record = CacheMeWithVersion.create
record_from_db = CacheMeWithVersion.where(id: record.id).select(:id).first
assert_raises(ActiveModel::MissingAttributeError) do
record_from_db.cache_version
end
end
end
end