工作生活

一次fori循环的优化

2019-07-09  本文已影响0人  MagnetoGo

背景

某项目上上传46条excel记录总是报超时的问题,把相关数据下载到测试环境测试,秒传;自己又写了个550条的excel记录上传在开发环境验证,秒传;查看前端限制了10秒,如果10秒都没传完说明太影响用户性能了,所以决定查看后端代码优化一下。
后端代码主要分为六部分:

  1. 解析excel,得到每行结果DemoExcelDTO集合List<DemoExcelDTO>;(此部分可优化代码少)
  2. 检查上级(模块名称)是否存在(fori循环查询数据库比较)
  3. 检查此行数据在否已经存在数据库(fori循环查询数据库比较)
  4. 检查表达式(需要发送http请求校验表达式的元素是否存在)
  5. 循环赋值准备保存数据库的数据(发送http请求获取指标表达式的元素拼装成类似1,2,3的String)
  6. 插入数据库

优化前

这里第1部分的代码已经具有很强的灵活性,并且优化空间少,就不在展示了;第6部分对数据库的操作也没有优化的空间;这里主要想对for循环优化进行思路解析,现贴上优化前的时间。

本次针对与46条评分指标上传的优化(除去最后一步的数据库保存)
------- 1.优化前 第一次8000+第二次9000+--------
批量上传--总消耗时间:10838
其中模块名称重复--消耗时间:2047
其中检查数据库重复--消耗时间:2411
其中检查表达式--消耗时间:2611
其中循环设置指标依赖集合--消耗时间:2508
------- 1 --------

优化前的代码

/**
     * 检查是否在数据库中存在
     */
    private String checkNameAndCodeDuplicateInDB(List<IndexScoreExcelDTO> scoreDTOS, Long scoreModelId) {
        StringBuilder msg = new StringBuilder();
        for (int i = 0; i < scoreDTOS.size(); i++) {
            IndexScoreExcelDTO indexscoreexceldto = scoreDTOS.get(i);
            Boolean exist = this.isExist(indexscoreexceldto.getName(), indexscoreexceldto.getCode(), scoreModelId);
            if (exist) {
                msg.append(String.format("行数为【%s】,错误原因:名称【%s】或编码【%s】已存在%s", i + 1, indexscoreexceldto.getName(),
                        indexscoreexceldto.getCode(), System.lineSeparator()));
            }
        }
        return msg.toString();
    }

        /**
     * 指标表达式检查
     */
    private String checkExpression(List<IndexScoreExcelDTO> scoreDTOS) {
        StringBuilder msg = new StringBuilder();
        String separator = System.lineSeparator();
        for (int i = 0; i < scoreDTOS.size(); i++) {
            IndexScoreExcelDTO indexScoreExcelDTO = scoreDTOS.get(i);
            String expression = indexScoreExcelDTO.getExpression();
            String errorMsg = ExpressionUtils
                    .getErrorMessageIfExpressionIsError(expression, expressionService, indexRemoteManager);
            if (StringUtils.isNotBlank(errorMsg)) {
                msg.append(
                        String.format("第%s行,指标表达式【%s】%s%s", i + 1, expression, errorMsg, separator));
            }
        }
        return msg.toString();
    }

思路解析

1.fori循环连接数据库比较校验 修改为 一次连接,内存比较校验(java8流)

可观察代码fori循环主要是因为需要知道是第几行的excel行有问题,那么我们为什么不能在最开始的时候设置行数,fori循环转换成foreach,或者java8的流呢?所以对excel转化进行了更改,新设置了一个属性excelLineNumber,这样就不必每次太过于关注循环的索引,因为每次连接数据都是比较耗时的,即使有连接池,我们也尽量避免多次连接数据库,消耗性能,除非是实时性非常高的功能;我们项目的实时性并没有那么高,所以可以修改为一次连接数据库,在内存中进行校验,能实现同样的需求,同时缩短了时间,可观察下面的代码,这里采用的java8的forEach,如果不像我们需要按照顺序展示多少行出问题了,还可以采用java8的并行流(parallelStream)进行优化;

