Skip to content

Instantly share code, notes, and snippets.

@kotas
Created July 9, 2014 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kotas/60bbb916be173fb40b48 to your computer and use it in GitHub Desktop.
Save kotas/60bbb916be173fb40b48 to your computer and use it in GitHub Desktop.
Timeline using Redis sorted set
require "benchmark"
require "redis"
require "hiredis"
require "redis/connection/hiredis"
redis = Redis.new(timeout: 60)
redis.select(10)
if ARGV.include?("create")
puts "Creating feeds"
redis.flushdb
1000.times do |n|
50.times do
r = rand(1..10000)
redis.zadd "feed:#{n}", r, "Feed #{n}: #{r}"
end
end
exit
end
Benchmark.bm(4) do |bm|
[10, 100, 200, 400, 600, 800, 1000].each do |n_feed|
bm.report("#{n_feed}") do
follows = (0..n_feed).map { |i| "feed:#{i}" }
redis.zunionstore "timeline", follows
redis.zrevrange "timeline", 0, 50
end
end
end
On iMac Core i5 3.4GHz
user system total real
10 0.000000 0.000000 0.000000 ( 0.019222)
100 0.000000 0.000000 0.000000 ( 0.263000)
200 0.000000 0.000000 0.000000 ( 0.987301)
400 0.000000 0.000000 0.000000 ( 4.010479)
600 0.000000 0.000000 0.000000 ( 9.127258)
800 0.010000 0.000000 0.010000 ( 16.349298)
1000 0.000000 0.000000 0.000000 ( 25.671639)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment