我正在使用 Ruby 1.8.7 和 redis-rb gem。我尝试了各种方法将 100 万条记录写入 Redis 列表。
我注意到当我使用线程时性能会受到影响。这是什么原因,我该如何预防?我将在线程环境中使用 Redis 进行生产,所以我很担心。
这是我的结果:
1_000_000.times do
$r.rpush "test_list", rand(1_000_000)
end
# Took: 74.2986769676208 sec.
threads = []
1_000.times do
threads << Thread.new {
1_000.times {
$r.rpush "test_list", rand(1_000_000)
}
}
end
threads.each { |k| k.join }
# Took: 391.705540895462 sec.
mutex = Mutex.new
threads = []
1_000.times do
threads << Thread.new {
1_000.times {
mutex.synchronize {
$r.rpush "test_list", rand(1_000_000)
}
}
}
end
threads.each { |k| k.join }
# Took: 308.474660873413 sec.
mutex = Mutex.new
threads = []
10.times do
threads << Thread.new {
100_000.times {
mutex.synchronize {
$r.rpush "test_list", rand(1_000_000)
}
}
}
end
threads.each { |k| k.join }
# Took: 109.103996992111 sec.
forks = []
8.times do
forks << fork {
125_000.times {
$r.rpush "test_list", rand(1_000_000)
}
}
end
forks.each { |k| Process.wait(k) }
# Took: 23.7934968471527 sec.