Skip to content

Commit

Permalink
Improve several map iteration (#2938)
Browse files Browse the repository at this point in the history
  • Loading branch information
biyuhao authored and beiwei30 committed Dec 12, 2018
1 parent 6c9f74c commit 227a169
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class ConsistentHashLoadBalanceTest extends LoadBalanceBaseTest {
public void testConsistentHashLoadBalance() {
int runs = 10000;
Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, ConsistentHashLoadBalance.NAME);
for (Invoker minvoker : counter.keySet()) {
Long count = counter.get(minvoker).get();
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
Assert.assertTrue("abs diff should < avg", Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class LeastActiveBalanceTest extends LoadBalanceBaseTest {
public void testLeastActiveLoadBalance_select() {
int runs = 10000;
Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, LeastActiveLoadBalance.NAME);
for (Invoker minvoker : counter.keySet()) {
Long count = counter.get(minvoker).get();
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
// System.out.println(count);
Assert.assertTrue("abs diff shoud < avg",
Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class RandomLoadBalanceTest extends LoadBalanceBaseTest {
public void testRandomLoadBalanceSelect() {
int runs = 1000;
Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, RandomLoadBalance.NAME);
for (Invoker minvoker : counter.keySet()) {
Long count = counter.get(minvoker).get();
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
Assert.assertTrue("abs diff should < avg", Math.abs(count - runs / (0f + invokers.size())) < runs / (0f + invokers.size()));
}

Expand All @@ -43,8 +43,8 @@ public void testRandomLoadBalanceSelect() {
}
}
counter = getInvokeCounter(runs, LeastActiveLoadBalance.NAME);
for (Invoker minvoker : counter.keySet()) {
Long count = counter.get(minvoker).get();
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
}
Assert.assertEquals(runs, counter.get(invoker1).intValue());
Assert.assertEquals(0, counter.get(invoker2).intValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ private void assertStrictWRRResult(int loop, Map<Invoker, InvokeResult> resultMa
public void testRoundRobinLoadBalanceSelect() {
int runs = 10000;
Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, RoundRobinLoadBalance.NAME);
for (Invoker minvoker : counter.keySet()) {
Long count = counter.get(minvoker).get();
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
Assert.assertTrue("abs diff should < 1", Math.abs(count - runs / (0f + invokers.size())) < 1f);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,10 @@ public void testSelectBalance() {
counter.get(sinvoker).incrementAndGet();
}

for (Invoker minvoker : counter.keySet()) {
Long count = counter.get(minvoker).get();
for (Map.Entry<Invoker, AtomicLong> entry : counter.entrySet()) {
Long count = entry.getValue().get();
// System.out.println(count);
if (minvoker.isAvailable())
if (entry.getKey().isAvailable())
Assert.assertTrue("count should > avg", count > runs / invokers.size());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public Menu() {
}

public Menu(Map<String, List<String>> menus) {
for (String key : menus.keySet()) {
this.menus.put(key, new ArrayList<String>(menus.get(key)));
for (Map.Entry<String, List<String>> entry : menus.entrySet()) {
this.menus.put(entry.getKey(), new ArrayList<String>(entry.getValue()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
merge(expected, firstMenuMap);
merge(expected, secondMenuMap);
assertEquals(expected.keySet(), menu.getMenus().keySet());
for (String key : expected.keySet()) {
for (Map.Entry<String, List<String>> entry : expected.entrySet()) {
// FIXME: cannot guarantee the sequence of the merge result, check implementation in
// MergeableClusterInvoker#invoke
List<String> values1 = new ArrayList<String>(expected.get(key));
List<String> values2 = new ArrayList<String>(menu.getMenus().get(key));
List<String> values1 = new ArrayList<String>(entry.getValue());
List<String> values2 = new ArrayList<String>(menu.getMenus().get(entry.getKey()));
Collections.sort(values1);
Collections.sort(values2);
assertEquals(values1, values2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,11 @@ private static void serializeInternal(JavaBeanDescriptor descriptor, Object obj,
}
} else if (obj instanceof Map) {
Map map = (Map) obj;
for (Object key : map.keySet()) {
Object value = map.get(key);
map.forEach((key, value) -> {
Object keyDescriptor = key == null ? null : createDescriptorIfAbsent(key, accessor, cache);
Object valueDescriptor = value == null ? null : createDescriptorIfAbsent(value, accessor, cache);
descriptor.setProperty(keyDescriptor, valueDescriptor);
} // ~ end of loop map
});// ~ end of loop map
} else {
if (JavaBeanAccessor.isAccessByMethod(accessor)) {
Map<String, Method> methods = ReflectUtils.getBeanPropertyReadMethods(obj.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public static void sent(Channel channel, Request request) {
* @param channel channel to close
*/
public static void closeChannel(Channel channel) {
for (long id : CHANNELS.keySet()) {
if (channel.equals(CHANNELS.get(id))) {
DefaultFuture future = getFuture(id);
for (Map.Entry<Long, Channel> entry: CHANNELS.entrySet()) {
if (channel.equals(entry.getValue())) {
DefaultFuture future = getFuture(entry.getKey());
if (future != null && !future.isDone()) {
Response disconnectResponse = new Response(future.getId());
disconnectResponse.setStatus(Response.CHANNEL_INACTIVE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.fail;

Expand Down Expand Up @@ -114,8 +115,8 @@ public void test_Decode_Error_MagicNum() throws IOException {
inputBytes.put(new byte[]{MAGIC_HIGH, 0}, TelnetCodec.DecodeResult.NEED_MORE_INPUT);
inputBytes.put(new byte[]{0, MAGIC_LOW}, TelnetCodec.DecodeResult.NEED_MORE_INPUT);

for (byte[] input : inputBytes.keySet()) {
testDecode_assertEquals(assemblyDataProtocol(input), inputBytes.get(input));
for (Map.Entry<byte[], Object> entry: inputBytes.entrySet()) {
testDecode_assertEquals(assemblyDataProtocol(entry.getKey()), entry.getValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class TelnetCodecTest {
protected Codec2 codec;
Expand Down Expand Up @@ -235,8 +236,8 @@ public void testDecode_WithExitByte() throws IOException {
exitbytes.put(new byte[]{1, -1, -12, -1, -3, 6}, false); //must equal the bytes
exitbytes.put(new byte[]{-1, -19, -1, -3, 6}, true); /* Linux Pause */

for (byte[] exit : exitbytes.keySet()) {
testDecode_WithExitByte(exit, exitbytes.get(exit));
for (Map.Entry<byte[], Boolean> entry : exitbytes.entrySet()) {
testDecode_WithExitByte(entry.getKey(), entry.getValue());
}
}

Expand Down

0 comments on commit 227a169

Please sign in to comment.