You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MemoryEater.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.memory;
  19. import java.io.BufferedReader;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.net.MalformedURLException;
  24. import javax.xml.transform.Result;
  25. import javax.xml.transform.Source;
  26. import javax.xml.transform.Templates;
  27. import javax.xml.transform.Transformer;
  28. import javax.xml.transform.TransformerConfigurationException;
  29. import javax.xml.transform.sax.SAXResult;
  30. import javax.xml.transform.sax.SAXTransformerFactory;
  31. import javax.xml.transform.stream.StreamSource;
  32. import org.apache.commons.io.IOUtils;
  33. import org.apache.commons.io.output.NullOutputStream;
  34. import org.apache.fop.apps.FOUserAgent;
  35. import org.apache.fop.apps.Fop;
  36. import org.apache.fop.apps.FopFactory;
  37. import org.apache.fop.apps.MimeConstants;
  38. /**
  39. * Debug tool to create and process large FO files by replicating them a specified number of times.
  40. */
  41. public final class MemoryEater {
  42. private SAXTransformerFactory tFactory
  43. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  44. private Templates replicatorTemplates;
  45. private Stats stats;
  46. private MemoryEater() throws TransformerConfigurationException, MalformedURLException {
  47. File xsltFile = new File("test/xsl/fo-replicator.xsl");
  48. Source xslt = new StreamSource(xsltFile);
  49. replicatorTemplates = tFactory.newTemplates(xslt);
  50. }
  51. private void eatMemory(File foFile, int runRepeats, int replicatorRepeats) throws Exception {
  52. stats = new Stats();
  53. FopFactory fopFactory = FopFactory.newInstance(foFile.getParentFile().toURI());
  54. for (int i = 0; i < runRepeats; i++) {
  55. eatMemory(i, foFile, replicatorRepeats, fopFactory);
  56. stats.progress(i, runRepeats);
  57. }
  58. stats.dumpFinalStats();
  59. System.out.println(stats.getGoogleChartURL());
  60. }
  61. private void eatMemory(int callIndex, File foFile, int replicatorRepeats, FopFactory fopFactory)
  62. throws Exception {
  63. Source src = new StreamSource(foFile);
  64. Transformer transformer = replicatorTemplates.newTransformer();
  65. transformer.setParameter("repeats", new Integer(replicatorRepeats));
  66. OutputStream out = new NullOutputStream(); //write to /dev/nul
  67. try {
  68. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  69. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out);
  70. Result res = new SAXResult(fop.getDefaultHandler());
  71. transformer.transform(src, res);
  72. stats.notifyPagesProduced(fop.getResults().getPageCount());
  73. if (callIndex == 0) {
  74. System.out.println(foFile.getName() + " generates "
  75. + fop.getResults().getPageCount() + " pages.");
  76. }
  77. stats.checkStats();
  78. } finally {
  79. IOUtils.closeQuietly(out);
  80. }
  81. }
  82. private static void prompt() throws IOException {
  83. BufferedReader in = new BufferedReader(new java.io.InputStreamReader(System.in));
  84. System.out.print("Press return to continue...");
  85. in.readLine();
  86. }
  87. /**
  88. * Main method.
  89. * @param args the command-line arguments
  90. */
  91. public static void main(String[] args) {
  92. boolean doPrompt = true; //true if you want a chance to start the monitoring console
  93. try {
  94. int replicatorRepeats = 2;
  95. int runRepeats = 1;
  96. if (args.length > 0) {
  97. replicatorRepeats = Integer.parseInt(args[0]);
  98. }
  99. if (args.length > 1) {
  100. runRepeats = Integer.parseInt(args[1]);
  101. }
  102. File testFile = new File("examples/fo/basic/readme.fo");
  103. System.out.println("MemoryEater! About to replicate the test file "
  104. + replicatorRepeats + " times and run it " + runRepeats + " times...");
  105. if (doPrompt) {
  106. prompt();
  107. }
  108. System.out.println("Processing...");
  109. long start = System.currentTimeMillis();
  110. MemoryEater app = new MemoryEater();
  111. app.eatMemory(testFile, runRepeats, replicatorRepeats);
  112. long duration = System.currentTimeMillis() - start;
  113. System.out.println("Success! Job took " + duration + " ms");
  114. if (doPrompt) {
  115. prompt();
  116. }
  117. } catch (Exception e) {
  118. e.printStackTrace();
  119. }
  120. }
  121. }