Skip to content

Commit c5531a3

Browse files
committed
added jmh microbenchmark tests
1 parent 5deaa22 commit c5531a3

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed

docs/jmh_results.ods

31.3 KB
Binary file not shown.

docs/jmh_results.png

32 KB
Loading

jbbp/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818
</description>
1919

2020
<dependencies>
21+
<dependency>
22+
<groupId>org.openjdk.jmh</groupId>
23+
<artifactId>jmh-core</artifactId>
24+
<version>1.5.2</version>
25+
<scope>test</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.openjdk.jmh</groupId>
29+
<artifactId>jmh-generator-annprocess</artifactId>
30+
<version>1.5.2</version>
31+
<scope>test</scope>
32+
</dependency>
2133
<dependency>
2234
<groupId>junit</groupId>
2335
<artifactId>junit</artifactId>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2017 Igor Maznitsa.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.igormaznitsa.jbbp.benchmarks;
17+
18+
import com.igormaznitsa.jbbp.JBBPParser;
19+
import com.igormaznitsa.jbbp.io.JBBPBitInputStream;
20+
import com.igormaznitsa.jbbp.mapper.Bin;
21+
import com.igormaznitsa.jbbp.mapper.BinType;
22+
import com.igormaznitsa.jbbp.utils.TargetSources;
23+
import org.junit.Test;
24+
import org.openjdk.jmh.annotations.Benchmark;
25+
26+
import java.io.ByteArrayInputStream;
27+
import java.io.IOException;
28+
import java.util.Random;
29+
30+
31+
/**
32+
* Test set to check productivity of different work modes with parsing data of the same data set.
33+
*/
34+
public class JBBP_Benchmark {
35+
36+
private static final JBBPParser parser = JBBPParser.prepare("ubyte val; data [(val>>1)*(val+3)]{ bit:3 a; bit:3 b; bit:2 c; skip:1; }");
37+
38+
private static final Random RND = new Random(12345);
39+
40+
private static final byte[] DATA;
41+
42+
static {
43+
final int val = 201;
44+
DATA = new byte[1 + ((val >> 1) * (val + 3)) * 2];
45+
RND.nextBytes(DATA);
46+
DATA[0] = (byte) val;
47+
}
48+
49+
public static class InData {
50+
@Bin(name = "a", type = BinType.BIT)
51+
public byte a;
52+
@Bin(name = "b", type = BinType.BIT)
53+
public byte b;
54+
@Bin(name = "c", type = BinType.BIT)
55+
public byte c;
56+
}
57+
58+
public static class Data {
59+
@Bin(name = "val", type = BinType.UBYTE)
60+
public int val;
61+
62+
@Bin(name = "data")
63+
public InData [] data;
64+
}
65+
66+
public static void main(String... args) {
67+
System.out.println("-------------");
68+
System.out.println(parser.convertToSrc(TargetSources.JAVA_1_6, "com.igormaznitsa.jbbp.benchmarks.JBBP_Benchmark_Parser").get(0).getResult().values().iterator().next());
69+
System.out.println("-------------");
70+
}
71+
72+
73+
@Benchmark
74+
public void measureParse_DynamicAndMapping() throws IOException {
75+
parser.parse(DATA).mapTo(Data.class);
76+
}
77+
78+
@Benchmark
79+
public void measureParse_Dynamic() throws IOException {
80+
parser.parse(DATA);
81+
}
82+
83+
@Benchmark
84+
public void measureParse_Static() throws IOException {
85+
new JBBP_Benchmark_Parser().read(new JBBPBitInputStream(new ByteArrayInputStream(DATA)));
86+
}
87+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.igormaznitsa.jbbp.benchmarks;
2+
3+
import com.igormaznitsa.jbbp.model.*;
4+
import com.igormaznitsa.jbbp.io.*;
5+
import com.igormaznitsa.jbbp.compiler.*;
6+
import com.igormaznitsa.jbbp.compiler.tokenizer.*;
7+
import java.io.IOException;
8+
import java.util.*;
9+
10+
/**
11+
* Generated from JBBP script by internal JBBP Class Source Generator
12+
*/
13+
public class JBBP_Benchmark_Parser {
14+
15+
/**
16+
* The Constant contains parser flags
17+
* @see JBBPParser#FLAG_SKIP_REMAINING_FIELDS_IF_EOF
18+
*/
19+
protected static final int _ParserFlags_ = 0;
20+
public static class _ASTRUCT1234 {
21+
22+
public byte a;
23+
public byte b;
24+
public byte c;
25+
private final JBBP_Benchmark_Parser _Root_;
26+
27+
public _ASTRUCT1234 (JBBP_Benchmark_Parser root) {
28+
_Root_ = root;
29+
}
30+
31+
public _ASTRUCT1234 read(final JBBPBitInputStream In) throws IOException {
32+
this.a = In.readBitField(JBBPBitNumber.BITS_3);
33+
this.b = In.readBitField(JBBPBitNumber.BITS_3);
34+
this.c = In.readBitField(JBBPBitNumber.BITS_2);
35+
In.skip(1);
36+
return this;
37+
}
38+
39+
public _ASTRUCT1234 write(final JBBPBitOutputStream Out) throws IOException {
40+
Out.writeBits(this.a,JBBPBitNumber.BITS_3);
41+
Out.writeBits(this.b,JBBPBitNumber.BITS_3);
42+
Out.writeBits(this.c,JBBPBitNumber.BITS_2);
43+
for(int I=0; I<1; I++) Out.write(0);
44+
return this;
45+
}
46+
}
47+
48+
public char val;
49+
protected _ASTRUCT1234 [] _astruct1234;
50+
51+
public JBBP_Benchmark_Parser () {
52+
}
53+
54+
public JBBP_Benchmark_Parser read(final JBBPBitInputStream In) throws IOException {
55+
this.val = (char)(In.readByte() & 0xFF);
56+
if (this._astruct1234 == null || this._astruct1234.length != (((int)this.val>>1)*((int)this.val+3))){ this._astruct1234 = new _ASTRUCT1234[(((int)this.val>>1)*((int)this.val+3))]; for(int I=0;I<(((int)this.val>>1)*((int)this.val+3));I++){ this._astruct1234[I] = new _ASTRUCT1234(this);}}for (int I=0;I<(((int)this.val>>1)*((int)this.val+3));I++){ this._astruct1234[I].read(In); }
57+
return this;
58+
}
59+
60+
public JBBP_Benchmark_Parser write(final JBBPBitOutputStream Out) throws IOException {
61+
Out.write(this.val);
62+
for (int I=0;I<(((int)this.val>>1)*((int)this.val+3));I++){ this._astruct1234[I].write(Out); }
63+
return this;
64+
}
65+
}

0 commit comments

Comments
 (0)