-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from allegro/performance_optimization
Performance optimization
- Loading branch information
Showing
4 changed files
with
219 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...rter/src/jmh/java/tech/allegro/schema/json2avro/converter/JsonAvroConverterBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package tech.allegro.schema.json2avro.converter; | ||
|
||
import org.apache.avro.Schema; | ||
import org.apache.avro.generic.GenericData; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
import org.openjdk.jmh.runner.options.TimeValue; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@State(Scope.Benchmark) | ||
public class JsonAvroConverterBenchmark { | ||
|
||
private JsonAvroConverter converter = new JsonAvroConverter(); | ||
private byte[] messageWithNullField; | ||
private byte[] completeMessage; | ||
private Schema schema; | ||
|
||
@Setup | ||
public void setup() { | ||
converter = new JsonAvroConverter(); | ||
schema = new Schema.Parser().parse( | ||
"{" + | ||
" \"type\" : \"record\"," + | ||
" \"name\" : \"Acme\"," + | ||
" \"fields\" : [" + | ||
" { \"name\" : \"username\", \"type\" : \"string\" }," + | ||
" { \"name\" : \"age\", \"type\" : [\"null\", \"int\"], \"default\": null }]" + | ||
"}"); | ||
|
||
messageWithNullField = "{ \"username\": \"mike\" }".getBytes(); | ||
completeMessage = "{ \"username\": \"mike\", \"age\": 30}".getBytes(); | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.SampleTime) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
public GenericData.Record conversionLatencyForMessageWithNotProvidedOptionalField() { | ||
return converter.convertToGenericDataRecord(messageWithNullField, schema); | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
public GenericData.Record conversionThroughputForMessageWithNotProvidedOptionalField() { | ||
return converter.convertToGenericDataRecord(messageWithNullField, schema); | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.SampleTime) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
public GenericData.Record conversionLatencyForCompleteMessage() { | ||
return converter.convertToGenericDataRecord(completeMessage, schema); | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
public GenericData.Record conversionThroughputForCompleteMessage() { | ||
return converter.convertToGenericDataRecord(completeMessage, schema); | ||
} | ||
|
||
public static void main(String[] args) throws RunnerException { | ||
Options opt = new OptionsBuilder() | ||
.include(".*" + JsonAvroConverterBenchmark.class.getSimpleName() + ".*") | ||
.warmupIterations(2) | ||
.measurementIterations(2) | ||
.measurementTime(TimeValue.seconds(20)) | ||
.warmupTime(TimeValue.seconds(5)) | ||
.forks(1) | ||
.threads(1) | ||
.syncIterations(true) | ||
.build(); | ||
|
||
new Runner(opt).run(); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
converter/src/main/java/tech/allegro/schema/json2avro/converter/AvroTypeExceptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package tech.allegro.schema.json2avro.converter; | ||
|
||
import org.apache.avro.AvroTypeException; | ||
|
||
import java.util.Deque; | ||
import java.util.stream.StreamSupport; | ||
|
||
import static java.util.Spliterator.ORDERED; | ||
import static java.util.Spliterators.spliteratorUnknownSize; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
class AvroTypeExceptions { | ||
static AvroTypeException enumException(Deque<String> fieldPath, String expectedSymbols) { | ||
return new AvroTypeException(new StringBuilder() | ||
.append("Field ") | ||
.append(path(fieldPath)) | ||
.append(" is expected to be of enum type and be one of ") | ||
.append(expectedSymbols) | ||
.toString()); | ||
} | ||
|
||
static AvroTypeException unionException(String fieldName, String expectedTypes, Deque<String> offendingPath) { | ||
return new AvroTypeException(new StringBuilder() | ||
.append("Could not evaluate union, field") | ||
.append(fieldName) | ||
.append("is expected to be one of these: ") | ||
.append(expectedTypes) | ||
.append("If this is a complex type, check if offending field: ") | ||
.append(path(offendingPath)) | ||
.append(" adheres to schema.") | ||
.toString()); | ||
} | ||
|
||
static AvroTypeException typeException(Deque<String> fieldPath, String expectedType) { | ||
return new AvroTypeException(new StringBuilder() | ||
.append("Field ") | ||
.append(path(fieldPath)) | ||
.append(" is expected to be type: ") | ||
.append(expectedType) | ||
.toString()); | ||
} | ||
|
||
private static String path(Deque<String> path) { | ||
return StreamSupport.stream(spliteratorUnknownSize(path.descendingIterator(), ORDERED), false) | ||
.map(Object::toString).collect(joining(".")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters