-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Performance improvements and bug fixes to TTL::Cache.
Added tests and benchmarks for TTL caches. Added timecop as a development dependency. Updated readme with TTL benchmarks and changelog. Updated authors and emails in gemspec.
- Loading branch information
Seberius
committed
Mar 29, 2015
1 parent
e4f4b6d
commit 0a53cea
Showing
9 changed files
with
208 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,45 +1,43 @@ | ||
require 'bundler' | ||
require 'lru' | ||
require 'benchmark' | ||
require 'lru' | ||
require 'lru_cache' | ||
require 'threadsafe-lru' | ||
|
||
Bundler.require | ||
|
||
lru = Cache::LRU.new(:max_elements => 1_000) | ||
# Lru | ||
lru = Cache::LRU.new(max_elements: 1_000) | ||
|
||
# LruCache | ||
lru_cache = LRUCache.new(1_000) | ||
|
||
lru_redux = LruRedux::Cache.new(1_000) | ||
lru_redux_thread_safe = LruRedux::ThreadSafeCache.new(1_000) | ||
# ThreadSafeLru | ||
thread_safe_lru = ThreadSafeLru::LruCache.new(1_000) | ||
|
||
# LruRedux | ||
redux = LruRedux::Cache.new(1_000) | ||
redux_thread_safe = LruRedux::ThreadSafeCache.new(1_000) | ||
|
||
puts "** LRU Benchmarks **" | ||
Benchmark.bmbm do |bm| | ||
bm.report 'ThreadSafeLru' do | ||
1_000_000.times { thread_safe_lru.get(rand(2_000)) { :value } } | ||
end | ||
|
||
bm.report 'LRU' do | ||
1_000_000.times { lru[rand(2_000)] ||= :value } | ||
end | ||
|
||
bm.report "thread safe lru" do | ||
1_000_000.times do | ||
thread_safe_lru.get(rand(2_000)){ :value } | ||
end | ||
bm.report 'LRUCache' do | ||
1_000_000.times { lru_cache[rand(2_000)] ||= :value } | ||
end | ||
|
||
[ | ||
[lru, "lru gem"], | ||
[lru_cache, "lru_cache gem"], | ||
].each do |cache, name| | ||
bm.report name do | ||
1_000_000.times do | ||
cache[rand(2_000)] ||= :value | ||
end | ||
end | ||
bm.report 'LruRedux::Cache' do | ||
1_000_000.times { redux.getset(rand(2_000)) { :value } } | ||
end | ||
|
||
[ | ||
[lru_redux, "lru_redux gem"], | ||
[lru_redux_thread_safe, "lru_redux thread safe"] | ||
].each do |cache, name| | ||
bm.report name do | ||
1_000_000.times do | ||
cache.getset(rand(2_000)) { :value } | ||
end | ||
end | ||
bm.report 'LruRedux::ThreadSafeCache' do | ||
1_000_000.times { redux_thread_safe.getset(rand(2_000)) { :value } } | ||
end | ||
end |
This file contains 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,33 @@ | ||
require 'bundler' | ||
require 'benchmark' | ||
require 'fast_cache' | ||
|
||
Bundler.require | ||
|
||
# FastCache | ||
fast_cache = FastCache::Cache.new(1_000, 5 * 60) | ||
|
||
# LruRedux | ||
redux_ttl = LruRedux::TTL::Cache.new(1_000, 5 * 60) | ||
redux_ttl_thread_safe = LruRedux::TTL::ThreadSafeCache.new(1_000, 5 * 60) | ||
redux_ttl_disabled = LruRedux::TTL::Cache.new(1_000, :none) | ||
|
||
puts | ||
puts "** TTL Benchmarks **" | ||
Benchmark.bmbm do |bm| | ||
bm.report 'FastCache' do | ||
1_000_000.times { fast_cache.fetch(rand(2_000)) { :value } } | ||
end | ||
|
||
bm.report 'LruRedux::TTL::Cache' do | ||
1_000_000.times { redux_ttl.getset(rand(2_000)) { :value } } | ||
end | ||
|
||
bm.report 'LruRedux::TTL::ThreadSafeCache' do | ||
1_000_000.times { redux_ttl_thread_safe.getset(rand(2_000)) { :value } } | ||
end | ||
|
||
bm.report 'LruRedux::TTL::Cache (TTL disabled)' do | ||
1_000_000.times { redux_ttl_disabled.getset(rand(2_000)) { :value } } | ||
end | ||
end |
This file contains 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 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 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,8 +6,8 @@ require 'lru_redux/version' | |
Gem::Specification.new do |spec| | ||
spec.name = "lru_redux" | ||
spec.version = LruRedux::VERSION | ||
spec.authors = ["Sam Saffron"] | ||
spec.email = ["[email protected]"] | ||
spec.authors = ["Sam Saffron", "Kaijah Hougham"] | ||
spec.email = ["[email protected]", "[email protected]"] | ||
spec.description = %q{An efficient implementation of an lru cache} | ||
spec.summary = %q{An efficient implementation of an lru cache} | ||
spec.homepage = "https://github.com/SamSaffron/lru_redux" | ||
|
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec| | |
spec.add_development_dependency "guard-minitest" | ||
spec.add_development_dependency "guard" | ||
spec.add_development_dependency "rb-inotify" | ||
spec.add_development_dependency "timecop", "~> 0.7" | ||
end |
This file contains 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,90 @@ | ||
require 'timecop' | ||
|
||
class TTLCacheTest < CacheTest | ||
def setup | ||
Timecop.freeze(Time.now) | ||
@c = LruRedux::TTL::Cache.new 3, 5 * 60 | ||
end | ||
|
||
def teardown | ||
Timecop.return | ||
end | ||
|
||
def test_ttl | ||
assert_equal 300, @c.ttl | ||
|
||
@c.ttl = 10 * 60 | ||
|
||
assert_equal 600, @c.ttl | ||
end | ||
|
||
# TTL tests using Timecop | ||
def test_ttl_eviction_on_access | ||
@c[:a] = 1 | ||
@c[:b] = 2 | ||
|
||
Timecop.freeze(Time.now + 330) | ||
|
||
@c[:c] = 3 | ||
|
||
assert_equal([[:c, 3]], @c.to_a) | ||
end | ||
|
||
def test_ttl_eviction_on_expire | ||
@c[:a] = 1 | ||
@c[:b] = 2 | ||
|
||
Timecop.freeze(Time.now + 330) | ||
|
||
@c.expire | ||
|
||
assert_equal([], @c.to_a) | ||
end | ||
|
||
def test_ttl_eviction_on_new_max_size | ||
@c[:a] = 1 | ||
@c[:b] = 2 | ||
|
||
Timecop.freeze(Time.now + 330) | ||
|
||
@c.max_size = 10 | ||
|
||
assert_equal([], @c.to_a) | ||
end | ||
|
||
def test_ttl_eviction_on_new_ttl | ||
@c[:a] = 1 | ||
@c[:b] = 2 | ||
|
||
Timecop.freeze(Time.now + 330) | ||
|
||
@c.ttl = 10 * 60 | ||
|
||
assert_equal([[:b, 2], [:a, 1]], @c.to_a) | ||
|
||
@c.ttl = 2 * 60 | ||
|
||
assert_equal([], @c.to_a) | ||
end | ||
|
||
def test_ttl_precedence_over_lru | ||
@c[:a] = 1 | ||
|
||
Timecop.freeze(Time.now + 60) | ||
|
||
@c[:b] = 2 | ||
@c[:c] = 3 | ||
|
||
@c[:a] | ||
|
||
assert_equal [[:a, 1], [:c, 3], [:b, 2]], | ||
@c.to_a | ||
|
||
Timecop.freeze(Time.now + 270) | ||
|
||
@c[:d] = 4 | ||
|
||
assert_equal [[:d, 4], [:c, 3], [:b, 2]], | ||
@c.to_a | ||
end | ||
end |
This file contains 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,20 @@ | ||
class TTLThreadSafeCacheTest < TTLCacheTest | ||
def setup | ||
Timecop.freeze(Time.now) | ||
@c = LruRedux::TTL::ThreadSafeCache.new 3, 5 * 60 | ||
end | ||
|
||
def teardown | ||
Timecop.return | ||
end | ||
|
||
def test_recursion | ||
@c[:a] = 1 | ||
@c[:b] = 2 | ||
|
||
# should not blow up | ||
@c.each do |k, _| | ||
@c[k] | ||
end | ||
end | ||
end |