业务实战场景(十九)Java复制压缩文件25倍性能提升

2023-09-12  本文已影响0人  后来丶_a24d

目录


背景


压缩文件处理

pigz并发压缩性能测试,机器配置高的

  1. real:实际经过的时间(从计时开始到程序完成的总时间)
  2. user:用户态模式下CPU使用的时间
  3. sys:内核态模式下CPU使用的时间

pigz并发压缩性能测试,机器配置一般的

pigz说明

apache common的gzip,单线程处理

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;

import java.io.*;
import java.util.List;
public static void archive(File srcFile, File destFile) throws Exception {
        TarArchiveOutputStream taos = new TarArchiveOutputStream(new FileOutputStream(destFile));
        archive(srcFile, taos, BASE_DIR);
        taos.flush();
        taos.close();
    }
private static void archiveFile(File file, TarArchiveOutputStream taos, String dir) throws Exception {
        TarArchiveEntry entry = new TarArchiveEntry(dir + file.getName());
        entry.setSize(file.length());
        taos.putArchiveEntry(entry);
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            int count;
            byte[] data = new byte[BUFFER];
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                taos.write(data, 0, count);
            }
        } catch (IOException e) {

        } finally {
            if (bis != null) {
                bis.close();
            }
        }
        taos.closeArchiveEntry();
    }
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StopWatch;

import java.io.*;

public static void compressFile(File file, boolean delete) throws Exception {
        try (FileInputStream in = new FileInputStream(file); FileOutputStream fout = new FileOutputStream(file.getPath() + EXT); BufferedOutputStream out
                = new BufferedOutputStream(fout); GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(out)) {
            final byte[] buffer = new byte[5 * 1024 * 1024];
            int n = 0;
            while (-1 != (n = in.read(buffer))) {
                gzOut.write(buffer, 0, n);
            }
            gzOut.close();
            in.close();
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            if (delete && file.delete()) {
                logger.info("删除");
            }
            stopWatch.stop();
            System.out.println("delete time ____" + stopWatch.getTotalTimeMillis());
        }

    }
 <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.21</version>
        </dependency>

        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.9.9</version>
        </dependency>
 <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.example.demo.GzTest</mainClass> <!-- replace with your main class -->
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
 // 上面给的
        String tarPath = archive(coverFile);
        System.out.println(" arch time __" + (System.currentTimeMillis() - start));
        long startCompress = System.currentTimeMillis();
        // 上面给的 主要看这块执行时间对比
        compress(tarPath, true);
        System.out.println("compress time __" + (System.currentTimeMillis() - startCompress));
        System.out.println("compress all time __" + (System.currentTimeMillis() - start));
java -jar 你的jar包测
4核.png

其他

byte[] data = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
    out.write(data, 0, count);
}

参考文章

上一篇 下一篇

猜你喜欢

热点阅读