4 "list-max-ziplist-value" 16
5 "list-max-ziplist-entries" 32
6 "set-max-intset-entries" 32
9 proc create_random_dataset
{num cmd
} {
14 for {set i
0} {$i < $num} {incr i
} {
15 # Make sure all the weights are different because
16 # Redis does not use a stable sort but Tcl does.
19 set rint
[expr int
(rand
()*1000000)]
21 set rint
[expr rand
()]
23 if {![info exists seenrand
($rint)]} break
28 r hset wobj_
$i weight
$rint
29 lappend tosort
[list $i $rint]
31 set sorted
[lsort -index 1 -real $tosort]
32 for {set i
0} {$i < $num} {incr i
} {
33 lappend result
[lindex $sorted $i 0]
38 foreach {num cmd enc title
} {
39 16 lpush ziplist
"Ziplist"
40 64 lpush linkedlist
"Linked list"
41 64 sadd hashtable
"Hash table"
43 set result
[create_random_dataset
$num $cmd]
44 assert_encoding
$enc tosort
46 test
"$title: SORT BY key" {
47 assert_equal
$result [r sort tosort
{BY weight_
*}]
50 test
"$title: SORT BY hash field" {
51 assert_equal
$result [r sort tosort
{BY wobj_
*->weight
}]
55 set result
[create_random_dataset
16 lpush
]
57 assert_equal
[lsort -integer $result] [r sort tosort GET
#]
60 test
"SORT GET <const>" {
62 set res
[r sort tosort GET foo
]
63 assert_equal
16 [llength $res]
64 foreach item
$res { assert_equal
{} $item }
67 test
"SORT GET (key and hash) with sanity check" {
68 set l1
[r sort tosort GET
# GET weight_*]
69 set l2
[r sort tosort GET
# GET wobj_*->weight]
70 foreach {id1 w1
} $l1 {id2 w2
} $l2 {
71 assert_equal
$id1 $id2
72 assert_equal
$w1 [r get weight_
$id1]
73 assert_equal
$w2 [r get weight_
$id1]
77 test
"SORT BY key STORE" {
78 r sort tosort
{BY weight_
*} store sort-res
79 assert_equal
$result [r
lrange sort-res
0 -1]
80 assert_equal
16 [r llen sort-res
]
81 assert_encoding ziplist sort-res
84 test
"SORT BY hash field STORE" {
85 r sort tosort
{BY wobj_
*->weight
} store sort-res
86 assert_equal
$result [r
lrange sort-res
0 -1]
87 assert_equal
16 [r llen sort-res
]
88 assert_encoding ziplist sort-res
92 assert_equal
[lsort -decreasing -integer $result] [r sort tosort
{DESC
}]
95 test
"SORT ALPHA against integer encoded strings" {
104 test
"SORT sorted set" {
111 r sort zset alpha desc
114 test
"SORT sorted set: +inf and -inf handling" {
119 r zadd zset
1000000 d
125 test
"SORT regression for issue #19, sorting floats" {
127 set floats
{1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}
131 assert_equal
[lsort -real $floats] [r sort mylist
]
136 set res
[create_random_dataset
$num lpush
]
138 test
"SORT speed, $num element list BY key, 100 times" {
139 set start
[clock clicks
-milliseconds]
140 for {set i
0} {$i < 100} {incr i
} {
141 set sorted
[r sort tosort
{BY weight_
* LIMIT
0 10}]
143 set elapsed
[expr [clock clicks
-milliseconds]-$start]
144 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
148 test
"SORT speed, $num element list BY hash field, 100 times" {
149 set start
[clock clicks
-milliseconds]
150 for {set i
0} {$i < 100} {incr i
} {
151 set sorted
[r sort tosort
{BY wobj_
*->weight LIMIT
0 10}]
153 set elapsed
[expr [clock clicks
-milliseconds]-$start]
154 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
158 test
"SORT speed, $num element list directly, 100 times" {
159 set start
[clock clicks
-milliseconds]
160 for {set i
0} {$i < 100} {incr i
} {
161 set sorted
[r sort tosort
{LIMIT
0 10}]
163 set elapsed
[expr [clock clicks
-milliseconds]-$start]
164 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
168 test
"SORT speed, $num element list BY <const>, 100 times" {
169 set start
[clock clicks
-milliseconds]
170 for {set i
0} {$i < 100} {incr i
} {
171 set sorted
[r sort tosort
{BY nokey LIMIT
0 10}]
173 set elapsed
[expr [clock clicks
-milliseconds]-$start]
174 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "