title: Saxon xslt transform to PDF slow on server but fast on local
body: <p>I am facing an performance issue using saxon and apache fo to transform xml to PDF. the pdf we use to test has 85 pages and is around 320k. it spends almost 2 mins on  <strong>transform</strong> method call and in local it only takes less than 5 seconds.
  we monitor cpu usage and GC during that method call and found that on server, cpu usage stays steady at 5% and we did not have any limitation on cpu from server side. GC happens every 1 to 2 seconds but they are all minor GC and each one takes only 10 to 50ms.  we also monitor io wait during test and it stayed very low.<br>
  libs we are using are：　saxon 9.1 and apache fop 2.1 (we tested with different saxon and apache versions but issue remains)
  xml and xsl files are too large so i am not able to post them. below is the sample code from transformation: </p>
  
  <pre><code> public static TransformerFactory transformerFactory;
   public static Transformer xlsProcessor;
  
   public static byte[] generatePDF(InputStream xmlData, String xslFile)
          throws TransformerException, IOException {
  
  
      byte[] fileArray = null;
      InputStream xsltfile = null;
      ByteArrayOutputStream outStream = null;
      try {
          xsltfile =
              XmlToPdfGenerator.class.getClassLoader()
                      .getResourceAsStream(xslFile);
  
          StreamSource source = new StreamSource(xmlData);
  
          StreamSource transformSource = new StreamSource(xsltfile);
          if (null== fopFactory){
              File xconf= new File(XmlToPdfGenerator.class.getClassLoader().getResource("a xconf file").getFile());
              fopFactory = FopFactory.newInstance(xconf);
          }
          FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
          outStream = new ByteArrayOutputStream();
          Transformer xslfoTransformer =
              getTransformer(transformSource);
          if (xslfoTransformer != null) {
          Fop fop;
          try {
              fop =
                  fopFactory.newFop(MimeConstants.MIME_PDF,
                          foUserAgent, outStream);
              Result res = new SAXResult(fop.getDefaultHandler());
              try {
  
              xslfoTransformer.transform(source, res);
  
              fileArray = outStream.toByteArray();
              } catch (TransformerException e) {
                  // some error handling logic omitted
              } catch (Exception e) {
                  // some error handling logic omitted
              }
          } catch (FOPException e) {
                  // some error handling logic omitted
          }
          }
      } catch (TransformerFactoryConfigurationError e) {
                  // some error handling logic omitted
      } catch (Exception e) {
                  // some error handling logic omitted
      } finally {
          if (null != xsltfile) {
          xsltfile.close();
          }
          if (null != outStream) {
          outStream.close();
          }
      }
      return fileArray;
      }
  
   private static Transformer getTransformer(StreamSource streamSource) {
     if (null==transformerFactory){
      transformerFactory =
              new net.sf.saxon.TransformerFactoryImpl();
      }
      try {
          if (xlsProcessor == null) {
              xlsProcessor  =
                  transformerFactory.newTransformer(streamSource);
          }
          return xlsProcessor  ;
      } catch (TransformerConfigurationException e) {
          // some error handling logic 
      }
      return null;
  
      }
  </code></pre>
  
  <p>I doubt there is any code issue caused this as it works normal on local.
  greatly appreciated if any thoughts on this!</p>