@@ -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)); | |||||
}); | |||||
} | |||||
} | |||||
} |
@@ -1,9 +1,6 @@ | |||||
package com.hz.pm.api.user.manage; | package com.hz.pm.api.user.manage; | ||||
import cn.hutool.core.collection.CollUtil; | 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.conditions.query.LambdaQueryWrapper; | ||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils; | import com.baomidou.mybatisplus.core.toolkit.StringUtils; | ||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers; | import com.baomidou.mybatisplus.core.toolkit.Wrappers; | ||||
@@ -40,12 +37,7 @@ import org.springframework.transaction.annotation.Transactional; | |||||
import java.time.LocalDateTime; | import java.time.LocalDateTime; | ||||
import java.util.*; | 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.Collectors; | ||||
import java.util.stream.LongStream; | |||||
import java.util.stream.Stream; | |||||
/** | /** | ||||
* @author liuxinxin | * @author liuxinxin | ||||
@@ -419,30 +411,4 @@ public class UserInfoManage { | |||||
return userInfoHelper.getUserFullInfo(userInfo); | 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)); | |||||
}); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} | } |
@@ -1,6 +1,7 @@ | |||||
package com.hz.pm.api.todocenter; | package com.hz.pm.api.todocenter; | ||||
import cn.hutool.json.JSONUtil; | import cn.hutool.json.JSONUtil; | ||||
import cn.hutool.poi.excel.ExcelUtil; | |||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers; | import com.baomidou.mybatisplus.core.toolkit.Wrappers; | ||||
import com.hz.pm.api.AppTests; | import com.hz.pm.api.AppTests; | ||||
@@ -329,4 +330,13 @@ public class FlowableTest extends AppTests { | |||||
System.out.println(mhUnitService.getOne(query)); | 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("项目名称"))); | |||||
} | |||||
} | |||||
} | } |