Skip to content

Instantly share code, notes, and snippets.

@oli-h
Created January 10, 2019 12:48
Show Gist options
  • Select an option

  • Save oli-h/cf3c07a713e534e44093c56e851d6c3f to your computer and use it in GitHub Desktop.

Select an option

Save oli-h/cf3c07a713e534e44093c56e851d6c3f to your computer and use it in GitHub Desktop.
public class JsonSchemaValidatorPerformanceCompare {
private static final Logger log = LoggerFactory.getLogger(JsonSchemaValidatorPerformanceCompare.class);
public static void main(String[] args) throws Exception {
BasicConfigurator.configure();
String hookSchema = ResourcesUtils.loadResource("gateleen_hooking_schema_hook", true);
final JsonSchema schemaFGE = JsonSchemaFactory.byDefault().getJsonSchema(JsonLoader.fromString(hookSchema));
JsonObject schemaObject = new JsonObject(hookSchema);
// FGE-Lib: 34.3 us
// NT-Lib: 3.8 us
String jsonString = "{" +
" 'methods': ['PUT','POST','DELETE']," +
" 'destination':'/go/somewhere'," +
" 'filter':'.*'," +
" 'headers': [{'header':'x-y', 'value':'gugus'}]," +
" 'queueExpireAfter':30" +
"}";
jsonString = jsonString.replace('\'', '"');
final JsonNode json = JsonLoader.fromString(jsonString);
final Buffer buffer = Buffer.buffer(jsonString);
com.networknt.schema.JsonSchema schemaNT = com.networknt.schema.JsonSchemaFactory.getInstance().getSchema(hookSchema);
while (true) {
long t0 = System.nanoTime();
// Gateleen's own validator (based on FGE)
for (int i = 0; i < 10_000; i++) {
ValidationResult validationResult = Validator.validateStatic(buffer, hookSchema, log);
}
long t1 = System.nanoTime();
// bare FGE with preparsed schema instance
for (int i = 0; i < 10_000; i++) {
ProcessingReport processingMessages = schemaFGE.validateUnchecked(JsonLoader.fromString(jsonString));
// ProcessingReport processingMessages = schemaFGE.validateUnchecked(json);
// Assert.assertTrue(processingMessages.isSuccess());
}
long t2 = System.nanoTime();
// NetworkNT's validator - non preparsed
for (int i = 0; i < 10_000; i++) {
schemaNT = com.networknt.schema.JsonSchemaFactory.getInstance().getSchema(hookSchema);
final Set<ValidationMessage> validateMessages = schemaNT.validate(JsonLoader.fromString(jsonString));
// final Set<ValidationMessage> validateMessages = schema.validate(json);
// Assert.assertEquals(0, validateMessages.size());
}
long t3 = System.nanoTime();
// NetworkNT's validator - preparsed
for (int i = 0; i < 10_000; i++) {
final Set<ValidationMessage> validateMessages = schemaNT.validate(JsonLoader.fromString(jsonString));
// final Set<ValidationMessage> validateMessages = schema.validate(json);
// Assert.assertEquals(0, validateMessages.size());
}
long t4 = System.nanoTime();
long nanosGateleen = (t1 - t0) / 10_000;
long nanosFGEpreparsed = (t2 - t1) / 10_000;
long nanosNetworkNT = (t3 - t2) / 10_000;
long nanosNetworkNTpreparsed = (t4 - t3) / 10_000;
System.out.print("Gateleen (FGE): " + (nanosGateleen / 1000.0) + " us ");
System.out.print("FGE preparsed: " + (nanosFGEpreparsed / 1000.0) + " us ");
System.out.print("NetworkNT: " + (nanosNetworkNT / 1000.0) + " us ");
System.out.print("NetworkNT preparsed: " + (nanosNetworkNTpreparsed / 1000.0) + " us ");
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment