Skip to content

Instantly share code, notes, and snippets.

@unarist
Created October 27, 2017 15:25
Show Gist options
  • Save unarist/ddb175f079d2322267a5ab706ce07d27 to your computer and use it in GitHub Desktop.
Save unarist/ddb175f079d2322267a5ab706ce07d27 to your computer and use it in GitHub Desktop.
#5538
namespace :fconv do
desc "Reset feeds"
task reset: :environment do
redis = Redis.current
keys = redis.keys('feed:*')
redis.del(keys) if keys.present?
puts "adding"
1000.times do |u|
key = FeedManager.instance.key(:home, u)
redis.pipelined do
FeedManager::MAX_ITEMS.times do |i|
reblog = i >= 300
redis.zadd(key, reblog ? i * 2 : i, i)
end
end
end
end
desc "Current implementation"
task cur: :environment do
redis = Redis.current
fm = FeedManager.instance
puts Benchmark::CAPTION
puts Benchmark.measure {
#User.includes(:account).find_each do |user|
# account = user.account
1000.times do |u|
account = Account.new(id: u)
timeline_key = fm.key(:home, account.id)
reblog_key = fm.key(:home, account.id, 'reblogs')
#redis.zrange(timeline_key, 0, -1, with_scores: true).each do |entry|
entries = redis.zrange(timeline_key, 0, -1, with_scores: true)
entries.each do |entry|
next if entry[0] == entry[1]
reblogged_id, reblogging_id = entry
redis.zrem(timeline_key, reblogged_id)
redis.zadd(timeline_key, reblogging_id, reblogging_id)
redis.zadd(reblog_key, reblogging_id, reblogged_id)
end
end
}
end
desc "Current implementation"
task script: :environment do
redis = Redis.current
fm = FeedManager.instance
script = <<-LUA
local timeline_key = KEYS[1]
local reblog_key = KEYS[2]
local items = redis.call('zrange', timeline_key, 0, -1, 'withscores')
for i = 1, #items, 2 do
local reblogged_id = items[i]
local reblogging_id = items[i + 1]
if (reblogged_id ~= reblogging_id) then
redis.call('zrem', timeline_key, reblogged_id)
redis.call('zadd', timeline_key, reblogging_id, reblogging_id)
redis.call('zadd', reblog_key, reblogging_id, reblogged_id)
end
end
LUA
script_hash = redis.script(:load, script)
puts Benchmark::CAPTION
puts Benchmark.measure {
#User.includes(:account).find_each do |user|
# account = user.account
#redis.pipelined do
1000.times do |u|
account = Account.new(id: u)
timeline_key = fm.key(:home, account.id)
reblog_key = fm.key(:home, account.id, 'reblogs')
redis.evalsha(script_hash, [timeline_key, reblog_key])
end
#end
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment