Package jsonij.json.benchmark

Source Code of jsonij.json.benchmark.BenchmarkingJSONParser

/**
* Copyright (C) 2010-2011 J.W.Marsden
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

package jsonij.json.benchmark;

import java.io.IOException;
import java.io.Reader;
import jsonij.json.JSONParser;
import jsonij.json.Value;
import jsonij.parser.ParserException;

/**
* JSON Wrapper to provide benchmarking statistics enabling the JSONiJ Performance
* to be measured.
*
* @author openecho
*/
public class BenchmarkingJSONParser extends JSONParser {

    Benchmark benchmark;

    public BenchmarkingJSONParser() {
        super();
        benchmark = new Benchmark();
    }

    @Override
    public Value parse(java.lang.String target) throws IOException, ParserException {
        long requestStart = System.nanoTime();
        Value value = super.parse(target);
        long requestComplete = System.nanoTime();
        benchmark.reset();
        benchmark.setStartTime(requestStart);
        benchmark.setFinishTime(requestComplete);
        benchmark.setInstance(target);
        benchmark.setSteps(value.nestedSize());
        return value;
    }

    @Override
    public Value parse(Reader targetReader) throws IOException, ParserException {
        long requestStart = System.nanoTime();
        Value value = super.parse(targetReader);
        long requestComplete = System.nanoTime();
        benchmark.reset();
        benchmark.setStartTime(requestStart);
        benchmark.setFinishTime(requestComplete);
        String instanceString = "";
        targetReader.reset();
        int c;
        while((c = targetReader.read()) != -1) {
            instanceString += (char) c;
        }
        benchmark.setInstance(instanceString);
        benchmark.setSteps(value.nestedSize());
        return value;
    }
}
TOP

Related Classes of jsonij.json.benchmark.BenchmarkingJSONParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.