Compression time should now look much more useful

This commit is contained in:
szymon 2020-08-02 22:33:11 +02:00
parent 84cc3fdbfc
commit 8f52745cdd
5 changed files with 32 additions and 28 deletions

View File

@ -11,12 +11,14 @@ import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.nio.file.Files;
import java.time.Duration;
import java.time.Instant;
public class LZMACompressor {
public static void createArchive(File in, File out, ServerCommandSource ctx) {
Utilities.info("Starting compression...", ctx);
long start = System.nanoTime();
Instant start = Instant.now();
try (FileOutputStream outStream = new FileOutputStream(out);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outStream);
@ -45,7 +47,6 @@ public class LZMACompressor {
arc.closeArchiveEntry();
} catch (IOException e) {
TextileBackup.LOGGER.error("An exception occurred while trying to compress: " + path.getFileName(), e);
Utilities.sendError("Something went wrong while compressing files!", ctx);
}
});
@ -53,12 +54,9 @@ public class LZMACompressor {
arc.finish();
} catch (IOException e) {
TextileBackup.LOGGER.error("An exception occurred!", e);
Utilities.sendError("Something went wrong while compressing files!", ctx);
}
long end = System.nanoTime();
Utilities.info("Compression took: " + ((end - start) / 1000000000.0) + "s", ctx);
Utilities.info("Compression took: " + Utilities.formatDuration(Duration.between(start, Instant.now())) + " seconds.", ctx);
}
}

View File

@ -11,6 +11,8 @@ import org.at4j.comp.bzip2.BZip2OutputStreamSettings;
import java.io.*;
import java.nio.file.Files;
import java.time.Duration;
import java.time.Instant;
public class ParallelBZip2Compressor {
public static void createArchive(File in, File out, ServerCommandSource ctx, int coreLimit) {
@ -18,7 +20,7 @@ public class ParallelBZip2Compressor {
BZip2OutputStreamSettings settings = new BZip2OutputStreamSettings().setNumberOfEncoderThreads(coreLimit);
long start = System.nanoTime();
Instant start = Instant.now();
try (FileOutputStream fileOutputStream = new FileOutputStream(out);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
@ -47,7 +49,6 @@ public class ParallelBZip2Compressor {
arc.closeArchiveEntry();
} catch (IOException e) {
TextileBackup.LOGGER.error("An exception occurred while trying to compress: " + path.getFileName(), e);
Utilities.sendError("Something went wrong while compressing files!", ctx);
}
});
@ -55,12 +56,9 @@ public class ParallelBZip2Compressor {
arc.finish();
} catch (IOException e) {
TextileBackup.LOGGER.error("An exception occurred!", e);
Utilities.sendError("Something went wrong while compressing files!", ctx);
}
long end = System.nanoTime();
Utilities.info("Compression took: " + ((end - start) / 1000000000.0) + "s", ctx);
Utilities.info("Compression took: " + Utilities.formatDuration(Duration.between(start, Instant.now())) + " seconds.", ctx);
}
}

View File

@ -10,14 +10,14 @@ import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.nio.file.Files;
import java.time.Duration;
import java.time.Instant;
public class ParallelGzipCompressor {
public static void createArchive(File in, File out, ServerCommandSource ctx, int coreLimit) {
Utilities.info("Starting compression...", ctx);
long start = System.nanoTime();
TextileBackup.LOGGER.debug("Compression starts at: {}", start);
Instant start = Instant.now();
try (FileOutputStream outStream = new FileOutputStream(out);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outStream);
@ -46,7 +46,6 @@ public class ParallelGzipCompressor {
arc.closeArchiveEntry();
} catch (IOException e) {
TextileBackup.LOGGER.error("An exception occurred while trying to compress file: " + path, e);
Utilities.sendError("Something went wrong while compressing files!", ctx);
}
});
@ -54,12 +53,9 @@ public class ParallelGzipCompressor {
arc.finish();
} catch (IOException e) {
TextileBackup.LOGGER.error("An exception happened!", e);
Utilities.sendError("Something went wrong while compressing files!", ctx);
}
long end = System.nanoTime();
Utilities.info("Compression took: " + ((end - start) / 1000000000.0) + "s", ctx);
Utilities.info("Compression took: " + Utilities.formatDuration(Duration.between(start, Instant.now())) + " seconds.", ctx);
}
}

View File

@ -9,6 +9,8 @@ import org.apache.commons.compress.parallel.InputStreamSupplier;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.concurrent.*;
import java.util.zip.ZipEntry;
@ -24,9 +26,7 @@ public class ParallelZipCompressor {
public static void createArchive(File in, File out, ServerCommandSource ctx, int coreLimit) {
Utilities.info("Starting compression...", ctx);
long start = System.nanoTime();
TextileBackup.LOGGER.debug("Compression starts at: {}", start);
Instant start = Instant.now();
try (FileOutputStream fileOutputStream = new FileOutputStream(out);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
@ -56,14 +56,11 @@ public class ParallelZipCompressor {
arc.finish();
} catch (IOException | InterruptedException | ExecutionException e) {
TextileBackup.LOGGER.error("An exception happened!", e);
TextileBackup.LOGGER.error("An exception occured!", e);
Utilities.sendError("Something went wrong while compressing files!", ctx);
}
long end = System.nanoTime();
Utilities.info("Compression took: " + ((end - start) / 1000000000.0) + "s", ctx);
Utilities.info("Compression took: " + Utilities.formatDuration(Duration.between(start, Instant.now())) + " seconds.", ctx);
}
static class FileInputStreamSupplier implements InputStreamSupplier {

View File

@ -13,7 +13,9 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
@ -107,6 +109,19 @@ public class Utilities {
return DateTimeFormatter.ofPattern("dd.MM.yyyy_HH-mm-ss");
}
public static String formatDuration(Duration duration) {
DateTimeFormatter formatter;
if(duration.toHours() > 0)
formatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
else if(duration.toMinutes() > 0)
formatter = DateTimeFormatter.ofPattern("mm:ss.SSS");
else
formatter = DateTimeFormatter.ofPattern("ss.SSS");
return LocalTime.ofNanoOfDay(duration.toNanos()).format(formatter);
}
public static void info(String s, ServerCommandSource ctx){
if(ctx != null && ctx.getEntity() != null)
ctx.sendFeedback(new LiteralText(s), false);