From: Jeremias Maerki Date: Thu, 21 Aug 2008 15:49:13 +0000 (+0000) Subject: Added some performance statistics to MemoryEater. X-Git-Tag: fop-1_0~446 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=761b318e762396909b3a8d21aacbb43ea31a1bc1;p=xmlgraphics-fop.git Added some performance statistics to MemoryEater. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@687786 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/test/java/org/apache/fop/memory/MemoryEater.java b/test/java/org/apache/fop/memory/MemoryEater.java index bdbd47897..2fdb32ac9 100644 --- a/test/java/org/apache/fop/memory/MemoryEater.java +++ b/test/java/org/apache/fop/memory/MemoryEater.java @@ -34,6 +34,7 @@ import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.stream.StreamSource; +import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.NullOutputStream; import org.apache.fop.apps.FOUserAgent; @@ -51,28 +52,46 @@ public class MemoryEater { private FopFactory fopFactory = FopFactory.newInstance(); private Templates replicatorTemplates; + private Stats stats = new Stats(); + public MemoryEater() throws TransformerConfigurationException, MalformedURLException { File xsltFile = new File("test/xsl/fo-replicator.xsl"); Source xslt = new StreamSource(xsltFile); replicatorTemplates = tFactory.newTemplates(xslt); } - private void eatMemory(File foFile, int replicatorRepeats) throws Exception { + private void eatMemory(File foFile, int runRepeats, int replicatorRepeats) throws Exception { + for (int i = 0; i < runRepeats; i++) { + eatMemory(i, foFile, replicatorRepeats); + stats.progress(i, runRepeats); + } + System.out.println(stats.getGoogleChartURL()); + } + + private void eatMemory(int callIndex, File foFile, int replicatorRepeats) throws Exception { Source src = new StreamSource(foFile); Transformer transformer = replicatorTemplates.newTransformer(); transformer.setParameter("repeats", new Integer(replicatorRepeats)); OutputStream out = new NullOutputStream(); //write to /dev/nul - FOUserAgent userAgent = fopFactory.newFOUserAgent(); - userAgent.setBaseURL(foFile.getParentFile().toURL().toExternalForm()); - Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out); - Result res = new SAXResult(fop.getDefaultHandler()); - - transformer.transform(src, res); + try { + FOUserAgent userAgent = fopFactory.newFOUserAgent(); + userAgent.setBaseURL(foFile.getParentFile().toURL().toExternalForm()); + Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out); + Result res = new SAXResult(fop.getDefaultHandler()); - System.out.println("Generated " + fop.getResults().getPageCount() + " pages."); + transformer.transform(src, res); + stats.notifyPagesProduced(fop.getResults().getPageCount()); + if (callIndex == 0) { + System.out.println(foFile.getName() + " generates " + + fop.getResults().getPageCount() + " pages."); + } + stats.checkStats(); + } finally { + IOUtils.closeQuietly(out); + } } private static void prompt() throws IOException { @@ -108,9 +127,7 @@ public class MemoryEater { long start = System.currentTimeMillis(); MemoryEater app = new MemoryEater(); - for (int i = 0; i < runRepeats; i++) { - app.eatMemory(testFile, replicatorRepeats); - } + app.eatMemory(testFile, runRepeats, replicatorRepeats); long duration = System.currentTimeMillis() - start; System.out.println("Success! Job took " + duration + " ms"); diff --git a/test/java/org/apache/fop/memory/Stats.java b/test/java/org/apache/fop/memory/Stats.java new file mode 100644 index 000000000..503fdad47 --- /dev/null +++ b/test/java/org/apache/fop/memory/Stats.java @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +/* $Id$ */ + +package org.apache.fop.memory; + +import java.util.Iterator; +import java.util.List; + +class Stats { + + private static final int INTERVAL = 2000; + + private long lastProgressDump = System.currentTimeMillis(); + private int pagesProduced; + + private int step; + private int stepCount; + + private List samples = new java.util.LinkedList(); + + public void checkStats() { + long now = System.currentTimeMillis(); + if (now > lastProgressDump + INTERVAL) { + dumpStats(); + reset(); + } + } + + public void notifyPagesProduced(int count) { + pagesProduced += count; + } + + public void reset() { + pagesProduced = 0; + lastProgressDump = System.currentTimeMillis(); + } + + public void dumpStats() { + long duration = System.currentTimeMillis() - lastProgressDump; + + if (stepCount != 0) { + int progress = 100 * step / stepCount; + System.out.println("Progress: " + progress + "%, " + (stepCount - step) + " left"); + } + + long ppm = 60000 * pagesProduced / duration; + System.out.println("Speed: " + ppm + "ppm"); + samples.add(new Sample((int)ppm)); + } + + public String getGoogleChartURL() { + StringBuffer sb = new StringBuffer("http://chart.apis.google.com/chart?"); + //http://chart.apis.google.com/chart?cht=ls&chd=t:60,40&chs=250x100&chl=Hello|World + sb.append("cht=ls"); + sb.append("&chd=t:"); + boolean first = true; + int maxY = 0; + Iterator iter = samples.iterator(); + while (iter.hasNext()) { + Sample sample = (Sample)iter.next(); + if (first) { + first = false; + } else { + sb.append(','); + } + sb.append(sample.ppm); + maxY = Math.max(maxY, sample.ppm); + } + int ceilY = ((maxY / 1000) + 1) * 1000; + sb.append("&chs=1000x300"); //image size + sb.append("&chds=0,").append(ceilY); //data scale + sb.append("&chg=0,20"); //scale steps + sb.append("&chxt=y"); + sb.append("&chxl=0:|0|" + ceilY); + return sb.toString(); + } + + private static class Sample { + + private int ppm; + + public Sample(int ppm) { + this.ppm = ppm; + } + } + + public void progress(int step, int stepCount) { + this.step = step; + this.stepCount = stepCount; + + } + +}