Skip to content

Commit 96c8e03

Browse files
committed
Tweaks.
1 parent 321c97d commit 96c8e03

File tree

8 files changed

+145
-122
lines changed

8 files changed

+145
-122
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ tasks.test {
3030
jmh {
3131
jmhVersion = "1.37"
3232
includes = listOf(".Benchmark.")
33-
benchmarkMode = listOf("thrpt")
33+
benchmarkMode = listOf("avgt")
3434
fork = 2
3535
warmup = "10s"
3636
warmupIterations = 3
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.stringcompressor;
2+
3+
/**
4+
* @author Jean Dannemann Carone
5+
*/
6+
public class BaseBenchmark {
7+
8+
public static byte[] generate10MbString(byte[] charset) {
9+
return BaseTest.generateRandomString(10 * 1024 * 1024, charset);
10+
}
11+
12+
}

src/jmh/java/com/stringcompressor/FiveBitAsciiCompressorBenchmarAk.java

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.stringcompressor;
2+
3+
import org.openjdk.jmh.annotations.Benchmark;
4+
import org.openjdk.jmh.annotations.Level;
5+
import org.openjdk.jmh.annotations.Scope;
6+
import org.openjdk.jmh.annotations.Setup;
7+
import org.openjdk.jmh.annotations.State;
8+
import org.openjdk.jmh.runner.Runner;
9+
import org.openjdk.jmh.runner.RunnerException;
10+
import org.openjdk.jmh.runner.options.OptionsBuilder;
11+
12+
import static com.stringcompressor.FiveBitAsciiCompressor.DEFAULT_5BIT_CHARSET;
13+
14+
/**
15+
* @author Jean Dannemann Carone
16+
*/
17+
@State(Scope.Benchmark)
18+
public class FiveBitAsciiCompressorBenchmark extends BaseBenchmark {
19+
20+
AsciiCompressor compressor;
21+
byte[] toCompress;
22+
byte[] toDecompress;
23+
24+
@Setup(Level.Trial)
25+
public void setUp() {
26+
compressor = new FiveBitAsciiCompressor();
27+
toCompress = generate10MbString(DEFAULT_5BIT_CHARSET);
28+
toDecompress = compressor.compress(toCompress);
29+
}
30+
31+
@Benchmark
32+
public byte[] compress10Mb() {
33+
return compressor.compress(toCompress);
34+
}
35+
36+
@Benchmark
37+
public byte[] decompress10Mb() {
38+
return compressor.decompress(toDecompress);
39+
}
40+
41+
/**
42+
* For debugging (see JMH in build.gradle.kts).
43+
*/
44+
public static void main(String[] args) throws RunnerException {
45+
new Runner(
46+
new OptionsBuilder()
47+
.include(FiveBitAsciiCompressorBenchmark.class.getSimpleName())
48+
.forks(0)
49+
.build())
50+
.run();
51+
}
52+
53+
}

src/jmh/java/com/stringcompressor/FourBitAsciiCompressorBenchmark.java

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,42 @@
11
package com.stringcompressor;
22

33
import org.openjdk.jmh.annotations.Benchmark;
4+
import org.openjdk.jmh.annotations.Level;
5+
import org.openjdk.jmh.annotations.Scope;
6+
import org.openjdk.jmh.annotations.Setup;
7+
import org.openjdk.jmh.annotations.State;
48
import org.openjdk.jmh.runner.Runner;
59
import org.openjdk.jmh.runner.RunnerException;
610
import org.openjdk.jmh.runner.options.OptionsBuilder;
711

8-
import java.util.Random;
9-
1012
import static com.stringcompressor.FourBitAsciiCompressor.DEFAULT_4BIT_CHARSET;
1113

1214
/**
1315
* @author Jean Dannemann Carone
1416
*/
15-
public class FourBitAsciiCompressorBenchmark {
16-
17-
private static final AsciiCompressor COMPRESSOR = new FourBitAsciiCompressor();
18-
private static final int MAX_STRINGS = 256; // Must be a power of 2 for bitwise module.
19-
private static final byte[][] INPUT_STRINGS = new byte[MAX_STRINGS][];
20-
private static final byte[][] COMPRESSED_STRINGS = new byte[MAX_STRINGS][];
21-
private static final Random RANDOM = new Random();
22-
23-
private static int index;
24-
25-
static {
26-
COMPRESSOR.setPreserveOriginal(true);
27-
COMPRESSOR.setThrowException(false);
28-
29-
int charSetLen = DEFAULT_4BIT_CHARSET.length;
30-
31-
for (int i = 0; i < MAX_STRINGS; i++) {
32-
byte[] string = new byte[10 * 1024 * 1024]; // 10 MB each string.
33-
34-
for (int j = 0, len = string.length; j < len; j++)
35-
string[j] = DEFAULT_4BIT_CHARSET[RANDOM.nextInt(charSetLen)];
36-
37-
INPUT_STRINGS[i] = string;
38-
COMPRESSED_STRINGS[i] = COMPRESSOR.compress(string);
39-
}
40-
17+
@State(Scope.Benchmark)
18+
public class FourBitAsciiCompressorBenchmark extends BaseBenchmark {
19+
20+
AsciiCompressor compressor;
21+
byte[] toCompress;
22+
byte[] toDecompress;
23+
24+
@Setup(Level.Trial)
25+
public void setUp() {
26+
compressor = new FourBitAsciiCompressor();
27+
toCompress = generate10MbString(DEFAULT_4BIT_CHARSET);
28+
toDecompress = compressor.compress(toCompress);
4129
}
4230

43-
// @Benchmark
44-
// public void baseline() {
45-
// }
46-
4731
@Benchmark
48-
public byte[] compressStrings() {
49-
return COMPRESSOR.compress(INPUT_STRINGS[index++ & 0x7FFFFFFF & MAX_STRINGS - 1]);
32+
public byte[] compress10Mb() {
33+
return compressor.compress(toCompress);
5034
}
5135

52-
// @Benchmark
53-
// public byte[] decompressStrings() {
54-
// return COMPRESSOR.decompress(COMPRESSED_STRINGS[index++ & 0x7FFFFFFF & MAX_STRINGS - 1]);
55-
// }
36+
@Benchmark
37+
public byte[] decompress10Mb() {
38+
return compressor.decompress(toDecompress);
39+
}
5640

5741
/**
5842
* For debugging (see JMH in build.gradle.kts).
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.stringcompressor;
2+
3+
import org.openjdk.jmh.annotations.Benchmark;
4+
import org.openjdk.jmh.annotations.Level;
5+
import org.openjdk.jmh.annotations.Scope;
6+
import org.openjdk.jmh.annotations.Setup;
7+
import org.openjdk.jmh.annotations.State;
8+
import org.openjdk.jmh.runner.Runner;
9+
import org.openjdk.jmh.runner.RunnerException;
10+
import org.openjdk.jmh.runner.options.OptionsBuilder;
11+
12+
import static com.stringcompressor.SixBitAsciiCompressor.DEFAULT_6BIT_CHARSET;
13+
14+
/**
15+
* @author Jean Dannemann Carone
16+
*/
17+
@State(Scope.Benchmark)
18+
public class SixBitAsciiCompressorBenchmark extends BaseBenchmark {
19+
20+
AsciiCompressor compressor;
21+
byte[] toCompress;
22+
byte[] toDecompress;
23+
24+
@Setup(Level.Trial)
25+
public void setUp() {
26+
compressor = new SixBitAsciiCompressor();
27+
toCompress = generate10MbString(DEFAULT_6BIT_CHARSET);
28+
toDecompress = compressor.compress(toCompress);
29+
}
30+
31+
@Benchmark
32+
public byte[] compress10Mb() {
33+
return compressor.compress(toCompress);
34+
}
35+
36+
@Benchmark
37+
public byte[] decompress10Mb() {
38+
return compressor.decompress(toDecompress);
39+
}
40+
41+
/**
42+
* For debugging (see JMH in build.gradle.kts).
43+
*/
44+
public static void main(String[] args) throws RunnerException {
45+
new Runner(
46+
new OptionsBuilder()
47+
.include(SixBitAsciiCompressorBenchmark.class.getSimpleName())
48+
.forks(0)
49+
.build())
50+
.run();
51+
}
52+
53+
}

src/main/java/com/stringcompressor/AsciiCompressor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public abstract class AsciiCompressor {
2121
* the original input string, encoding it and making it unusable. To avoid
2222
* this behavior and compress a copy of the original, set this to <code>true</code>.
2323
*/
24-
protected boolean preserveOriginal = true; // TODO: Set this false after updating benchmarks.
24+
protected boolean preserveOriginal;
2525

2626
/**
2727
* Throw validation exceptions while compressing. Useful for debug but not

src/main/java/com/stringcompressor/exception/CharacterNotSupportedException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.stringcompressor.exception;
22

3+
/**
4+
* @author Jean Dannemann Carone
5+
*/
36
public class CharacterNotSupportedException extends RuntimeException {
47

58
public CharacterNotSupportedException(String message) {

0 commit comments

Comments
 (0)