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 16 sadd intset
"Intset"
42 64 sadd hashtable
"Hash table"
44 set result
[create_random_dataset
$num $cmd]
45 assert_encoding
$enc tosort
47 test
"$title: SORT BY key" {
48 assert_equal
$result [r sort tosort
{BY weight_
*}]
51 test
"$title: SORT BY hash field" {
52 assert_equal
$result [r sort tosort
{BY wobj_
*->weight
}]
56 set result
[create_random_dataset
16 lpush
]
58 assert_equal
[lsort -integer $result] [r sort tosort GET
#]
61 test
"SORT GET <const>" {
63 set res
[r sort tosort GET foo
]
64 assert_equal
16 [llength $res]
65 foreach item
$res { assert_equal
{} $item }
68 test
"SORT GET (key and hash) with sanity check" {
69 set l1
[r sort tosort GET
# GET weight_*]
70 set l2
[r sort tosort GET
# GET wobj_*->weight]
71 foreach {id1 w1
} $l1 {id2 w2
} $l2 {
72 assert_equal
$id1 $id2
73 assert_equal
$w1 [r get weight_
$id1]
74 assert_equal
$w2 [r get weight_
$id1]
78 test
"SORT BY key STORE" {
79 r sort tosort
{BY weight_
*} store sort-res
80 assert_equal
$result [r
lrange sort-res
0 -1]
81 assert_equal
16 [r llen sort-res
]
82 assert_encoding ziplist sort-res
85 test
"SORT BY hash field STORE" {
86 r sort tosort
{BY wobj_
*->weight
} store sort-res
87 assert_equal
$result [r
lrange sort-res
0 -1]
88 assert_equal
16 [r llen sort-res
]
89 assert_encoding ziplist sort-res
93 assert_equal
[lsort -decreasing -integer $result] [r sort tosort
{DESC
}]
96 test
"SORT ALPHA against integer encoded strings" {
105 test
"SORT sorted set" {
112 r sort zset alpha desc
115 test
"SORT sorted set: +inf and -inf handling" {
120 r zadd zset
1000000 d
126 test
"SORT regression for issue #19, sorting floats" {
128 set floats
{1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}
132 assert_equal
[lsort -real $floats] [r sort mylist
]
137 set res
[create_random_dataset
$num lpush
]
139 test
"SORT speed, $num element list BY key, 100 times" {
140 set start
[clock clicks
-milliseconds]
141 for {set i
0} {$i < 100} {incr i
} {
142 set sorted
[r sort tosort
{BY weight_
* LIMIT
0 10}]
144 set elapsed
[expr [clock clicks
-milliseconds]-$start]
145 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
149 test
"SORT speed, $num element list BY hash field, 100 times" {
150 set start
[clock clicks
-milliseconds]
151 for {set i
0} {$i < 100} {incr i
} {
152 set sorted
[r sort tosort
{BY wobj_
*->weight LIMIT
0 10}]
154 set elapsed
[expr [clock clicks
-milliseconds]-$start]
155 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
159 test
"SORT speed, $num element list directly, 100 times" {
160 set start
[clock clicks
-milliseconds]
161 for {set i
0} {$i < 100} {incr i
} {
162 set sorted
[r sort tosort
{LIMIT
0 10}]
164 set elapsed
[expr [clock clicks
-milliseconds]-$start]
165 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
169 test
"SORT speed, $num element list BY <const>, 100 times" {
170 set start
[clock clicks
-milliseconds]
171 for {set i
0} {$i < 100} {incr i
} {
172 set sorted
[r sort tosort
{BY nokey LIMIT
0 10}]
174 set elapsed
[expr [clock clicks
-milliseconds]-$start]
175 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "