xmlgraphics-fop-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeremias Maerki <...@jeremias-maerki.ch>
Subject Re: problems with times
Date Wed, 21 Sep 2005 10:12:44 GMT

On 21.09.2005 12:03:56 ALi wrote:
> with the new code .... this
> 
>                 StreamSource xsltSource = new StreamSource(xsltfile);
> 		TransformerFactory transFact = TransformerFactory.newInstance();
> 		try {
> 		Templates cachedXSLT = transFact.newTemplates(xsltSource);
> 		Transformer trans = cachedXSLT.newTransformer();

ALi, that's not a cache! That's exactly the same as writing:

Transformer trans = transFact.newTransformer(xsltSource);

Please read the article I gave you the link for.

And to be clear: You won't get rid of the longer processing time for the
first invocation. There is class loading and just-in-time compiling.
There is NO way around this!!! The Templates cache will only let you
avoid reloading the same stylesheet each time you need it.

Manuel was probably right in admonishing me about the comment about
Templates caching. Sigh.


> 		long tiempo1 = System.currentTimeMillis();
> 		long tiempo2=0;
>                 // Construct fop with desired output format
>                 Driver driver = new Driver();
>                 driver.setRenderer(Driver.RENDER_PDF);
>                 // Setup output
>                 try {
>                 	OutputStream out = new java.io.FileOutputStream(pdffile);
>                 	out = new java.io.BufferedOutputStream(out);
>                 	
>                 	try {
>                 		
>                 		driver.setOutputStream(out);
>                 		
>                 		
>                 		// Set the value of a <param> in the stylesheet
>                 		trans.setParameter("versionParam", "2.0");
>                 		
>                 	Source src = new StreamSource(new StringReader(xml));
>                		Result res = new SAXResult(driver.getContentHandler());
>                 		
>                 		// Start XSLT transformation and FOP processing
>                 		trans.transform(src, res);
>                 		tiempo2 = System.currentTimeMillis();
>                 		depura("Success!");
>                 	} catch (TransformerException e) {
>                 		logger.warn("Error generando pdf :"+e.getMessage());
>                 		
>                 	} catch (SAXException e) {
> 						// TODO Auto-generated catch block
> 						e.printStackTrace();
> 					} finally {
>                 		out.close();
>                 	}
> 					  System.out.println(tiempo1);
> 		                System.out.println(tiempo2);
> 		                System.out.println("Total:" + (tiempo2-tiempo1));
> 		                logger.info("Tiempo 1:" + tiempo1);
> 		                logger.info("Tiempo 2:" + tiempo2);
> 		                logger.info("Total :" + (tiempo2-tiempo1));
> main () {
>         generadorConCache gen = new generadorConCache();
>         System.out.println(gen.transformar(msgEtiquetas,xsltEtiquetas));
>         System.out.println(gen.transformar(msgText,xslt));
>         System.out.println(gen.transformar(msgEtiquetas,xsltEtiquetas));
>         System.out.println(gen.transformar(msgText,xslt));
>         System.out.println(gen.transformar(msgEtiquetas,xsltEtiquetas));
>         System.out.println(gen.transformar(msgText,xslt));
> }
> 
> 
> And these are the times now (with these code)
> 
> Total:6875
> Total:2262
> Total:2463
> Total:1090
> Total:2085
> Total:927
> 
> 
> and with the last code always reading
> 
> Total:8675
> Total:2221
> Total:2638
> Total:1234
> Total:2453
> Total:1079
> 
> why the first time is so big?
> 
> 
> 
> 
> On 21.09.2005 11:21:50 ALi wrote:
> > this is my code ...
> > xml is in resultstr (<?xml ...........>)
> > xslt is the name of the file (/name.file.xsl)
> > long tiempo1 = System.currentTimeMillis();
> > long tiempo2 = 0;
> > // Construct fop with desired output format
> > Driver driver = new Driver();
> > driver.setRenderer(Driver.RENDER_PDF);
> > // Setup output
> > try {
> > java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
> > //out = new java.io.BufferedOutputStream(out);
> > try {
> > driver.setOutputStream(out);
> > // Setup XSLT
> > TransformerFactory factory = TransformerFactory.newInstance();
> > Transformer transformer = factory
> > .newTransformer(new StreamSource(xsltfile));
> 
> That's exactly it. You reload the stylesheet every time. If you create a
> stylesheet cache, you can simply reuse Templates instances.
> 
> //Create the Templates object and store it in the cache
> Templates templates = factory.newTemplates(new StreamSource(xsltfile));
> 
> //To create the Transformer with the Templates objects from the cache
> Transformer transformer = templates.newTransformer();
> 
> > // Set the value of a <param> in the stylesheet
> > transformer.setParameter("versionParam", "2.0");
> > // Setup input for XSLT transformation
> > Source src = new StreamSource(new StringReader(resultStr));
> > // Resulting SAX events (the generated FO) must be piped through to FOP
> > Result res = new SAXResult(driver.getContentHandler());
> > // Start XSLT transformation and FOP processing
> > transformer.transform(src, res);
> > tiempo2 = System.currentTimeMillis();
> > response.setContentType("application/pdf");
> > byte[] cont = out.toByteArray();
> > response.setContentLength(cont.length);
> > response.getOutputStream().write(cont);
> > response.getOutputStream().flush();
> > } catch (TransformerException e) {
> > logger.warn("Error generando pdf :" + e.getMessage());
> > } finally {
> > out.close();
> > }
> > } catch (FileNotFoundException e) {
> > e.printStackTrace();
> > } catch (IOException e) {
> > logger.debug("Error cerrando el fichero " + e.getMessage());
> > }
> > logger.info("Tiempo 1:" + tiempo1);
> > logger.info("Tiempo 2:" + tiempo2);
> > logger.info("Total :" + (tiempo2 - tiempo1));
> >
> 
> 
> 
> On 9/21/05, ALi <osatien@gmail.com> wrote:
> >
> > this is my code ...
> > xml is in resultstr (<?xml ...........>)
> > xslt is the name of the file (/name.file.xsl)
> > long tiempo1 = System.currentTimeMillis();
> > long tiempo2 = 0;
> > // Construct fop with desired output format
> > Driver driver = new Driver();
> > driver.setRenderer(Driver.RENDER_PDF);
> > // Setup output
> > try {
> > java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
> > //out = new java.io.BufferedOutputStream(out);
> > try {
> > driver.setOutputStream(out);
> > // Setup XSLT
> > TransformerFactory factory = TransformerFactory.newInstance();
> > Transformer transformer = factory
> > .newTransformer(new StreamSource(xsltfile));
> > // Set the value of a <param> in the stylesheet
> > transformer.setParameter("versionParam", "2.0");
> > // Setup input for XSLT transformation
> > Source src = new StreamSource(new StringReader(resultStr));
> > // Resulting SAX events (the generated FO) must be piped through to FOP
> > Result res = new SAXResult(driver.getContentHandler());
> > // Start XSLT transformation and FOP processing
> > transformer.transform(src, res);
> > tiempo2 = System.currentTimeMillis();
> > response.setContentType("application/pdf");
> > byte[] cont = out.toByteArray();
> > response.setContentLength(cont.length);
> > response.getOutputStream().write(cont);
> > response.getOutputStream().flush();
> > } catch (TransformerException e) {
> > logger.warn("Error generando pdf :" + e.getMessage());
> > } finally {
> > out.close();
> > }
> > } catch (FileNotFoundException e) {
> > e.printStackTrace();
> > } catch (IOException e) {
> > logger.debug("Error cerrando el fichero " + e.getMessage());
> > }
> > logger.info("Tiempo 1:" + tiempo1);
> > logger.info("Tiempo 2:" + tiempo2);
> > logger.info("Total :" + (tiempo2 - tiempo1));
> >
> > On 9/21/05, Jeremias Maerki <dev@jeremias-maerki.ch > wrote:
> > >
> > > I assume the operating system has very little influence on the time
> > > here.
> > > The fact that ALi has a big difference in the first couple of
> > > invocations is a clear indicator that he experiences the effect of class
> > > loading and just-in-time compiling by the JavaVM. And yes, ALi, the
> > > stylesheets have to be "compiled" (or at least parsed) but I guess from
> > > your code that this happens each time in your case. Unfortunately, we
> > > don't see the code of your makePDF() method.
> > >
> > > You can reuse the "pre-compiled" XSLT stylesheet if you use the JAXP
> > > Templates object. Please note that it's not FOP that compiles the XSLT
> > > stylesheet. It's probably you in makePDF() by using JAXP. FOP only
> > > processes the output of the XSL transformation. There are lots of
> > > articles on the web (like the following) that demonstrate how to cache
> > > Templates instances:
> > > http://www.javaworld.com/javaworld/jw-05-2003/jw-0502-xsl.html
> > >
> > > On 21.09.2005 10:22:22 Manuel Mall wrote:
> > > > Are you sure its not your operating system which causes the time
> > > > differences. Even OSes tend to warm up, ie. the first time around
> > > > existing stuff gets swapped out and the new code loaded. Next time
> > > > around this is not required any more.
> > > >
> > > > Manuel
> > > >
> > > > On Wed, 21 Sep 2005 04:07 pm, ALi wrote:
> > > > > Hello i have problems with times in generating pdf
> > > > > I want to know why the times are so different between one invocation
> > > > > and two
> > > > >
> > > > >
> > > > > for example i have the class makePDF
> > > > >
> > > > > main () {
> > > > > time1
> > > > > makePDF(xml,xsl);
> > > > > time2
> > > > > println(time2-time1)
> > > > > }
> > > > > the times are over 8 seconds
> > > > > more or less (with my xsl)
> > > > >
> > > > > if i have
> > > > > main () {
> > > > > for (1-10) {
> > > > > time1
> > > > > makePDF(xml,xsl);
> > > > > time2
> > > > > println(time2-time1)
> > > > > }
> > > > > }
> > > > >
> > > > > the first time is 8 seconds, but the seconds ones are less but much
> > > > > less the second is 3 seconds the third 1500 miliseconds and after
> > > > > over 300 miliseconds .....
> > > > > why is these? is it because the fop need to compile the xsl? Are
any
> > > > > form to force to cache the compilation of xsl? (in order to always
> > > > > take the same time.
> > > > >
> > > > > We have different xsl and one class that makes alls
> > > > >
> > > > >
> > > > > thx for your help
> > >
> > >
> > >
> > > Jeremias Maerki
> > >
> > > many thx for the url jeremias
> >



Jeremias Maerki


---------------------------------------------------------------------
To unsubscribe, e-mail: fop-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-users-help@xmlgraphics.apache.org


Mime
View raw message