aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Generating swing components on fly slow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Generating swing components on fly slow" Watch "Generating swing components on fly slow" New topic
Author

Generating swing components on fly slow

Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Hello,
I have a Java swing application that communicates with a server through HTTP client/server requests.
the server sends me an html stream. i parse this html response and convert it to swing components. it is mapping html input fields to swing components.
The solution is working but the performance is not satisfactory:
-parsing the html form took 47 secs
-generating and rendering swing components took 134 secs
so as you can see this is poor performance.
I'm wondering what is a way to speed up this process? is it possible to find a clever way to do this performance tunning: parsing html form and converting to swing components?

thanks.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
1) Are you using a SwingWorker object so that the stream reading and HTML parsing are done in a background thread, and the Swing rendering done on the EDT?
2) How do you know that it's the Swing rendering that takes 134 seconds? Have you run this through a profiler?
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

pete stein wrote:1) Are you using a SwingWorker object so that the stream reading and HTML parsing are done in a background thread, and the Swing rendering done on the EDT?


hmm no i think i don't do the parsing in swingworker . because i need first to parse and collect html input fields ..put them in an array then walk away this array and create corresponding swing components and add them to JPanel. if i run the html parser in a thread and the swing render in other thread i'm not sure to have all html input fields to map them to swing to generate. do you have an Idea how i can achieve this with a SwingWoker? combining html form parsing and swing rendering in two threads cooperating toghether?

pete stein wrote:
2) How do you know that it's the Swing rendering that takes 134 seconds? Have you run this through a profiler?


yes , i simply used System.currentTimeMills() before and after the method for parsing/rendring swing
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10372
    
    8

Yahya Elyasse wrote: i parse this html response and convert it to swing components. it is mapping html input fields to swing components.


Is the HTML pure related content? Or does it also contain some more stuff not related to the UI? This could be one bottleneck.
When are you creating the Swing components? After parsing the HTML? Is it possible for you to create them first and then use from the cache?

Disclaimer: I have never tried this out myself. Please take it with a pinch of salt. I am just thinking out loud here.
Also google for XML-Swing frameworks. I read about one such framework recently, but cannot recall the name right now. Since its an existing framework, chances are they have already taken care of the optimization.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Maneesh Godbole wrote:

When are you creating the Swing components? After parsing the HTML? Is it possible for you to create them first and then use from the cache?

Yes after i finish parsing html i have an array which contains html input form fields i then go throught this array and generate corresponding swing components.
Maneesh Godbole wrote:
Disclaimer: I have never tried this out myself. Please take it with a pinch of salt. I am just thinking out loud here.
Also google for XML-Swing frameworks. I read about one such framework recently, but cannot recall the name right now. Since its an existing framework, chances are they have already taken care of the optimization.

Yes i'm aware of the xml swing converters like http://www.swixml.org/ . but the problem is that my java application is bounded to a HTTP server data which i have no control over and have to deal with HTML form sometimes and xml content in other times.
There is also the cobra and Lobo java libraries .but those will require me to change a lot of my code.and i don't think corba or lobo will increase much the performance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

No offence, but I think there must be a flaw in your program. 47 seconds to parse an HTML file? How large is that file, a few hundred megabytes? And over 2 minutes for generating and rendering components? My most heavy GUI ever created takes only a few seconds (5-10 max) to create the GUI, and that is one heavy GUI.

Can you show us your rendering and genering code?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Rob Prime wrote:No offence, but I think there must be a flaw in your program. 47 seconds to parse an HTML file? How large is that file, a few hundred megabytes? And over 2 minutes for generating and rendering components? My most heavy GUI ever created takes only a few seconds (5-10 max) to create the GUI, and that is one heavy GUI.

Can you show us your rendering and genering code?

hmm ,oops i think i didn't calculated the time right .actually i simply did something like :

but i see i did a mistake and had to divide by 1000 not 100.
so sorry for the mistake .the right timings are :
Form Parsing took 4 secs
Form building took 13 secs

so it takes about 17 secs to render the swing form. is this a reasonable timing? my client is complaining that the form rendering is a bit slow (here 17 secs)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41839
    
  63
4 seconds still sounds like a long time to parse a file. Which API are you using to do this? Does it include the time to download it? How big is the file? Is the code building some intermediate data structures in that time?

13 seconds, too, is on the high side for building a GUI. How many elements does the GUI have? Is there additional processing being done in that time?


Ping & DNS - my free Android networking tools app
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

ok here you are the two essential classes : class Forms.java for parsing html and class HTMLForm.java for creating swing GUI.
Hope you can give me Ideas how to improve rendering speed of the swing form converted from html form.
thanks


