1
// Copyright 2020 The CubeFS Authors.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
24
func TestKmin(t *testing.T) {
25
partitions := make([]*DataPartition, 0)
27
rand.Seed(time.Now().UnixNano())
28
length := rand.Intn(100) + 2
30
for i := 0; i < length; i++ {
31
rand.Seed(time.Now().UnixNano())
34
dp := new(DataPartition)
35
dp.Metrics = new(DataPartitionMetrics)
36
dp.Metrics.AvgWriteLatencyNano = i
37
partitions = append(partitions, dp)
39
fmt.Printf("%-20s", "origin partitions:")
40
for _, v := range partitions {
41
fmt.Printf("%v ", v.GetAvgWrite())
45
kth := selectKminDataPartition(partitions, (length-1)*80/100+1)
47
kmin := partitions[kth].GetAvgWrite()
49
fmt.Printf("%-20s%v/%v", "kth of length:", kth, length)
52
fmt.Printf("%-20s%v", "kmin:", kmin)
55
fmt.Printf("%-20s", "faster partitions:")
56
for _, v := range partitions[:kth] {
57
if v.GetAvgWrite() > kmin {
59
fmt.Println("select error!")
62
fmt.Printf("%v ", v.GetAvgWrite())
66
fmt.Printf("%-20s", "slower partitions:")
67
for _, v := range partitions[kth:] {
68
fmt.Printf("%v ", v.GetAvgWrite())
69
if v.GetAvgWrite() < kmin {
71
fmt.Println("select error!")