選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

FOPTestbed.java 11KB

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