diff --git a/src/main/java/eu/mikroskeem/uurimustoo/algoritmidetest/Main.java b/src/main/java/eu/mikroskeem/uurimustoo/algoritmidetest/Main.java index c8404a0..57eab91 100644 --- a/src/main/java/eu/mikroskeem/uurimustoo/algoritmidetest/Main.java +++ b/src/main/java/eu/mikroskeem/uurimustoo/algoritmidetest/Main.java @@ -7,11 +7,11 @@ import eu.mikroskeem.shuriken.common.streams.ByteArrays; */ public class Main { public static void main(String... args){ - System.out.println("==== Väikese class faili pakkimise test"); + System.out.println("==== Väikese class faili pakkimise test (100x)"); byte[] classFile = ByteArrays.fromInputStream(Main.class.getResourceAsStream("/Test.class")); Utils.generateReport(classFile); - System.out.println("=== Suure class faili pakkimise test"); + System.out.println("=== Suure class faili pakkimise test (100x)"); byte[] classFile2 = ByteArrays.fromInputStream(Main.class.getResourceAsStream("/TestBig.class")); Utils.generateReport(classFile2); } diff --git a/src/main/java/eu/mikroskeem/uurimustoo/algoritmidetest/Utils.java b/src/main/java/eu/mikroskeem/uurimustoo/algoritmidetest/Utils.java index 08f3d9d..820ed4b 100644 --- a/src/main/java/eu/mikroskeem/uurimustoo/algoritmidetest/Utils.java +++ b/src/main/java/eu/mikroskeem/uurimustoo/algoritmidetest/Utils.java @@ -15,6 +15,8 @@ import java.util.List; import java.util.Objects; import java.util.stream.Collectors; +import static eu.mikroskeem.uurimustoo.algoritmidetest.Utils.DummyCompressor.of; + /** * @author Mark Vainomaa */ @@ -38,10 +40,14 @@ public class Utils { public static CompressorResult executeCompressor(byte[] input, AbstractAlgorithm compressor){ String compressorName = compressor.getName(); + boolean decompressFailed = false; + + /* Measure compressing speed */ Instant start = Instant.now(); byte[] compressed = compressor.compress(input); Instant end = Instant.now(); - /* Verify for sure */ + + /* Measure decompressing speed and validate it */ Instant start2 = null; Instant end2 = null; try { @@ -52,22 +58,32 @@ public class Utils { assert decompressed.length == input.length; } catch (Throwable e){ System.out.println(String.format("Tekkis viga %s algoritmi lahtipakkimisel! %s", compressorName, e.toString())); - compressorName = compressorName + " (katki)"; + decompressFailed = true; } - float diff = (float)input.length / (float)compressed.length; + float sizeDiff = (float)input.length / (float)compressed.length; int compressTime = Duration.between(start, end).getNano(); int decompressTime = start2!=null&&end2!=null?Duration.between(start2, end2).getNano():-1; - return new CompressorResult(compressorName, compressed.length, diff, compressTime, decompressTime); + return new CompressorResult(compressor, decompressFailed, compressed.length, sizeDiff, compressTime, decompressTime); } - public static List executeAllAndSort(byte[] input){ - List compressorResults = getAllCompressors().stream() - .map(c -> executeCompressor(input, c)) - .collect(Collectors.toList()); - compressorResults.add(new CompressorResult("Orginaal", input.length, 1, -1, -1)); // Inject original size - Collections.sort(compressorResults); - Collections.reverse(compressorResults); - return compressorResults; + public static List executeAllNTimes(byte[] input, int n){ + int i = 0; + List lastExecuted = executeAll(input, getAllCompressors()); + while(i < n-1){ + lastExecuted = executeAll(input, lastExecuted.stream() + .filter(r -> !r.isDecompressFailed()) + .map(CompressorResult::getCompressor) + .collect(Collectors.toList())); + i++; + } + lastExecuted.add(of(input.length)); + Collections.sort(lastExecuted); + Collections.reverse(lastExecuted); + return lastExecuted; + } + + public static List executeAll(byte[] input, List compressors){ + return compressors.stream().map(c -> executeCompressor(input, c)).collect(Collectors.toList()); } public static void generateReport(byte[] input){ @@ -75,10 +91,10 @@ public class Utils { System.out.println("----------------------------------------------------------------------------"); System.out.println("| Algoritm | Suurus | Väiksem | Aeg | Aeg (lahti) |"); /* Show other results */ - executeAllAndSort(input).forEach(compressorResult -> { + executeAllNTimes(input, 100).forEach(compressorResult -> { System.out.println("|----------------|--------------|--------------|-------------|-------------|"); System.out.format("| %-12s| %-10s| x%-10.3f| %-9s| %-9s|%n", - compressorResult.getCompressorName(), + compressorResult.getCompressor().getName(), compressorResult.getSize(), compressorResult.getDifference(), (compressorResult.getTimeInNS() / 1000000)+"ms", @@ -91,7 +107,8 @@ public class Utils { @RequiredArgsConstructor @Getter public static class CompressorResult implements Comparable { - private final String compressorName; + private final AbstractAlgorithm compressor; + private final boolean decompressFailed; private final int size; private final float difference; private final int timeInNS; @@ -102,4 +119,12 @@ public class Utils { return new Integer(size).compareTo(o.getSize()); } } + + static class DummyCompressor extends AbstractAlgorithm { + @Getter private final String name = "Orginaal"; + + static CompressorResult of(int size){ + return new CompressorResult(new DummyCompressor(), false, size, 1.0F, -1, -1); + } + } }