aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2017-10-13 22:21:40 +0300
committerLasse Collin <lasse.collin@tukaani.org>2017-12-28 19:52:20 +0200
commitf4c0a49d8b69e1f94b50a790f2d42a88782a4beb (patch)
tree46751bd64860ffa1319b7d19bc54182ab5c5b3e4
parent28ca97fc3f9153460b0b88054351f959a36d1dfb (diff)
downloadxz-java-f4c0a49d8b69e1f94b50a790f2d42a88782a4beb.tar.gz
Add TestAllocSpeed to test the array caching.
-rw-r--r--build.xml8
-rw-r--r--fileset-src.txt1
-rw-r--r--src/TestAllocSpeed.java106
3 files changed, 115 insertions, 0 deletions
diff --git a/build.xml b/build.xml
index b1591d7..508d58f 100644
--- a/build.xml
+++ b/build.xml
@@ -74,6 +74,14 @@
</manifest>
</jar>
+ <jar destfile="${jar_dir}/TestAllocSpeed.jar" basedir="${classes_dir}"
+ includes="TestAllocSpeed.class">
+ <manifest>
+ <attribute name="Main-Class" value="TestAllocSpeed"/>
+ <attribute name="Class-Path" value="xz.jar"/>
+ </manifest>
+ </jar>
+
<jar destfile="${jar_dir}/XZEncDemo.jar" basedir="${classes_dir}"
includes="XZEncDemo.class">
<manifest>
diff --git a/fileset-src.txt b/fileset-src.txt
index 684b6a7..8c539a6 100644
--- a/fileset-src.txt
+++ b/fileset-src.txt
@@ -1,3 +1,4 @@
+src/TestAllocSpeed.java
src/LZMADecDemo.java
src/LZMAEncDemo.java
src/XZDecDemo.java
diff --git a/src/TestAllocSpeed.java b/src/TestAllocSpeed.java
new file mode 100644
index 0000000..1a14563
--- /dev/null
+++ b/src/TestAllocSpeed.java
@@ -0,0 +1,106 @@
+/*
+ * TestAllocSpeed
+ *
+ * Author: Lasse Collin <lasse.collin@tukaani.org>
+ *
+ * This file has been put into the public domain.
+ * You can do whatever you want with this file.
+ */
+
+/*
+ * Usage:
+ * time java -jar build/jar/TestAllocSpeed.jar MODE ITERS THREADS < FILE
+ * where
+ * MODE is "true" for compression or "false" for decompression,
+ * ITERS is the number of iterations to done by each thread,
+ * THREADS is the number of threads, and
+ * FILE is the input file (preferably tiny, but at most 1 MiB).
+ *
+ * Each thread has a different random seed so in compression mode each
+ * thread will use different options in different order. This way the
+ * ArrayCache gets more diverse load.
+ *
+ * Examples:
+ * time java -jar build/jar/TestAllocSpeed.jar true 1000 4 < README
+ * time java -jar build/jar/TestAllocSpeed.jar false 10000 4 < foo.xz
+ */
+
+import java.io.*;
+import java.util.Random;
+import org.tukaani.xz.*;
+
+class TestAllocSpeed implements Runnable {
+ private static boolean compressing;
+ private static int repeats;
+ private static final byte[] testdata = new byte[1 << 20];
+ private static int testdataSize;
+ private static volatile IOException exception = null;
+
+ private final Random rng;
+
+ public TestAllocSpeed(long seed) {
+ rng = new Random(seed);
+ }
+
+ private void compress() throws IOException {
+ ByteArrayOutputStream byteStream = new ByteArrayOutputStream(
+ testdataSize + 1024);
+ LZMA2Options options = new LZMA2Options();
+ options.setDictSize(1 << (16 + rng.nextInt(6)));
+
+ for (int i = 0; i < repeats; ++i) {
+ XZOutputStream out = new XZOutputStream(byteStream, options);
+ out.write(testdata, 0, testdataSize);
+ out.finish();
+ }
+ }
+
+ private void decompress() throws IOException {
+ ByteArrayInputStream byteStream = new ByteArrayInputStream(
+ testdata, 0, testdataSize);
+ byte[] outbuf = new byte[8192];
+
+ for (int i = 0; i < repeats; ++i) {
+ byteStream.reset();
+ XZInputStream in = new XZInputStream(byteStream);
+ while (in.read(outbuf) > 0) {}
+ }
+ }
+
+ public void run() {
+ try {
+ if (compressing) {
+ compress();
+ } else {
+ decompress();
+ }
+ } catch (IOException e) {
+ exception = e;
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ compressing = Boolean.parseBoolean(args[0]);
+ repeats = Integer.parseInt(args[1]);
+ final int threadCount = Integer.parseInt(args[2]);
+
+ if (threadCount < 1 || threadCount > 64)
+ throw new Exception("Thread count must be 1-64");
+
+ testdataSize = System.in.read(testdata);
+
+ ArrayCache.setDefaultCache(BasicArrayCache.getInstance());
+
+ Thread[] threads = new Thread[threadCount];
+ for (int i = 0; i < threadCount; ++i) {
+ threads[i] = new Thread(new TestAllocSpeed(i));
+ threads[i].start();
+ }
+
+ for (int i = 0; i < threadCount; ++i)
+ threads[i].join();
+
+ if (exception != null)
+ throw exception;
+ }
+}