4 "list-max-ziplist-value" 16
5 "list-max-ziplist-entries" 32
8 proc create_random_dataset
{num cmd
} {
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.
18 set rint
[expr int
(rand
()*1000000)]
20 set rint
[expr rand
()]
22 if {![info exists seenrand
($rint)]} break
27 r hset wobj_
$i weight
$rint
28 lappend tosort
[list $i $rint]
30 set sorted
[lsort -index 1 -real $tosort]
31 for {set i
0} {$i < $num} {incr i
} {
32 lappend result
[lindex $sorted $i 0]
37 foreach {num cmd enc title
} {
38 16 lpush ziplist
"Ziplist"
39 64 lpush linkedlist
"Linked list"
41 set result
[create_random_dataset
$num $cmd]
42 assert_encoding
$enc tosort
44 test
"$title: SORT BY key" {
45 assert_equal
$result [r sort tosort
{BY weight_
*}]
48 test
"$title: SORT BY hash field" {
49 assert_equal
$result [r sort tosort
{BY wobj_
*->weight
}]
53 set result
[create_random_dataset
16 lpush
]
55 assert_equal
[lsort -integer $result] [r sort tosort GET
#]
58 test
"SORT GET <const>" {
60 set res
[r sort tosort GET foo
]
61 assert_equal
16 [llength $res]
62 foreach item
$res { assert_equal
{} $item }
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]
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
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
90 assert_equal
[lsort -decreasing -integer $result] [r sort tosort
{DESC
}]
93 test
"SORT ALPHA against integer encoded strings" {
102 test
"SORT sorted set" {
109 r sort zset alpha desc
112 test
"SORT sorted set: +inf and -inf handling" {
117 r zadd zset
1000000 d
123 test
"SORT regression for issue #19, sorting floats" {
125 set floats
{1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}
129 assert_equal
[lsort -real $floats] [r sort mylist
]
134 set res
[create_random_dataset
$num lpush
]
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}]
141 set elapsed
[expr [clock clicks
-milliseconds]-$start]
142 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
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}]
151 set elapsed
[expr [clock clicks
-milliseconds]-$start]
152 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
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}]
161 set elapsed
[expr [clock clicks
-milliseconds]-$start]
162 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
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}]
171 set elapsed
[expr [clock clicks
-milliseconds]-$start]
172 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "