de.unentscheidbar.csv2.ProfileMe.java Source code

Java tutorial

Introduction

Here is the source code for de.unentscheidbar.csv2.ProfileMe.java

Source

/*
 * Copyright 2015 Daniel Huss
 *
 * 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 de.unentscheidbar.csv2;

import java.io.File;
import java.util.concurrent.TimeUnit;

import com.google.common.base.Stopwatch;

import de.unentscheidbar.csv2.ParserBenchmark.Dataset;

@SuppressWarnings("javadoc")
public final class ProfileMe {

    private static final Dataset dataset;
    private static final String csv;

    static {
        try {
            dataset = Dataset.random;
            csv = dataset.get(2000000);
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }

    public static void main(String[] args) throws Exception {

        File gofile = new File("go");
        while (!gofile.exists()) {
            System.out.println("Waiting...");
            Thread.sleep(1000);
        }
        double kibPerRun = csv.length() / 2048.0;
        System.out.println("go " + kibPerRun + " kib");
        while (gofile.exists()) {
            Stopwatch sw = Stopwatch.createStarted();
            long ops = operation();
            long micros = sw.elapsed(TimeUnit.MICROSECONDS);
            System.out.println("@" + ((ops * kibPerRun / micros) * 1_000_000 / 1024.0) + " mbps");
        }
        System.out.println("exiting");
    }

    static long operation() throws Exception {

        Stopwatch sw = Stopwatch.createStarted();
        long ops = 0;
        while (sw.elapsed(TimeUnit.SECONDS) < 1) {
            ops++;
            for (CsvRow row : CsvFormat.defaultFormat().parser().parse(csv)) {
                if (row.asList().size() < 0)
                    throw new AssertionError();
            }
            // for ( CSVRecord row : CSVFormat.DEFAULT.parse( new StringReader( csv ) ) ) {
            // if ( row.size() < 0 ) throw new AssertionError();
            // }
        }
        return ops;
    }
}