Search in sources :

Example 6 with FileWriterWithEncoding

use of org.apache.commons.io.output.FileWriterWithEncoding in project polymap4-core by Polymap4.

the class Graphic method equipSvg.

public static void equipSvg(File f) {
    try {
        // read/parse
        String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
        Document doc = factory.createDocument(f.toURI().toURL().toString());
        // modify
        List<String> NAMES = Lists.newArrayList("circle", "ellipse", "line", "mesh", "path", "polygon", "polyline", "rect");
        NodeList elms = doc.getElementsByTagName("*");
        for (int i = 0; i < elms.getLength(); i++) {
            Node node = elms.item(i);
            if (node instanceof Element && NAMES.contains(node.getNodeName())) {
                ((Element) node).setAttribute("fill", "param(fill-color)");
                ((Element) node).setAttribute("fill-opacity", "param(fill-opacity)");
                ((Element) node).setAttribute("stroke", "param(stroke-color)");
                ((Element) node).setAttribute("stroke-opacity", "param(stroke-opacity)");
                ((Element) node).setAttribute("stroke-width", "param(stroke-width)");
            }
        }
        // write
        try (FileWriterWithEncoding out = new FileWriterWithEncoding(f, "UTF-8")) {
            SVGTranscoder t = new SVGTranscoder();
            t.transcode(new TranscoderInput(doc), new TranscoderOutput(out));
        }
        log.info("SVG: " + FileUtils.readFileToString(f, "UTF-8"));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) SVGTranscoder(org.apache.batik.transcoder.svg2svg.SVGTranscoder) IOException(java.io.IOException) FileWriterWithEncoding(org.apache.commons.io.output.FileWriterWithEncoding) TranscoderOutput(org.apache.batik.transcoder.TranscoderOutput) TranscoderInput(org.apache.batik.transcoder.TranscoderInput) SAXSVGDocumentFactory(org.apache.batik.dom.svg.SAXSVGDocumentFactory)

Example 7 with FileWriterWithEncoding

use of org.apache.commons.io.output.FileWriterWithEncoding in project jackrabbit by apache.

the class AbstractPerformanceTest method writeReport.

private void writeReport(String test, String name, DescriptiveStatistics statistics) throws IOException {
    File report = new File("target", test + ".txt");
    boolean needsPrefix = !report.exists();
    PrintWriter writer = new PrintWriter(new FileWriterWithEncoding(report, "UTF-8", true));
    try {
        if (needsPrefix) {
            writer.format("# %-34.34s     min     10%%     50%%     90%%     max%n", test);
        }
        writer.format("%-36.36s  %6.0f  %6.0f  %6.0f  %6.0f  %6.0f%n", name, statistics.getMin(), statistics.getPercentile(10.0), statistics.getPercentile(50.0), statistics.getPercentile(90.0), statistics.getMax());
    } finally {
        writer.close();
    }
}
Also used : FileWriterWithEncoding(org.apache.commons.io.output.FileWriterWithEncoding) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 8 with FileWriterWithEncoding

use of org.apache.commons.io.output.FileWriterWithEncoding in project sling by apache.

the class ReportLogger method writeReportMethodLevel.

/**
     * Write report for method level tests
     *
     * @param resultFileName the name of the result file (without extension)
     * @param testSuiteName the name of the test suite name
     * @param testCaseName
     * @param className
     * @param methodName
     * @param min
     * @param percentile10
     * @param percentile50
     * @param percentile90
     * @param max

     */
private static void writeReportMethodLevel(String resultFileName, String testSuiteName, String testCaseName, String className, String methodName, double min, double percentile10, double percentile50, double percentile90, double max, boolean showDecimals) throws IOException {
    File report = getReportFile(resultFileName, ".txt");
    boolean needsPrefix = !report.exists();
    PrintWriter writer = new PrintWriter(new FileWriterWithEncoding(report, "UTF-8", true));
    try {
        if (needsPrefix) {
            writer.format("%-40.40s|%-120.120s|%-80.80s|%-40.40s|      DateTime      |  min  |   10%%   |   50%%   |   90%%   |   max%n", "Test Suite", "Test Case", "Test Class", "Test Method");
        }
        writer.format(showDecimals ? "%-40.40s|%-120.120s|%-80.80s|%-40.40s|%-20.20s|%7.2f|%9.2f|%9.2f|%9.2f|%9.2f%n" : "%-40.40s|%-120.120s|%-80.80s|%-40.40s|%-20.20s|%7.0f|%9.0f|%9.0f|%9.0f|%9.0f%n", testSuiteName, (testCaseName.length() < 120) ? (testCaseName) : (testCaseName.substring(0, 115) + "[...]"), className, methodName, getDate(), min, percentile10, percentile50, percentile90, max);
    } finally {
        writer.close();
    }
}
Also used : FileWriterWithEncoding(org.apache.commons.io.output.FileWriterWithEncoding) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 9 with FileWriterWithEncoding

use of org.apache.commons.io.output.FileWriterWithEncoding in project sling by apache.

the class ReportLogger method writeReportClassLevel.

/**
     * Write report for class level tests
     *
     * @param resultFileName the name of the result file (without extension)
     * @param testSuiteName the name of the test suite name
     * @param min
     * @param percentile10
     * @param percentile50
     * @param percentile90
     * @param max

     */
private static void writeReportClassLevel(String resultFileName, String testSuiteName, double min, double percentile10, double percentile50, double percentile90, double max) throws IOException {
    File report = getReportFile(resultFileName, ".txt");
    boolean needsPrefix = !report.exists();
    PrintWriter writer = new PrintWriter(new FileWriterWithEncoding(report, "UTF-8", true));
    try {
        if (needsPrefix) {
            writer.format("# %-50.50s     min     10%%     50%%     90%%     max%n", resultFileName);
        }
        writer.format("%-52.52s  %6.0f  %6.0f  %6.0f  %6.0f  %6.0f%n", testSuiteName, min, percentile10, percentile50, percentile90, max);
    } finally {
        writer.close();
    }
}
Also used : FileWriterWithEncoding(org.apache.commons.io.output.FileWriterWithEncoding) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 10 with FileWriterWithEncoding

use of org.apache.commons.io.output.FileWriterWithEncoding in project ANNIS by korpling.

the class AnnisRunner method doBenchmark.

public void doBenchmark(String benchmarkCount) {
    int count = Integer.parseInt(benchmarkCount);
    out.println("---> executing " + benchmarks.size() + " queries " + count + " times");
    AnnisRunner.OS currentOS = AnnisRunner.OS.other;
    try {
        currentOS = AnnisRunner.OS.valueOf(System.getProperty("os.name").toLowerCase());
    } catch (IllegalArgumentException ex) {
    }
    List<AnnisRunner.Benchmark> session = new ArrayList<>();
    // create sql + plan for each query and create count copies for each benchmark
    for (Benchmark benchmark : benchmarks) {
        if (clearCaches) {
            resetCaches(currentOS);
        }
        SqlGeneratorAndExtractor<QueryData, ?> generator = getGeneratorForQueryFunction(benchmark.functionCall);
        benchmark.sql = getGeneratorForQueryFunction(benchmark.functionCall).toSql(benchmark.queryData);
        out.println("---> SQL query for: " + benchmark.functionCall);
        out.println(benchmark.sql);
        try {
            benchmark.plan = queryDao.explain(generator, benchmark.queryData, false);
            out.println("---> query plan for: " + benchmark.functionCall);
            out.println(benchmark.plan);
        } catch (RuntimeException ex) {
            // nested DataAccessException would be better
            out.println("---> query plan failed for " + benchmark.functionCall);
        }
        benchmark.bestTimeInMilliseconds = Long.MAX_VALUE;
        benchmark.worstTimeInMilliseconds = Long.MIN_VALUE;
        out.println("---> running query sequentially " + SEQUENTIAL_RUNS + " times");
        String options = benchmarkOptions(benchmark.queryData);
        for (int i = 0; i < SEQUENTIAL_RUNS; ++i) {
            if (i > 0) {
                out.print(", ");
            }
            boolean error = false;
            long start = new Date().getTime();
            try {
                queryDao.executeQueryFunction(benchmark.queryData, generator);
            } catch (RuntimeException ex) {
                error = true;
            }
            long end = new Date().getTime();
            long runtime = end - start;
            if (benchMode == BenchmarkMode.sequential_random) {
                // record the runtime and other benchmark values when the sequental run is counted
                benchmark.sumTimeInMilliseconds += runtime;
                benchmark.values.add(runtime);
                benchmark.bestTimeInMilliseconds = Math.min(benchmark.bestTimeInMilliseconds, runtime);
                benchmark.worstTimeInMilliseconds = Math.max(benchmark.worstTimeInMilliseconds, runtime);
                ++benchmark.runs;
                if (error) {
                    ++benchmark.errors;
                }
            }
            out.print(runtime + " ms");
        }
        out.println();
        out.println(benchmark.bestTimeInMilliseconds + " ms best time for '" + benchmark.functionCall + ("".equals(options) ? "'" : "' with " + options));
        session.addAll(Collections.nCopies(count, benchmark));
    }
    // the others
    if (clearCaches) {
        resetCaches(currentOS);
    }
    // shuffle the benchmark queries
    Collections.shuffle(session);
    out.println();
    out.println("---> running queries in random order");
    // execute the random query order, record test times
    for (AnnisRunner.Benchmark benchmark : session) {
        if (benchmark.errors >= 3) {
            continue;
        }
        boolean error = false;
        SqlGeneratorAndExtractor<QueryData, ?> generator = getGeneratorForQueryFunction(benchmark.functionCall);
        long start = new Date().getTime();
        try {
            queryDao.executeQueryFunction(benchmark.queryData, generator);
        } catch (RuntimeException e) {
            error = true;
        }
        long end = new Date().getTime();
        long runtime = end - start;
        benchmark.sumTimeInMilliseconds += runtime;
        benchmark.values.add(runtime);
        benchmark.bestTimeInMilliseconds = Math.min(benchmark.bestTimeInMilliseconds, runtime);
        benchmark.worstTimeInMilliseconds = Math.max(benchmark.worstTimeInMilliseconds, runtime);
        ++benchmark.runs;
        if (error) {
            ++benchmark.errors;
        }
        String options = benchmarkOptions(benchmark.queryData);
        out.println(runtime + " ms for '" + benchmark.functionCall + ("".equals(options) ? "'" : "' with " + options) + (error ? " ERROR" : ""));
    }
    // compute average runtime for each query
    out.println();
    out.println("---> benchmark complete");
    for (AnnisRunner.Benchmark benchmark : benchmarks) {
        String options = benchmarkOptions(benchmark.queryData);
        out.println(benchmark.getMedian() + " ms (median for " + benchmark.runs + " runs" + (benchmark.errors > 0 ? ", " + benchmark.errors + " errors)" : ")") + " for '" + benchmark.functionCall + ("".equals(options) ? "'" : "' with " + options));
    }
    // show best runtime for each query
    out.println();
    out.println("---> worst times");
    for (AnnisRunner.Benchmark benchmark : benchmarks) {
        String options = benchmarkOptions(benchmark.queryData);
        out.println(benchmark.worstTimeInMilliseconds + " ms " + (benchmark.errors > 0 ? "(" + benchmark.errors + " errors)" : "") + " for '" + benchmark.functionCall + ("".equals(options) ? "'" : "' with " + options));
    }
    // show best runtime for each query
    out.println();
    out.println("---> best times");
    for (AnnisRunner.Benchmark benchmark : benchmarks) {
        String options = benchmarkOptions(benchmark.queryData);
        out.println(benchmark.bestTimeInMilliseconds + " ms " + (benchmark.errors > 0 ? "(" + benchmark.errors + " errors)" : "") + " for '" + benchmark.functionCall + ("".equals(options) ? "'" : "' with " + options));
    }
    out.println();
    // CSV output
    try (CSVWriter csv = new CSVWriter(new FileWriterWithEncoding(new File("annis_benchmark_result.csv"), "UTF-8"))) {
        String[] header = new String[] { "corpora", "query", "median", "diff-best", "diff-worst", "mean" };
        csv.writeNext(header);
        for (AnnisRunner.Benchmark benchmark : benchmarks) {
            double mean = (double) benchmark.sumTimeInMilliseconds / (double) benchmark.runs;
            long median = benchmark.getMedian();
            String[] line = new String[6];
            line[0] = StringUtils.join(benchmark.queryData.getCorpusList(), ",");
            line[1] = benchmark.functionCall;
            line[2] = "" + median;
            line[3] = "" + Math.abs(benchmark.bestTimeInMilliseconds - median);
            line[4] = "" + Math.abs(median - benchmark.worstTimeInMilliseconds);
            line[5] = "" + mean;
            csv.writeNext(line);
        }
    } catch (IOException ex) {
        log.error(null, ex);
    }
    // property output format for Jenkins Plot plugin
    try {
        File outputDir = new File("annis_benchmark_results");
        if (outputDir.isDirectory() || outputDir.mkdirs()) {
            int i = 1;
            for (AnnisRunner.Benchmark b : benchmarks) {
                Properties props = new Properties();
                props.put("YVALUE", "" + b.getMedian());
                try (FileWriterWithEncoding writer = new FileWriterWithEncoding(new File(outputDir, i + ".properties"), "UTF-8")) {
                    props.store(writer, "");
                }
                i++;
                // also write out a "time" and "count" file which can be used by the ANNIS4 prototype
                if (b.name != null) {
                    double mean = (double) b.sumTimeInMilliseconds / (double) b.runs;
                    Files.write("" + mean, new File(outputDir, b.name + ".time"), StandardCharsets.UTF_8);
                    if (b.count != null) {
                        Files.write("" + b.count, new File(outputDir, b.name + ".count"), StandardCharsets.UTF_8);
                    }
                }
            }
        }
    } catch (IOException ex) {
        log.error(null, ex);
    }
}
Also used : QueryData(annis.ql.parser.QueryData) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) ArrayList(java.util.ArrayList) CSVWriter(au.com.bytecode.opencsv.CSVWriter) IOException(java.io.IOException) Properties(java.util.Properties) Date(java.util.Date) FileWriterWithEncoding(org.apache.commons.io.output.FileWriterWithEncoding) File(java.io.File)

Aggregations

FileWriterWithEncoding (org.apache.commons.io.output.FileWriterWithEncoding)11 File (java.io.File)8 IOException (java.io.IOException)5 PrintWriter (java.io.PrintWriter)3 Properties (java.util.Properties)3 CSVWriter (au.com.bytecode.opencsv.CSVWriter)2 BufferedWriter (java.io.BufferedWriter)2 SortedKeyProperties (org.dkpro.tc.ml.report.util.SortedKeyProperties)2 QueryData (annis.ql.parser.QueryData)1 AnnotateQueryData (annis.sqlgen.extensions.AnnotateQueryData)1 LimitOffsetQueryData (annis.sqlgen.extensions.LimitOffsetQueryData)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 ZipFile (java.util.zip.ZipFile)1 SAXSVGDocumentFactory (org.apache.batik.dom.svg.SAXSVGDocumentFactory)1 TranscoderInput (org.apache.batik.transcoder.TranscoderInput)1 TranscoderOutput (org.apache.batik.transcoder.TranscoderOutput)1 SVGTranscoder (org.apache.batik.transcoder.svg2svg.SVGTranscoder)1