github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/scheduler/workload_test.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     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
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package scheduler
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  
    20  	"github.com/pingcap/ticdc/cdc/model"
    21  	"github.com/pingcap/ticdc/pkg/util/testleak"
    22  
    23  	"github.com/pingcap/check"
    24  )
    25  
    26  func Test(t *testing.T) { check.TestingT(t) }
    27  
    28  type workloadsSuite struct{}
    29  
    30  var _ = check.Suite(&workloadsSuite{})
    31  
    32  func (s *workloadsSuite) TestWorkloads(c *check.C) {
    33  	defer testleak.AfterTest(c)()
    34  	w := make(workloads)
    35  	w.SetCapture("capture1", model.TaskWorkload{
    36  		1: model.WorkloadInfo{Workload: 1},
    37  		2: model.WorkloadInfo{Workload: 2},
    38  	})
    39  	w.SetCapture("capture2", model.TaskWorkload{
    40  		4: model.WorkloadInfo{Workload: 1},
    41  		3: model.WorkloadInfo{Workload: 2},
    42  	})
    43  	w.SetTable("capture2", 5, model.WorkloadInfo{Workload: 8})
    44  	w.SetTable("capture3", 6, model.WorkloadInfo{Workload: 1})
    45  	w.RemoveTable("capture1", 4)
    46  	w.RemoveTable("capture5", 4)
    47  	w.RemoveTable("capture1", 1)
    48  	c.Assert(w, check.DeepEquals, workloads{
    49  		"capture1": {2: model.WorkloadInfo{Workload: 2}},
    50  		"capture2": {4: model.WorkloadInfo{Workload: 1}, 3: model.WorkloadInfo{Workload: 2}, 5: model.WorkloadInfo{Workload: 8}},
    51  		"capture3": {6: model.WorkloadInfo{Workload: 1}},
    52  	})
    53  	c.Assert(w.AvgEachTable(), check.Equals, uint64(2+1+2+8+1)/5)
    54  	c.Assert(w.SelectIdleCapture(), check.Equals, "capture3")
    55  
    56  	c.Assert(fmt.Sprintf("%.2f%%", w.Skewness()*100), check.Equals, "96.36%")
    57  }