add testcase
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
ehlxr 2021-02-10 11:28:29 +08:00
parent 10a11e2962
commit 83f7192ee5
5 changed files with 164 additions and 47 deletions

View File

@ -138,7 +138,8 @@ public abstract class AbstractClient implements Client {
Try.of(() -> { Try.of(() -> {
logger.debug("write {} to channel", sdkProto); 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 -> { .addListener(channelFuture -> {
if (channelFuture.isSuccess()) { if (channelFuture.isSuccess()) {
return; return;

View File

@ -1,6 +1,7 @@
package io.github.ehlxr.did; package io.github.ehlxr.did;
import io.github.ehlxr.did.client.SdkClient; import io.github.ehlxr.did.client.SdkClient;
import io.github.ehlxr.did.common.Try;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -8,6 +9,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.IntStream;
/** /**
* 压测 * 压测
@ -30,63 +33,59 @@ public class DidSdkPressTest {
} }
@Test @Test
public void asyncTest() throws Exception { public void syncTest() {
long start; int nums = 6000;
long end; int round = 10;
long cast;
long amount = 0;
long allcast = 0;
for (int k = 0; k < 10; k++) { AtomicLong amount = new AtomicLong();
// 初始发送总量 AtomicLong allcast = new AtomicLong();
int NUM = 80000;
final CountDownLatch countDownLatch = new CountDownLatch(NUM);
start = System.currentTimeMillis();
for (int i = 0; i < NUM; i++) {
client.invokeAsync(responseFuture -> countDownLatch.countDown());
}
// countDownLatch.await(10, TimeUnit.SECONDS); IntStream.range(0, round).forEach(j -> {
countDownLatch.await(); long start = System.currentTimeMillis();
end = System.currentTimeMillis(); IntStream.range(0, nums).parallel().forEach(i -> client.invokeSync());
cast = (end - start);
allcast += cast;
logger.info("invokeAsync test num is: {}, cast time: {} millsec, throughput: {} send/millsec", NUM, cast, (double) NUM / cast); long end = System.currentTimeMillis();
amount += NUM; long cast = (end - start);
// NUM = NUM + 5000; allcast.addAndGet(cast);
// TimeUnit.SECONDS.sleep(2);
}
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 @Test
public void syncTest() { public void asyncTest() {
long start; int nums = 6000;
long end; int round = 10;
long cast;
long amount = 0;
long allcast = 0;
for (int k = 0; k < 10; k++) { AtomicLong amount = new AtomicLong();
start = System.currentTimeMillis(); AtomicLong allcast = new AtomicLong();
int NUM = 60000;
for (int i = 0; i < NUM; i++) {
client.invokeSync();
}
end = System.currentTimeMillis(); IntStream.range(0, round).forEach(j -> {
cast = (end - start); long start = System.currentTimeMillis();
allcast += cast;
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; long end = System.currentTimeMillis();
// NUM += 5000; long cast = (end - start);
// TimeUnit.SECONDS.sleep(2); 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());
} }
} }

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date [%thread] %-5level %logger{35}:%line - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
<logger name="io.netty" level="OFF"/>
</configuration>

View File

@ -18,6 +18,12 @@
<groupId>io.github.ehlxr</groupId> <groupId>io.github.ehlxr</groupId>
<artifactId>did-core</artifactId> <artifactId>did-core</artifactId>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -0,0 +1,95 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* 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<Long> 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)));
});
}
}