-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmake-compat.pl
77 lines (53 loc) · 1.63 KB
/
make-compat.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl
use strict;
use warnings;
use Algorithm::ConsistentHash::Ketama;
my $hashfunc = 2;
my $ketama = Algorithm::ConsistentHash::Ketama->new(use_hashfunc => $hashfunc);
print <<"EOGO";
package ketama
import (
"fmt"
"strconv"
"testing"
)
var compat${hashfunc}Tests = [][]Bucket{
EOGO
my $keys = 1_000;
my $buckets = 50;
for my $bucket (1..$buckets) {
# print STDERR "bucket=$bucket\n";
$ketama->add_bucket( "server$bucket", 1 );
my %m;
for (my $i=0; $i < $keys; $i++) {
$m{$ketama->hash("foo$i")}++
}
print "\t{\n";
for my $key (sort keys %m) {
print "\t\t{\"$key\", $m{$key}},\n";
}
print "\t},\n";
}
print "}\n";
print <<"EOGO";
func TestKetama$hashfunc(t *testing.T) {
var buckets []Bucket
BUCKET:
for bucket := 1; bucket <= len(compat${hashfunc}Tests); bucket++ {
b := &Bucket{Label: fmt.Sprintf("server%d", bucket), Weight: 1}
buckets = append(buckets, *b)
k, _ := NewWithHash(buckets, HashFunc$hashfunc)
m := make(map[string]int)
for i := 0; i < $keys; i++ {
s := k.Hash("foo" + strconv.Itoa(i))
m[s]++
}
for _, tt := range compat${hashfunc}Tests[bucket-1] {
if m[tt.Label] != tt.Weight {
t.Errorf("compatibility check failed for buckets=%d key=%s expected=%d got=%d", bucket, tt.Label, tt.Weight, m[tt.Label])
continue BUCKET
}
}
}
}
EOGO