Skip to content

Commit

Permalink
add test for cluster metric
Browse files Browse the repository at this point in the history
Signed-off-by: yunfeiyanggzq <[email protected]>
  • Loading branch information
yunfeiyanggzq committed Jun 8, 2020
1 parent bd29e04 commit e933da3
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,13 @@
*/
package com.alibaba.csp.sentinel.cluster.flow.statistic.metric;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.alibaba.csp.sentinel.cluster.flow.statistic.data.ClusterFlowEvent;
import com.alibaba.csp.sentinel.slots.statistic.base.LongAdder;
import com.alibaba.csp.sentinel.slots.statistic.cache.CacheMap;
import com.alibaba.csp.sentinel.util.AssertUtil;

import java.util.*;
import java.util.Map.Entry;

/**
* @author Eric Zhao
* @since 1.4.0
Expand Down Expand Up @@ -89,6 +82,7 @@ public double getAvg(Object value) {
}

public Map<Object, Double> getTopValues(int number) {
AssertUtil.isTrue(number > 0, "number must be positive");
metric.currentWindow();
List<CacheMap<Object, LongAdder>> buckets = metric.values();

Expand All @@ -114,7 +108,7 @@ public Map<Object, Double> getTopValues(int number) {
@Override
public int compare(Entry<Object, Long> a,
Entry<Object, Long> b) {
return (int)(b.getValue() == null ? 0 : b.getValue()) - (int)(a.getValue() == null ? 0 : a.getValue());
return (int) (b.getValue() == null ? 0 : b.getValue()) - (int) (a.getValue() == null ? 0 : a.getValue());
}
});

Expand All @@ -126,7 +120,7 @@ public int compare(Entry<Object, Long> a,
if (x.getValue() == 0) {
break;
}
doubleResult.put(x.getKey(), ((double)x.getValue()) / metric.getIntervalInSecond());
doubleResult.put(x.getKey(), ((double) x.getValue()) / metric.getIntervalInSecond());
}

return doubleResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.cluster.flow.statistic.metric;

import com.alibaba.csp.sentinel.cluster.flow.statistic.data.ClusterFlowEvent;
import org.junit.Assert;
import org.junit.Test;

public class ClusterMetricTest {

@Test
public void testTryOccupyNext() {
ClusterMetric metric = new ClusterMetric(5, 25);
metric.add(ClusterFlowEvent.PASS, 1);
metric.add(ClusterFlowEvent.PASS, 2);
metric.add(ClusterFlowEvent.PASS, 1);
metric.add(ClusterFlowEvent.BLOCK, 1);
Assert.assertEquals(4, metric.getSum(ClusterFlowEvent.PASS));
Assert.assertEquals(1, metric.getSum(ClusterFlowEvent.BLOCK));
Assert.assertEquals(160, metric.getAvg(ClusterFlowEvent.PASS), 0.01);
Assert.assertEquals(200, metric.tryOccupyNext(ClusterFlowEvent.PASS, 111, 900));
metric.add(ClusterFlowEvent.PASS, 1);
metric.add(ClusterFlowEvent.PASS, 2);
metric.add(ClusterFlowEvent.PASS, 1);
Assert.assertEquals(200, metric.tryOccupyNext(ClusterFlowEvent.PASS, 222, 900));
metric.add(ClusterFlowEvent.PASS, 1);
metric.add(ClusterFlowEvent.PASS, 2);
metric.add(ClusterFlowEvent.PASS, 1);
Assert.assertEquals(0, metric.tryOccupyNext(ClusterFlowEvent.PASS, 333, 900));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.cluster.flow.statistic.metric;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class ClusterParamMetricTest {

@Test
public void testClusterParamMetric() throws InterruptedException {
Map<Object, Double> topMap = new HashMap<Object, Double>();
ClusterParamMetric metric = new ClusterParamMetric(5, 25, 100);
metric.addValue("e1", -1);
metric.addValue("e1", -2);
metric.addValue("e1", -54);
metric.addValue("e1", -34);
metric.addValue("e1", 2);
metric.addValue("e1", 2);
metric.addValue("e2", 100);
metric.addValue("e2", 23);
metric.addValue("e3", 100);
metric.addValue("e3", 230);
Assert.assertEquals(-86, metric.getSum("e1"));
Assert.assertEquals(-3440, metric.getAvg("e1"), 0.01);
topMap.put("e3", (double) 13200);
Assert.assertEquals(topMap, metric.getTopValues(1));
topMap.put("e2", (double) 4920);
topMap.put("e1", (double) -3440);
Assert.assertEquals(topMap, metric.getTopValues(5));
metric.addValue("e2", 100);
metric.addValue("e2", 23);
Assert.assertEquals(246, metric.getSum("e2"));
Assert.assertEquals(9840, metric.getAvg("e2"), 0.01);
}

@Test(expected = IllegalArgumentException.class)
public void testIllegalArgument() {
ClusterParamMetric metric = new ClusterParamMetric(5, 25, 100);
metric.getTopValues(-1);
}
}

0 comments on commit e933da3

Please sign in to comment.