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.

FOPTestbed.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.threading;
  19. import java.io.File;
  20. import java.io.OutputStream;
  21. import java.text.DecimalFormat;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.Templates;
  26. import javax.xml.transform.TransformerConfigurationException;
  27. import javax.xml.transform.TransformerFactory;
  28. import javax.xml.transform.stream.StreamSource;
  29. import org.apache.commons.io.IOUtils;
  30. import org.apache.commons.io.output.CountingOutputStream;
  31. import org.apache.commons.io.output.NullOutputStream;
  32. import org.apache.commons.logging.Log;
  33. import org.apache.commons.logging.LogFactory;
  34. import org.apache.fop.activity.ContainerUtil;
  35. import org.apache.fop.activity.Initializable;
  36. import org.apache.fop.configuration.Configurable;
  37. import org.apache.fop.configuration.Configuration;
  38. import org.apache.fop.configuration.ConfigurationException;
  39. /**
  40. * Testbed for multi-threading tests. The class can run a configurable set of task a number of
  41. * times in a configurable number of threads to easily reproduce multi-threading issues.
  42. */
  43. public class FOPTestbed
  44. implements Configurable, Initializable {
  45. private static final Log LOG = LogFactory.getLog(FOPTestbed.class);
  46. private int repeat;
  47. private List taskList = new java.util.ArrayList();
  48. private int threads;
  49. private File outputDir;
  50. private Configuration fopCfg;
  51. private Processor foprocessor;
  52. private boolean writeToDevNull;
  53. private int counter;
  54. private List results = Collections.synchronizedList(new java.util.LinkedList());
  55. /** {@inheritDoc} */
  56. public void configure(Configuration configuration) throws ConfigurationException {
  57. this.threads = configuration.getChild("threads").getValueAsInteger(10);
  58. this.outputDir = new File(configuration.getChild("output-dir").getValue());
  59. this.writeToDevNull = configuration.getChild("devnull").getValueAsBoolean(false);
  60. Configuration tasks = configuration.getChild("tasks");
  61. this.repeat = tasks.getAttributeAsInteger("repeat", 1);
  62. Configuration[] entries = tasks.getChildren("task");
  63. for (Configuration entry : entries) {
  64. this.taskList.add(new TaskDef(entry));
  65. }
  66. this.fopCfg = configuration.getChild("processor");
  67. }
  68. /** {@inheritDoc} */
  69. public void initialize() throws Exception {
  70. this.foprocessor = createFOProcessor();
  71. }
  72. /**
  73. * Starts the stress test.
  74. */
  75. public void doStressTest() {
  76. LOG.info("Starting stress test...");
  77. long start = System.currentTimeMillis();
  78. this.counter = 0;
  79. //Initialize threads
  80. ThreadGroup workerGroup = new ThreadGroup("FOP workers");
  81. List threadList = new java.util.LinkedList();
  82. for (int ti = 0; ti < this.threads; ti++) {
  83. TaskRunner runner = new TaskRunner();
  84. // ContainerUtil.enableLogging(runner, logger);
  85. Thread thread = new Thread(workerGroup, runner, "Worker- " + ti);
  86. threadList.add(thread);
  87. }
  88. //Start threads
  89. for (Object aThreadList : threadList) {
  90. ((Thread) aThreadList).start();
  91. }
  92. //Wait for threads to end
  93. while (threadList.size() > 0) {
  94. Thread t = (Thread)threadList.get(0);
  95. if (!t.isAlive()) {
  96. threadList.remove(0);
  97. continue;
  98. }
  99. try {
  100. Thread.sleep(100);
  101. } catch (InterruptedException ie) {
  102. //ignore
  103. }
  104. }
  105. long duration = System.currentTimeMillis() - start;
  106. report(duration);
  107. }
  108. private void report(long duration) {
  109. int count = this.results.size();
  110. int failures = 0;
  111. long bytesWritten = 0;
  112. System.out.println("Report on " + count + " tasks:");
  113. for (Object result : this.results) {
  114. Result res = (Result) result;
  115. if (res.failure != null) {
  116. System.out.println("FAIL: " + (res.end - res.start) + " " + res.task);
  117. System.out.println(" -> " + res.failure.getMessage());
  118. failures++;
  119. } else {
  120. System.out.println("good: " + (res.end - res.start) + " " + res.filesize
  121. + " " + res.task);
  122. bytesWritten += res.filesize;
  123. }
  124. }
  125. System.out.println("Stress test duration: " + duration + "ms");
  126. if (failures > 0) {
  127. System.out.println(failures + " failures of " + count + " documents!!!");
  128. } else {
  129. float mb = 1024f * 1024f;
  130. System.out.println("Bytes written: " + (bytesWritten / mb) + " MB, "
  131. + (bytesWritten * 1000 / duration) + " bytes / sec");
  132. System.out.println("NO failures with " + count + " documents.");
  133. }
  134. }
  135. private class TaskRunner implements Runnable {
  136. public void run() {
  137. try {
  138. for (int r = 0; r < repeat; r++) {
  139. for (Object aTaskList : taskList) {
  140. TaskDef def = (TaskDef) aTaskList;
  141. final Task task = new Task(def, counter++, foprocessor);
  142. // ContainerUtil.enableLogging(task, logger);
  143. task.execute();
  144. }
  145. }
  146. } catch (Exception e) {
  147. LOG.error("Thread ended with an exception", e);
  148. }
  149. }
  150. }
  151. /**
  152. * Creates a new FOProcessor.
  153. * @return the newly created instance
  154. */
  155. public Processor createFOProcessor() {
  156. try {
  157. Class clazz = Class.forName(this.fopCfg.getAttribute("class",
  158. "org.apache.fop.threading.FOProcessorImpl"));
  159. Processor fop = (Processor)clazz.getDeclaredConstructor().newInstance();
  160. // ContainerUtil.enableLogging(fop, logger);
  161. ContainerUtil.configure(fop, this.fopCfg);
  162. ContainerUtil.initialize(fop);
  163. return fop;
  164. } catch (Exception e) {
  165. throw new RuntimeException("Error creating FO Processor", e);
  166. }
  167. }
  168. private class TaskDef {
  169. private String fo;
  170. private String xml;
  171. private String xslt;
  172. private Templates templates;
  173. public TaskDef(String fo) {
  174. this.fo = fo;
  175. }
  176. public TaskDef(Configuration cfg) throws ConfigurationException {
  177. this.fo = cfg.getAttribute("fo", null);
  178. if (this.fo == null) {
  179. this.xml = cfg.getAttribute("xml");
  180. this.xslt = cfg.getAttribute("xslt", null);
  181. if (this.xslt != null) {
  182. TransformerFactory factory = TransformerFactory.newInstance();
  183. Source xsltSource = new StreamSource(new File(xslt));
  184. try {
  185. this.templates = factory.newTemplates(xsltSource);
  186. } catch (TransformerConfigurationException tce) {
  187. throw new ConfigurationException("Invalid XSLT", tce);
  188. }
  189. }
  190. }
  191. }
  192. public String getFO() {
  193. return this.fo;
  194. }
  195. public String getXML() {
  196. return this.xml;
  197. }
  198. public Templates getTemplates() {
  199. return this.templates;
  200. }
  201. public String toString() {
  202. StringBuffer sb = new StringBuffer();
  203. if (this.fo != null) {
  204. sb.append("fo=");
  205. sb.append(this.fo);
  206. } else {
  207. sb.append("xml=");
  208. sb.append(this.xml);
  209. sb.append(" xslt=");
  210. sb.append(this.xslt);
  211. }
  212. return sb.toString();
  213. }
  214. }
  215. private class Task {
  216. private TaskDef def;
  217. private int num;
  218. private Processor fop;
  219. public Task(TaskDef def, int num, Processor fop) {
  220. this.def = def;
  221. this.num = num;
  222. this.fop = fop;
  223. }
  224. public void execute() throws Exception {
  225. LOG.info("Processing: " + def);
  226. long start = System.currentTimeMillis();
  227. try {
  228. DecimalFormat df = new DecimalFormat("00000");
  229. File outfile = new File(outputDir, df.format(num) + fop.getTargetFileExtension());
  230. OutputStream out;
  231. if (writeToDevNull) {
  232. out = new NullOutputStream();
  233. } else {
  234. out = new java.io.FileOutputStream(outfile);
  235. out = new java.io.BufferedOutputStream(out);
  236. }
  237. CountingOutputStream cout = new CountingOutputStream(out);
  238. try {
  239. Source src;
  240. Templates templates;
  241. if (def.getFO() != null) {
  242. src = new StreamSource(new File(def.getFO()));
  243. templates = null;
  244. } else {
  245. src = new StreamSource(new File(def.getXML()));
  246. templates = def.getTemplates();
  247. }
  248. fop.process(src, templates, cout);
  249. } finally {
  250. IOUtils.closeQuietly(cout);
  251. }
  252. results.add(new Result(def, start, System.currentTimeMillis(),
  253. cout.getByteCount()));
  254. } catch (Exception e) {
  255. results.add(new Result(def, start, System.currentTimeMillis(), e));
  256. throw e;
  257. }
  258. }
  259. }
  260. private static class Result {
  261. private TaskDef task;
  262. private long start;
  263. private long end;
  264. private long filesize;
  265. private Throwable failure;
  266. public Result(TaskDef task, long start, long end, long filesize) {
  267. this(task, start, end, null);
  268. this.filesize = filesize;
  269. }
  270. public Result(TaskDef task, long start, long end, Throwable failure) {
  271. this.task = task;
  272. this.start = start;
  273. this.end = end;
  274. this.failure = failure;
  275. }
  276. }
  277. }