diff --git a/did-sdk/src/main/java/io/github/ehlxr/did/client/AbstractClient.java b/did-sdk/src/main/java/io/github/ehlxr/did/client/AbstractClient.java index f9d77ed..341c1c4 100644 --- a/did-sdk/src/main/java/io/github/ehlxr/did/client/AbstractClient.java +++ b/did-sdk/src/main/java/io/github/ehlxr/did/client/AbstractClient.java @@ -138,7 +138,8 @@ public abstract class AbstractClient implements Client { Try.of(() -> { logger.debug("write {} to channel", sdkProto); - channelFuture.channel().writeAndFlush(Message.newBuilder().type((byte) 0xA).flag((byte) 0xC).content(sdkProto).build()) + channelFuture.channel() + .writeAndFlush(Message.newBuilder().type((byte) 0xA).flag((byte) 0xC).content(sdkProto).build()) .addListener(channelFuture -> { if (channelFuture.isSuccess()) { return; diff --git a/did-sdk/src/test/java/io/github/ehlxr/did/DidSdkPressTest.java b/did-sdk/src/test/java/io/github/ehlxr/did/DidSdkPressTest.java index 9a0c0f6..5ac4e04 100644 --- a/did-sdk/src/test/java/io/github/ehlxr/did/DidSdkPressTest.java +++ b/did-sdk/src/test/java/io/github/ehlxr/did/DidSdkPressTest.java @@ -1,6 +1,7 @@ package io.github.ehlxr.did; import io.github.ehlxr.did.client.SdkClient; +import io.github.ehlxr.did.common.Try; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -8,6 +9,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.IntStream; /** * 压测 @@ -30,63 +33,59 @@ public class DidSdkPressTest { } @Test - public void asyncTest() throws Exception { - long start; - long end; - long cast; - long amount = 0; - long allcast = 0; + public void syncTest() { + int nums = 6000; + int round = 10; - for (int k = 0; k < 10; k++) { - // 初始发送总量 - int NUM = 80000; - final CountDownLatch countDownLatch = new CountDownLatch(NUM); - start = System.currentTimeMillis(); - for (int i = 0; i < NUM; i++) { - client.invokeAsync(responseFuture -> countDownLatch.countDown()); - } + AtomicLong amount = new AtomicLong(); + AtomicLong allcast = new AtomicLong(); - // countDownLatch.await(10, TimeUnit.SECONDS); - countDownLatch.await(); - end = System.currentTimeMillis(); - cast = (end - start); - allcast += cast; + IntStream.range(0, round).forEach(j -> { + long start = System.currentTimeMillis(); + IntStream.range(0, nums).parallel().forEach(i -> client.invokeSync()); - logger.info("invokeAsync test num is: {}, cast time: {} millsec, throughput: {} send/millsec", NUM, cast, (double) NUM / cast); - amount += NUM; - // NUM = NUM + 5000; - // TimeUnit.SECONDS.sleep(2); - } + long end = System.currentTimeMillis(); + long cast = (end - start); + allcast.addAndGet(cast); - logger.info("invokeAsync test all num is: {}, all cast time: {} millsec, all throughput: {} send/millsec", amount, allcast, (double) amount / allcast); + logger.info("invokeSync test num is: {}, cast time: {} millsec, throughput: {} send/millsec", + nums, cast, (long) nums / cast); + amount.addAndGet(nums); + }); + + logger.info("invokeSync test all num is: {}, all cast time: {} millsec, all throughput: {} send/millsec", + amount, allcast, amount.get() / allcast.get()); } @Test - public void syncTest() { - long start; - long end; - long cast; - long amount = 0; - long allcast = 0; + public void asyncTest() { + int nums = 6000; + int round = 10; - for (int k = 0; k < 10; k++) { - start = System.currentTimeMillis(); - int NUM = 60000; - for (int i = 0; i < NUM; i++) { - client.invokeSync(); - } + AtomicLong amount = new AtomicLong(); + AtomicLong allcast = new AtomicLong(); - end = System.currentTimeMillis(); - cast = (end - start); - allcast += cast; + IntStream.range(0, round).forEach(j -> { + long start = System.currentTimeMillis(); - logger.info("invokeSync test num is: {}, cast time: {} millsec, throughput: {} send/millsec", NUM, cast, (double) NUM / cast); + final CountDownLatch countDownLatch = new CountDownLatch(nums); + IntStream.range(0, nums).parallel().forEach(i -> + Try.of(() -> + client.invokeAsync(rf -> countDownLatch.countDown()) + ).trap(Throwable::printStackTrace).run() + ); + Try.of((Try.ThrowableRunnable) countDownLatch::await).trap(Throwable::printStackTrace).run(); - amount += NUM; - // NUM += 5000; - // TimeUnit.SECONDS.sleep(2); - } + long end = System.currentTimeMillis(); + long cast = (end - start); + allcast.addAndGet(cast); - logger.info("invokeSync test all num is: {}, all cast time: {} millsec, all throughput: {} send/millsec", amount, allcast, (double) amount / allcast); + logger.info("invokeAsync test num is: {}, cast time: {} millsec, throughput: {} send/millsec", + nums, cast, (long) nums / cast); + amount.addAndGet(nums); + }); + + logger.info("invokeAsync test all num is: {}, all cast time: {} millsec, all throughput: {} send/millsec", + amount, allcast, amount.get() / allcast.get()); } } diff --git a/did-sdk/src/test/resources/logback.xml b/did-sdk/src/test/resources/logback.xml new file mode 100644 index 0000000..38eaada --- /dev/null +++ b/did-sdk/src/test/resources/logback.xml @@ -0,0 +1,16 @@ + + + + + %date [%thread] %-5level %logger{35}:%line - %msg%n + UTF-8 + + + + + + + + + + \ No newline at end of file diff --git a/did-server/pom.xml b/did-server/pom.xml index 112daee..45ad202 100644 --- a/did-server/pom.xml +++ b/did-server/pom.xml @@ -18,6 +18,12 @@ io.github.ehlxr did-core + + + junit + junit + test + diff --git a/did-server/src/test/java/io/github/ehlxr/did/SnowFlakeTest.java b/did-server/src/test/java/io/github/ehlxr/did/SnowFlakeTest.java new file mode 100644 index 0000000..bf6efa8 --- /dev/null +++ b/did-server/src/test/java/io/github/ehlxr/did/SnowFlakeTest.java @@ -0,0 +1,95 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2020 xrv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.ehlxr.did; + +import io.github.ehlxr.did.common.Try; +import io.github.ehlxr.did.generator.SnowFlake; +import org.junit.Before; +import org.junit.Test; + +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.ForkJoinPool; +import java.util.stream.IntStream; + +/** + * @author ehlxr + * @since 2021-02-10 09:37. + */ +public class SnowFlakeTest { + SnowFlake snowFlake; + + @Before + public void init() { + snowFlake = SnowFlake.newBuilder().datacenterId(1).machineId(1).build(); + } + + @Test + public void parallelTest() { + Set ids = new CopyOnWriteArraySet<>(); + + // 总数 + int counts = 40000; + + // 并发数 + int threads = 100; + CyclicBarrier cyclicBarrier = new CyclicBarrier(threads); + + // ExecutorService pool = Executors.newFixedThreadPool(threads); + ForkJoinPool pool = new ForkJoinPool(threads); + long now = System.currentTimeMillis(); + + IntStream.range(0, counts).forEach(i -> + pool.execute(() -> { + // 等待 threads 个任务准备就绪 + Try.of((Try.ThrowableRunnable) cyclicBarrier::await).trap(System.out::println).run(); + + long id = snowFlake.nextId(); + if (!ids.add(id)) { + System.out.println(id); + } + })); + + pool.shutdown(); + while (!pool.isTerminated()) { + Try.of(() -> Thread.sleep(10)).trap(System.out::println).run(); + } + long end = System.currentTimeMillis(); + System.out.println(ids.size() + " ids create at " + (end - now) + "ms."); + } + + @Test + public void qpsTest() { + int nums = 10000; + + IntStream.range(0, 10).forEach(j -> { + long start = System.currentTimeMillis(); + IntStream.range(0, nums).parallel().forEach(i -> snowFlake.nextId()); + + System.out.println("over: qps= " + ((long) nums * 1000 / (System.currentTimeMillis() - start))); + }); + } +}