Browse Source

提升cpu使用率

tags/24080901
WendyYang 8 months ago
parent
commit
395be3cc1c
3 changed files with 74 additions and 34 deletions
  1. +64
    -0
      hz-pm-api/src/main/java/com/hz/pm/api/common/handler/CpuLoadTask.java
  2. +0
    -34
      hz-pm-api/src/main/java/com/hz/pm/api/user/manage/UserInfoManage.java
  3. +10
    -0
      hz-pm-api/src/test/java/com/hz/pm/api/todocenter/FlowableTest.java

+ 64
- 0
hz-pm-api/src/main/java/com/hz/pm/api/common/handler/CpuLoadTask.java View File

@@ -0,0 +1,64 @@
package com.hz.pm.api.common.handler;

import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.system.oshi.OshiUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

/**
* <p>
* CpuLoadTask
* </p>
*
* @author WendyYang
* @since 20:30 2024/3/5
*/
@Slf4j
@Profile("prod")
@Configuration
public class CpuLoadTask {

private static final int LIMIT_COUNT = 2000000000;
private final AtomicInteger taskNo = new AtomicInteger(0);

private static final Integer CPU_LIMIT = 20;

private static final ExecutorService FIXED_THREAD_POOL;

static {
int logicalProcessorCount = OshiUtil.getProcessor().getLogicalProcessorCount();
int max = Math.max(logicalProcessorCount * CPU_LIMIT / 100, 1);
FIXED_THREAD_POOL = Executors.newFixedThreadPool(max);
}


@Scheduled(fixedRate = 1, timeUnit = TimeUnit.MINUTES)
public void run() {
double cpuUsage = OshiUtil.getCpuInfo().getUsed();
log.info("当前CPU使用率:{}", cpuUsage);
if (cpuUsage < CPU_LIMIT) {
log.info("提交计算任务:{}", taskNo.incrementAndGet());
FIXED_THREAD_POOL.execute(() -> {
StopWatch watch = new StopWatch();
watch.start();
long sum = Stream.generate(() -> RandomUtil.randomLong(1, LIMIT_COUNT))
.map(w -> w + LIMIT_COUNT / w)
.limit(LIMIT_COUNT)
.mapToLong(Long::longValue)
.sum();
watch.stop();
log.info("总计:" + sum + " 耗时:" + watch.getTotal(TimeUnit.SECONDS));
});
}
}

}

+ 0
- 34
hz-pm-api/src/main/java/com/hz/pm/api/user/manage/UserInfoManage.java View File

@@ -1,9 +1,6 @@
package com.hz.pm.api.user.manage;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.system.oshi.OshiUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@@ -40,12 +37,7 @@ import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
import java.util.stream.Stream;

/**
* @author liuxinxin
@@ -419,30 +411,4 @@ public class UserInfoManage {
return userInfoHelper.getUserFullInfo(userInfo);
}

public static void main(String[] args) {
int logicalProcessorCount = OshiUtil.getProcessor().getLogicalProcessorCount();
int max = (int) Math.max(logicalProcessorCount * 0.25, 1);
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(max);
final int limitCount = 1000000000;
while (true) {
if (System.currentTimeMillis() % 5000 == 0) {
double cpuUsage = OshiUtil.getCpuInfo().getUsed();
System.out.println(cpuUsage);
if (cpuUsage < 40) {
fixedThreadPool.execute(() -> {
StopWatch watch = new StopWatch();
watch.start();
Long sum = Stream.generate(() -> RandomUtil.randomLong(1, limitCount))
.map(w -> w + limitCount / w)
.limit(limitCount)
.collect(Collectors.summingLong(Long::longValue));
watch.stop();
System.out.println("总计:" + sum + " 耗时:" + watch.getTotal(TimeUnit.SECONDS));
});
}
}
}

}

}

+ 10
- 0
hz-pm-api/src/test/java/com/hz/pm/api/todocenter/FlowableTest.java View File

@@ -1,6 +1,7 @@
package com.hz.pm.api.todocenter;

import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hz.pm.api.AppTests;
@@ -329,4 +330,13 @@ public class FlowableTest extends AppTests {
System.out.println(mhUnitService.getOne(query));
}

public static void main(String[] args) {
String path = "/Users/wendy/Desktop/项目下达资金-评审金额 - 副本.xls";
List<Map<String, Object>> maps = ExcelUtil.getReader(path).readAll();
for (Map<String, Object> map : maps) {
String format = "update mh_project set total_money = %s where project_name = '%s' and total_money is null;";
System.out.println(String.format(format, map.get("评审金额"), map.get("项目名称")));
}
}

}

Loading…
Cancel
Save