aboutsummaryrefslogtreecommitdiff
path: root/src/TestAllocSpeed.java
blob: 1a14563d86b570c3dde59b4eed9941b8d9a70cf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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;
    }
}