/**
     * 检查是否在数据库中存在
     */
    private String checkNameAndCodeDuplicateInDB(List<IndexScoreExcelDTO> scoreDTOS, Long scoreModelId) {
        if(CollectionUtils.isEmpty(scoreDTOS)){
            return null;
        }
        StringBuilder msg = new StringBuilder();
        List<IndexScoreDO> indexScoreDOList = indexScoreDAO.findByScoreModelId(scoreModelId);
        List<String> indexScoreNameList = indexScoreDOList.stream().map(IndexScoreDO::getName).collect(Collectors.toList());
        List<String> indexScoreCodeList = indexScoreDOList.stream().map(IndexScoreDO::getCode).collect(Collectors.toList());
        scoreDTOS.forEach(scoreDTO -> {
            if (indexScoreNameList.contains(scoreDTO.getName().trim()) || indexScoreCodeList.contains(scoreDTO.getCode().trim())) {
                msg.append(String.format("行数为【%s】,错误原因:名称【%s】或编码【%s】已存在%s", scoreDTO.getExcelLineNumber(), scoreDTO.getName(),
                        scoreDTO.getCode(), System.lineSeparator()));
            }
        });
        return msg.toString();
    }

2.利用countDownLatch实现并行处理任务

CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行。CountDownLatch使用场景之一实现最大的并行性,有时我们想同时启动多个线程,实现最大程度的并行处理任务。
本文中的ExpressionUtils.getErrorMessageIfExpressionIsError里循环调用Http请求去另一个工程获取数据,检验是否存在表达式里的元素,如果有46行数据,一行里的表达式存在了3个元素,那么将发送138次Http请求,最大的问题就是这里请求了,后面第5步又同样再去获取元素,又发送了138次http请求,所以这里又很大的优化空间;发送这么多次请求可不可以减少时间呢?利用线程池+countDownLatch并行处理任务;

 public static final ExecutorService POOL = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 5, (new ThreadFactoryBuilder()).setNameFormat("pool-toolkit-%d").build());
  
   /**
     * 指标表达式检查
     */
       private String checkExpression(List<IndexScoreExcelDTO> scoreDTOS) {
        StringBuilder msg = new StringBuilder();
        String separator = System.lineSeparator();
        long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(scoreDTOS.size());

        UserIdentifier userIdentifier = CurrentUserHelper.getUserIdentifier();
        for (int i = 0; i < scoreDTOS.size(); i++) {
            IndexScoreExcelDTO indexScoreExcelDTO = scoreDTOS.get(i);
            String expression = indexScoreExcelDTO.getExpression();
            final int a = i;
            ThreadPool.POOL.execute(() -> {
                AtomContextHolder.setContext(userIdentifier);
                String errorMsg = ExpressionUtils.getErrorMessageIfExpressionIsError(expression, expressionService, indexCallProcessor);
                if (StringUtils.isNotBlank(errorMsg)) {
                    msg.append(String.format("第%s行,指标表达式【%s】%s%s", a + 1, expression, errorMsg, separator));
                }
                countDownLatch.countDown();
            });
        }
        try {
            //  最好设置过期时间,不然可能造成无限等待
            countDownLatch.await(5, TimeUnit.SECONDS);
        } catch (Exception e) {
            logger.error("异步处理指标表达式检查时出错", e);
            if (e.getCause() instanceof RuntimeException) {
                throw  (RuntimeException) e.getCause();
            }else{
                throw new RuntimeException("处理指标表达式检查时异常",e);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("其中检查表达式--消耗时间:" + (end - start));
        return msg.toString();
    }

运行结果

批量上传--总消耗时间:1791
其中模块名称重复--消耗时间:64
其中检查数据库重复--消耗时间:52
其中检查表达式--消耗时间:877
其中循环设置指标依赖集合--消耗时间:26

3.利用countDownLatch+Callable实现并行处理任务

Executor 框架中有一种方法可以实现异步,那就是实现 Callable 接口并重写call方法,并且Callable 可以在任务结束的时候提供一个返回值,Callable 的 call 方法也可以抛出异常;getErrorMessageIfExpressionIsError()方法中有业务断言需要抛出异常;

 /**
     * 指标表达式检查
     */
    private String checkExpression(List<IndexScoreExcelDTO> scoreDTOS) {
     StringBuilder msg = new StringBuilder();
        String separator = System.lineSeparator();
        long start = System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(scoreDTOS.size());

        UserIdentifier userIdentifier = CurrentUserHelper.getUserIdentifier();
        ExecutorService executor = Executors.newFixedThreadPool(5);
        try {
        for (int i = 0; i < scoreDTOS.size(); i++) {
            IndexScoreExcelDTO indexScoreExcelDTO = scoreDTOS.get(i);
            String expression = indexScoreExcelDTO.getExpression();
            final int a = i;
                Callable<String> run = new Callable<String>(){
                    @Override
                    public String call() {
                        AtomContextHolder.setContext(userIdentifier);
                        return ExpressionUtils.getErrorMessageIfExpressionIsError(expression, expressionService, indexCallProcessor);
                    }
                };
                executor.submit(run);
                String errorMsg = run.call();
                if (StringUtils.isNotBlank(errorMsg)) {
                    msg.append(String.format("第%s行,指标表达式【%s】%s%s", a + 1, expression, errorMsg, separator));
                }
                countDownLatch.countDown();
        }
            //  最好设置过期时间,不然可能造成无限等待
            countDownLatch.await(5, TimeUnit.SECONDS);
        } catch (Exception e) {
            logger.error("异步处理指标表达式检查时出错", e);
            if (e.getCause() instanceof RuntimeException) {
                throw  (RuntimeException) e.getCause();
            }else{
                throw new RuntimeException("处理指标表达式检查时异常",e);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("其中检查表达式--消耗时间:" + (end - start));
        return msg.toString();
}

运行结果

批量上传--总消耗时间:3555
其中模块名称重复--消耗时间:64
其中检查数据库重复--消耗时间:54
其中检查表达式--消耗时间:2659
其中循环设置指标依赖集合--消耗时间:32

4.利用CompletableFuture实现异步并行处理任务

CompletableFuture是jdk1.8引入的,里面弥补了jdk1.5的Futrue模型不足,学习谷歌的Google Guava中ListenableFuture和SettableFuture的特征,还提供了其它强大的功能,让Java拥有了完整的非阻塞编程模型:Future、Promise 和 Callback。
CompletableFuture能够将回调放到与任务不同的线程中执行,也能将回调作为继续执行的同步函数,在与任务相同的线程中执行。它避免了传统回调最大的问题,那就是能够将控制流分离到不同的事件处理器中。
indexCallProcessor接口中将http请求结果缓存到了ConcurrentHashMap中,在第5步中先获取缓存中的结果,如果结果为空再发送http请求。

 /**
     * 指标表达式检查 <br/>
     *  <p>1.利用串行Steam流异步多任务执行表达式检查</p>
     *  <p>2.当表达式执行完成后(CompletableFuture.whenComplete),拼接执行结果的错误</p>
     *  <p>3.当所有的CompletableFuture放入allOf,使用get()会阻塞主线程,直到allOf里面的所有线程都执行才唤醒。</p>
     *  <p>4.注:经简单测试(Stream+CompletableFuture异步+指定的线程池) > (Stream+CompletableFuture异步+ForkJoinPool)>(fori+countDownLatch+newFixedThreadPool)</p>
     */
    private String checkExpression(List<IndexScoreExcelDTO> scoreDTOS) {
        if(CollectionUtils.isEmpty(scoreDTOS)){
            return null;
        }
        StringBuilder msg = new StringBuilder();
        String separator = System.lineSeparator();
        UserIdentifier userIdentifier = CurrentUserHelper.getUserIdentifier();
        try {
            CompletableFuture[] expressionCheckFutureList =
                    scoreDTOS.stream().map(indexScoreExcelDTO ->
                        CompletableFuture.supplyAsync(() ->
                        {
                            // 因为是异步线程,需要传入当前user信息
                            ContextHolder.setContext(userIdentifier);
                            return ExpressionUtils.getErrorMessageIfExpressionIsError(indexScoreExcelDTO.getExpression(), expressionService, indexCallProcessor);
                        }, pool) .whenComplete((result, throwable) -> {
                                if (throwable == null && StringUtils.isNotBlank(result)) {
                                    msg.append(String.format("第%s行,指标表达式【%s】%s%s", indexScoreExcelDTO.getExcelLineNumber(), indexScoreExcelDTO.getExpression(), result, separator));
                                }
                            })
                    ).toArray(CompletableFuture[]::new);
            CompletableFuture.allOf(expressionCheckFutureList).get();
        } catch (InterruptedException e) {
            logger.error("异步处理指标表达式检查时的阻塞方法收到中断请求", e.getMessage());
            throw new RuntimeException("处理指标表达式检查时异常",e);
        } catch (ExecutionException e) {
            logger.error("异步处理指标表达式检查时出错", e);
            if (e.getCause() instanceof RuntimeException) {
                throw  (RuntimeException) e.getCause();
            }else{
                throw new RuntimeException("处理指标表达式检查时异常",e);
            }
        }
        return msg.toString();
    }

优化时间

------- 4.串行流+缓存http请求结果 优化后 第二次1926 第三次 2055--------
批量上传--总消耗时间:2050
其中模块名称重复--消耗时间:75
其中检查数据库重复--消耗时间:52
其中检查表达式--消耗时间:956
其中循环设置指标依赖集合--消耗时间:65

总结

本文主要从工作中上传时间过长导致失败的问题,引入了for循环优化的问题,采取了几种尝试性优化,如果我们时间较长的请求, 可以从线程并行、异步考虑,但也要合理采用。

上一篇下一篇

猜你喜欢

热点阅读