mirror of https://github.com/longtai-cn/hippo4j
parent
f585f4a448
commit
cef9f33f1f
@ -1,122 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.starter.test;
|
||||
|
||||
import cn.hippo4j.common.model.PoolRunStateInfo;
|
||||
import cn.hippo4j.common.toolkit.JSONUtil;
|
||||
import cn.hippo4j.core.executor.manage.GlobalThreadPoolManage;
|
||||
import cn.hippo4j.starter.monitor.collect.RunTimeInfoCollector;
|
||||
import cn.hippo4j.core.executor.support.ResizableCapacityLinkedBlockIngQueue;
|
||||
import cn.hippo4j.core.executor.support.ThreadFactoryBuilder;
|
||||
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
|
||||
import cn.hippo4j.common.toolkit.ThreadUtil;
|
||||
import cn.hippo4j.core.executor.DynamicThreadPoolWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 监控性能测试.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2022/1/12 08:17
|
||||
*/
|
||||
@Slf4j
|
||||
public class MonitorPerformanceTest {
|
||||
|
||||
private static final int MAX_EXECUTE = 20000;
|
||||
|
||||
private static final String TEST_FLAG = "test.monitor.performance";
|
||||
|
||||
private static final RunTimeInfoCollector COLLECTOR = new RunTimeInfoCollector(null);
|
||||
|
||||
private static final ThreadPoolExecutor DYNAMIC_EXECUTOR = ThreadPoolBuilder.builder()
|
||||
.dynamicPool()
|
||||
.threadFactory(TEST_FLAG)
|
||||
.poolThreadSize(5, 10)
|
||||
.workQueue(new ResizableCapacityLinkedBlockIngQueue(1024))
|
||||
.rejected(new ThreadPoolExecutor.AbortPolicy())
|
||||
.build();
|
||||
|
||||
private static final ThreadPoolExecutor MONITOR_DYNAMIC_EXECUTOR = ThreadPoolBuilder.builder()
|
||||
.dynamicPool()
|
||||
.threadFactory("dynamic.thread.pool")
|
||||
.poolThreadSize(5, 10)
|
||||
.workQueue(new ResizableCapacityLinkedBlockIngQueue(1024))
|
||||
.rejected(new ThreadPoolExecutor.AbortPolicy())
|
||||
.build();
|
||||
|
||||
private static final ScheduledThreadPoolExecutor COLLECT_VESSEL_EXECUTOR = new ScheduledThreadPoolExecutor(
|
||||
new Integer(1),
|
||||
ThreadFactoryBuilder.builder().daemon(true).prefix("client.scheduled.collect.dat").build());
|
||||
|
||||
static {
|
||||
DynamicThreadPoolWrapper wrapper = new DynamicThreadPoolWrapper(TEST_FLAG, DYNAMIC_EXECUTOR);
|
||||
GlobalThreadPoolManage.registerPool(TEST_FLAG, wrapper);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
log.info("测试线程池, 执行时长 :: {}", testExecutorTime(DYNAMIC_EXECUTOR));
|
||||
|
||||
log.info("开始执行 DYNAMIC_EXECUTOR 线程池销毁...");
|
||||
while (!DYNAMIC_EXECUTOR.isTerminated()) {
|
||||
ThreadUtil.sleep(500);
|
||||
}
|
||||
|
||||
COLLECT_VESSEL_EXECUTOR.scheduleWithFixedDelay(
|
||||
() -> {
|
||||
PoolRunStateInfo poolRunState = COLLECTOR.getPoolRunState(TEST_FLAG);
|
||||
log.info("采集数据 :: {}", JSONUtil.toJSONString(poolRunState));
|
||||
},
|
||||
1000,
|
||||
1000,
|
||||
TimeUnit.MILLISECONDS);
|
||||
|
||||
log.info("测试带有监控线程池, 执行时长 :: {}", testExecutorTime(MONITOR_DYNAMIC_EXECUTOR));
|
||||
|
||||
log.info("开始执行 MONITOR_DYNAMIC_EXECUTOR 线程池销毁...");
|
||||
while (!MONITOR_DYNAMIC_EXECUTOR.isTerminated()) {
|
||||
ThreadUtil.sleep(500);
|
||||
}
|
||||
|
||||
COLLECT_VESSEL_EXECUTOR.shutdownNow();
|
||||
}
|
||||
|
||||
private static Long testExecutorTime(ThreadPoolExecutor executor) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
for (int i = 0; i < MAX_EXECUTE; i++) {
|
||||
try {
|
||||
ThreadUtil.sleep(1);
|
||||
executor.execute(() -> {
|
||||
ThreadUtil.sleep(5);
|
||||
});
|
||||
} catch (Exception ex) {
|
||||
// ignore
|
||||
log.error("拒绝策略执行.");
|
||||
}
|
||||
}
|
||||
|
||||
long endTime = System.currentTimeMillis();
|
||||
|
||||
executor.shutdown();
|
||||
return endTime - startTime;
|
||||
}
|
||||
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.starter.test;
|
||||
|
||||
import cn.hippo4j.core.executor.DynamicThreadPoolExecutor;
|
||||
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
|
||||
import cn.hippo4j.common.toolkit.ThreadUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
/**
|
||||
* Rejected execution handler proxy test.
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2022/2/17 19:52
|
||||
*/
|
||||
@Slf4j
|
||||
public class RejectedExecutionHandlerProxyTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
test(i + "");
|
||||
}
|
||||
}
|
||||
|
||||
private static void test(String threadPoolId) {
|
||||
ThreadPoolExecutor executor = ThreadPoolBuilder.builder()
|
||||
.threadPoolId(threadPoolId)
|
||||
.threadFactory(threadPoolId)
|
||||
.poolThreadSize(1, 1)
|
||||
.workQueue(new LinkedBlockingQueue(1))
|
||||
.dynamicPool()
|
||||
.build();
|
||||
|
||||
for (int i = 0; i < 300; i++) {
|
||||
try {
|
||||
executor.execute(() -> ThreadUtil.sleep(Integer.MAX_VALUE));
|
||||
} catch (Exception ex) {
|
||||
log.error("ThreadPool name :: {}, Exception :: ", Thread.currentThread().getName(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
ThreadUtil.sleep(1000);
|
||||
|
||||
DynamicThreadPoolExecutor dynamicThreadPoolExecutor = (DynamicThreadPoolExecutor) executor;
|
||||
long rejectCount = dynamicThreadPoolExecutor.getRejectCountNum();
|
||||
log.info("ThreadPool name :: {}, Reject count :: {}", Thread.currentThread().getName(), rejectCount);
|
||||
}
|
||||
}
|
@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 cn.hippo4j.starter.test;
|
||||
|
||||
import cn.hippo4j.core.executor.support.ResizableCapacityLinkedBlockIngQueue;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 可调整大小的阻塞队列单元测试
|
||||
*
|
||||
* @author chen.ma
|
||||
* @date 2021/6/20 14:45
|
||||
*/
|
||||
@Slf4j
|
||||
public class ResizableCapacityLinkedBlockIngQueueTest {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ResizableCapacityLinkedBlockIngQueue blockIngQueue = new ResizableCapacityLinkedBlockIngQueue(5);
|
||||
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1,
|
||||
3,
|
||||
1024,
|
||||
TimeUnit.SECONDS,
|
||||
blockIngQueue);
|
||||
|
||||
/*
|
||||
* Runnable runnable = new Runnable() {
|
||||
*
|
||||
* @SneakyThrows public void run() { Thread.sleep(10000); } }; for (int i = 0; i <; i++) { threadPoolExecutor.execute(runnable); }
|
||||
*/
|
||||
|
||||
print(threadPoolExecutor);
|
||||
|
||||
Thread.sleep(1000);
|
||||
blockIngQueue.setCapacity(1000);
|
||||
print(threadPoolExecutor);
|
||||
|
||||
}
|
||||
|
||||
private static void print(ThreadPoolExecutor executor) {
|
||||
LinkedBlockingQueue queue = (LinkedBlockingQueue) executor.getQueue();
|
||||
|
||||
log.info("核心线程数 :: {}," +
|
||||
" 活动线程数 :: {}," +
|
||||
" 最大线程数 :: {}," +
|
||||
" 线程池活跃度 :: {}," +
|
||||
" 任务完成数 :: {}," +
|
||||
" 队列大小 :: {}," +
|
||||
" 当前排队线程数 :: {}," +
|
||||
" 队列剩余大小 :: {}," +
|
||||
" 队列使用度 :: {}",
|
||||
executor.getCorePoolSize(),
|
||||
executor.getActiveCount(),
|
||||
executor.getMaximumPoolSize(),
|
||||
divide(executor.getActiveCount(), executor.getMaximumPoolSize()),
|
||||
executor.getCompletedTaskCount(),
|
||||
(queue.size() + queue.remainingCapacity()),
|
||||
queue.size(),
|
||||
queue.remainingCapacity(),
|
||||
divide(queue.size(), queue.size() + queue.remainingCapacity()));
|
||||
|
||||
}
|
||||
|
||||
private static String divide(int num1, int num2) {
|
||||
return String.format("%1.2f%%", Double.parseDouble(num1 + "") / Double.parseDouble(num2 + "") * 100);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue