]> git.saurik.com Git - redis.git/blob - tests/unit/sort.tcl
Make SORT tests use both ziplists and linked lists as input
[redis.git] / tests / unit / sort.tcl
1 start_server {
2 tags {"sort"}
3 overrides {
4 "list-max-ziplist-value" 16
5 "list-max-ziplist-entries" 32
6 }
7 } {
8 proc create_random_dataset {num cmd} {
9 set tosort {}
10 set result {}
11 array set seenrand {}
12 r del tosort
13 for {set i 0} {$i < $num} {incr i} {
14 # Make sure all the weights are different because
15 # Redis does not use a stable sort but Tcl does.
16 while 1 {
17 randpath {
18 set rint [expr int(rand()*1000000)]
19 } {
20 set rint [expr rand()]
21 }
22 if {![info exists seenrand($rint)]} break
23 }
24 set seenrand($rint) x
25 r $cmd tosort $i
26 r set weight_$i $rint
27 r hset wobj_$i weight $rint
28 lappend tosort [list $i $rint]
29 }
30 set sorted [lsort -index 1 -real $tosort]
31 for {set i 0} {$i < $num} {incr i} {
32 lappend result [lindex $sorted $i 0]
33 }
34 set _ $result
35 }
36
37 foreach {num cmd enc title} {
38 16 lpush ziplist "Ziplist"
39 64 lpush linkedlist "Linked list"
40 } {
41 set result [create_random_dataset $num $cmd]
42 assert_encoding $enc tosort
43
44 test "$title: SORT BY key" {
45 assert_equal $result [r sort tosort {BY weight_*}]
46 }
47
48 test "$title: SORT BY hash field" {
49 assert_equal $result [r sort tosort {BY wobj_*->weight}]
50 }
51 }
52
53 set result [create_random_dataset 16 lpush]
54 test "SORT GET #" {
55 assert_equal [lsort -integer $result] [r sort tosort GET #]
56 }
57
58 test "SORT GET <const>" {
59 r del foo
60 set res [r sort tosort GET foo]
61 assert_equal 16 [llength $res]
62 foreach item $res { assert_equal {} $item }
63 }
64
65 test "SORT GET (key and hash) with sanity check" {
66 set l1 [r sort tosort GET # GET weight_*]
67 set l2 [r sort tosort GET # GET wobj_*->weight]
68 foreach {id1 w1} $l1 {id2 w2} $l2 {
69 assert_equal $id1 $id2
70 assert_equal $w1 [r get weight_$id1]
71 assert_equal $w2 [r get weight_$id1]
72 }
73 }
74
75 test "SORT BY key STORE" {
76 r sort tosort {BY weight_*} store sort-res
77 assert_equal $result [r lrange sort-res 0 -1]
78 assert_equal 16 [r llen sort-res]
79 assert_encoding ziplist sort-res
80 }
81
82 test "SORT BY hash field STORE" {
83 r sort tosort {BY wobj_*->weight} store sort-res
84 assert_equal $result [r lrange sort-res 0 -1]
85 assert_equal 16 [r llen sort-res]
86 assert_encoding ziplist sort-res
87 }
88
89 test "SORT DESC" {
90 assert_equal [lsort -decreasing -integer $result] [r sort tosort {DESC}]
91 }
92
93 test "SORT ALPHA against integer encoded strings" {
94 r del mylist
95 r lpush mylist 2
96 r lpush mylist 1
97 r lpush mylist 3
98 r lpush mylist 10
99 r sort mylist alpha
100 } {1 10 2 3}
101
102 test "SORT sorted set" {
103 r del zset
104 r zadd zset 1 a
105 r zadd zset 5 b
106 r zadd zset 2 c
107 r zadd zset 10 d
108 r zadd zset 3 e
109 r sort zset alpha desc
110 } {e d c b a}
111
112 test "SORT sorted set: +inf and -inf handling" {
113 r del zset
114 r zadd zset -100 a
115 r zadd zset 200 b
116 r zadd zset -300 c
117 r zadd zset 1000000 d
118 r zadd zset +inf max
119 r zadd zset -inf min
120 r zrange zset 0 -1
121 } {min c a b d max}
122
123 test "SORT regression for issue #19, sorting floats" {
124 r flushdb
125 set floats {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}
126 foreach x $floats {
127 r lpush mylist $x
128 }
129 assert_equal [lsort -real $floats] [r sort mylist]
130 }
131
132 tags {"slow"} {
133 set num 100
134 set res [create_random_dataset $num lpush]
135
136 test "SORT speed, $num element list BY key, 100 times" {
137 set start [clock clicks -milliseconds]
138 for {set i 0} {$i < 100} {incr i} {
139 set sorted [r sort tosort {BY weight_* LIMIT 0 10}]
140 }
141 set elapsed [expr [clock clicks -milliseconds]-$start]
142 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
143 flush stdout
144 }
145
146 test "SORT speed, $num element list BY hash field, 100 times" {
147 set start [clock clicks -milliseconds]
148 for {set i 0} {$i < 100} {incr i} {
149 set sorted [r sort tosort {BY wobj_*->weight LIMIT 0 10}]
150 }
151 set elapsed [expr [clock clicks -milliseconds]-$start]
152 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
153 flush stdout
154 }
155
156 test "SORT speed, $num element list directly, 100 times" {
157 set start [clock clicks -milliseconds]
158 for {set i 0} {$i < 100} {incr i} {
159 set sorted [r sort tosort {LIMIT 0 10}]
160 }
161 set elapsed [expr [clock clicks -milliseconds]-$start]
162 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
163 flush stdout
164 }
165
166 test "SORT speed, $num element list BY <const>, 100 times" {
167 set start [clock clicks -milliseconds]
168 for {set i 0} {$i < 100} {incr i} {
169 set sorted [r sort tosort {BY nokey LIMIT 0 10}]
170 }
171 set elapsed [expr [clock clicks -milliseconds]-$start]
172 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
173 flush stdout
174 }
175 }
176 }