Forms.java
-------------

HTMLForm.java
------------------


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41839
    
  63
Umm ... 750 lines of code? I don't think too many people here will be willing to go through that (not without taking a cut of the money you make for working on this anyway )
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Ulf Dittmer wrote:Umm ... 750 lines of code? I don't think too many people here will be willing to go through that (not without taking a cut of the money you make for working on this anyway )

some people asked me to show them the code..so i paste this long code. if you find it inconvienient posting long code i can remove it no problem.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41839
    
  63
It's fine, I just don't think people will take the time to try to understand it.

If someone asks for code, they don't mean 750 lines of it ... maybe 50.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Read through it all

I did notice you hardly ever cache results. A few examples:

getFormMessage() retrieves the response body (from the URL directly, or cached, can't tell), and parses the entire body each time its called. If getFormMessage() returns a non-null value, you do this twice. For every call to getFormMessage(), that could be an entire parse that's completely unnecessary if you cache getFormMessage() before the firs null-check.

Then, in getTitle(), you get the response body and parse yet again. Also:

Do you expect multiple titles, and then want to return the last one? If not, return title as soon as you set it. Because this way, you are going through all elements, even if you've found the title within the first few iterations.


You call logInputInfo several times. Each time, regardless of the logger level, you possibly call input.getId() and getLabelInput().getLabels().get(id) twice. I have no idea how expensive those two methods are, but it really is unnecessary. You should change it to something like this:

Those are just a few examples I noticed, but there are more.

Also, you should really, really consider caching more objects. I already mentioned getLabelInput() and/or getLabelInput().getLabels(), but also api.getResponse(), api.getResponse().getBody() (after you've tidied it), the parsed document, etc.

If you don't want to initialize these each time, you can lazy initialize them. For instance:
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Rob,
almost all methods are called once . I build form once and don't call most of methods twice. so i didn't considered caching results
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

You should consider caching at least the SimpleDocument; tidying and parsing is probably the most expensive task, and you're calling it at least twice - once in getFormMessage() and once in getTitle().
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Rob Prime wrote:You should consider caching at least the SimpleDocument; tidying and parsing is probably the most expensive task, and you're calling it at least twice - once in getFormMessage() and once in getTitle().

hmm I think you are right. i noticed that the tidy(html) method is called many times and that's a quit expensive method because it processes the whole html stream.
So yes thanks for mentioning that.I'll try changing to your proposed solution.
BTW: do you think that parsing html and caching html Inputs should be done separately first ,then after that i can go through html Inputs array and build & render corresponding swing components? won't this hurt much performance? I can't see an elegant way to do parsing and rendering simultaneously. do you think i should plug rendering code inside Forms class that does the html parsing? that will produce inelegant code i believe.any Ideas?

thanks.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Thanks Rob,
I did the caching changes and now the rendering speed is much better (no more than 4 secs).
However i feel HTML parsing still takes longer time (i'm said it took 5 secs) but the progress let me feel it takes longer. could it because of the logging logic? because for debugging purposes i'm logging all html stream ect..
in class Forms.java above can you detect some inefficient parsing code i might have done?

thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Nope. The only thing you could remove is all the "catch all exceptions and wrap them in an IllegalStateException" - in the end, all the getFormXXX methods call getForm(SimpleDocument, String, String), and that already catches all exceptions and wraps them in an IllegalStateException. So what will be happening is the following:
SomeException is thrown
It is wrapped in an IllegalStateException
That is wrapped in an IllegalStateException
That is wrapped in an IllegalStateException
...

So except for that one method, you can remove all the try-catch blocks. Not that that will cause much improvement in speed, but it will help you in debugging should an exception occur.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Thank you Rob. you helped me much optimizing my code performance.
I still need to investigate the html parsing code and make it work faster.
any Ideas guys how to speed html form parsing ? are there any Java libraries out there for parsing html forms quickly and returning html Form input fields?

thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41839
    
  63
How is HTML parsing done now - are you using a library? I might convert it to XML (using something like Neko or TagSoup), and then run a SAX parser over it, or use a library like HtmlUnit.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Ulf Dittmer wrote:How is HTML parsing done now - are you using a library? I might convert it to XML (using something like Neko or TagSoup), and then run a SAX parser over it, or use a library like HtmlUnit.

I'm only using the java DOM parser . I treat the html stream like xml and parse it using a DOM parser.
I'm not sure if there exits some libs for faster html parsing and that collect html form input fields. you mentioned some libraries i will definitely take a look at them.
thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generating swing components on fly slow