aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-unstable/utils/srandmember
diff options
context:
space:
mode:
Diffstat (limited to 'examples/redis-unstable/utils/srandmember')
-rw-r--r--examples/redis-unstable/utils/srandmember/README.md14
-rw-r--r--examples/redis-unstable/utils/srandmember/showdist.rb33
-rw-r--r--examples/redis-unstable/utils/srandmember/showfreq.rb23
3 files changed, 70 insertions, 0 deletions
diff --git a/examples/redis-unstable/utils/srandmember/README.md b/examples/redis-unstable/utils/srandmember/README.md
new file mode 100644
index 0000000..d3da1e8
--- /dev/null
+++ b/examples/redis-unstable/utils/srandmember/README.md
@@ -0,0 +1,14 @@
1The utilities in this directory plot the distribution of SRANDMEMBER to
2evaluate how fair it is.
3
4See http://theshfl.com/redis_sets for more information on the topic that lead
5to such investigation fix.
6
7showdist.rb -- shows the distribution of the frequency elements are returned.
8 The x axis is the number of times elements were returned, and
9 the y axis is how many elements were returned with such
10 frequency.
11
12showfreq.rb -- shows the frequency each element was returned.
13 The x axis is the element number.
14 The y axis is the times it was returned.
diff --git a/examples/redis-unstable/utils/srandmember/showdist.rb b/examples/redis-unstable/utils/srandmember/showdist.rb
new file mode 100644
index 0000000..2435857
--- /dev/null
+++ b/examples/redis-unstable/utils/srandmember/showdist.rb
@@ -0,0 +1,33 @@
1require 'redis'
2
3r = Redis.new
4r.select(9)
5r.del("myset");
6r.sadd("myset",(0..999).to_a)
7freq = {}
8100.times {
9 res = r.pipelined {
10 1000.times {
11 r.srandmember("myset")
12 }
13 }
14 res.each{|ele|
15 freq[ele] = 0 if freq[ele] == nil
16 freq[ele] += 1
17 }
18}
19
20# Convert into frequency distribution
21dist = {}
22freq.each{|item,count|
23 dist[count] = 0 if dist[count] == nil
24 dist[count] += 1
25}
26
27min = dist.keys.min
28max = dist.keys.max
29(min..max).each{|x|
30 count = dist[x]
31 count = 0 if count == nil
32 puts "#{x} -> #{"*"*count}"
33}
diff --git a/examples/redis-unstable/utils/srandmember/showfreq.rb b/examples/redis-unstable/utils/srandmember/showfreq.rb
new file mode 100644
index 0000000..625519c
--- /dev/null
+++ b/examples/redis-unstable/utils/srandmember/showfreq.rb
@@ -0,0 +1,23 @@
1require 'redis'
2
3r = Redis.new
4r.select(9)
5r.del("myset");
6r.sadd("myset",(0..999).to_a)
7freq = {}
8500.times {
9 res = r.pipelined {
10 1000.times {
11 r.srandmember("myset")
12 }
13 }
14 res.each{|ele|
15 freq[ele] = 0 if freq[ele] == nil
16 freq[ele] += 1
17 }
18}
19
20# Print the frequency each element was yield to process it with gnuplot
21freq.each{|item,count|
22 puts "#{item} #{count}"
23}