com.github.fge.jsonschema.NewAPIPerfTest.java Source code

Java tutorial

Introduction

Here is the source code for com.github.fge.jsonschema.NewAPIPerfTest.java

Source

/*
 * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com)
 *
 * This software is dual-licensed under:
 *
 * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
 *   later version;
 * - the Apache Software License (ASL) version 2.0.
 *
 * The text of this file and of both licenses is available at the root of this
 * project or, if you have the jar distribution, in directory META-INF/, under
 * the names LGPL-3.0.txt and ASL-2.0.txt respectively.
 *
 * Direct link to the sources:
 *
 * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
 * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
 */

package com.github.fge.jsonschema;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JacksonUtils;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;

import java.io.IOException;
import java.util.Map;

public final class NewAPIPerfTest {
    private static final JsonSchema SCHEMA;

    static {
        try {
            SCHEMA = JsonSchemaFactory.byDefault().getJsonSchema(SchemaVersion.DRAFTV4.getSchema());
        } catch (ProcessingException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    private NewAPIPerfTest() {
    }

    public static void main(final String... args) throws IOException, ProcessingException {
        final JsonNode googleAPI = JsonLoader.fromResource("/other/google-json-api.json");
        final Map<String, JsonNode> googleSchemas = JacksonUtils.asMap(googleAPI.get("schemas"));

        long begin, current;
        begin = System.currentTimeMillis();
        doValidate(googleSchemas, -1);
        current = System.currentTimeMillis();

        System.out.println("Initial validation :" + (current - begin) + " ms");

        begin = System.currentTimeMillis();
        for (int i = 0; i < 500; i++) {
            doValidate(googleSchemas, i);
            if (i % 20 == 0) {
                current = System.currentTimeMillis();
                System.out.println(String.format("Iteration %d (in %d ms)", i, current - begin));
            }
        }

        final long end = System.currentTimeMillis();
        System.out.println("END -- time in ms: " + (end - begin));
        System.exit(0);
    }

    private static void doValidate(final Map<String, JsonNode> schemas, final int i) throws ProcessingException {
        String name;
        JsonNode value;
        ProcessingReport report;

        for (final Map.Entry<String, JsonNode> entry : schemas.entrySet()) {
            name = entry.getKey();
            value = entry.getValue();
            report = SCHEMA.validate(value);
            if (!report.isSuccess()) {
                System.err.println("ERROR: schema " + name + " did not " + "validate (iteration " + i + ')');
                System.exit(1);
            }
        }
    }
}