330 likes | 525 Views
Optimize Your Java Code By Tools. NuPoint Technology Inc. Manager of R&D Dept. Lin, Weicheng. Agenda. Why to Optimize Your Code? Introduction Optimize by OptimizeIt Conclusions. Why to Optimize Your Code?. Why coding?
E N D
Optimize Your Java Code By Tools NuPoint Technology Inc. Manager of R&D Dept. Lin, Weicheng
Agenda • Why to Optimize Your Code? • Introduction • Optimize by OptimizeIt • Conclusions
Why to Optimize Your Code? • Why coding? • Why do your program need more execution time or developing time than that of others’? • Do you agree that Programming is an ART?
Introduction • How to write a good program? • Suitable language • C++, Java, Perl, Scheme, … • Good algorithm • You need to learn Algorithm, OS, … • Good implementation • You need not only a good brain but a good tool • Bad implementation • Allocate too much memory • Instantiate too many objects • Bad programmatic logic • …
Introduction (Cont.) • There are several tools for optimization use: OptimizeIt, … • Functions of Optimization Tools • Profiling • Code Coverage Checking • Thread Debugging • Progress Tracking • Request Analyzing
Optimize by OptimizeIt • The goal of the optimization • A quick introduction to the application • Application architecture • Integrating JBuilder and OptimizeIt • Optimizing your first project
The Goal of the Optimization • Using JBuilder and OptimizeIt to solve performance problems with an email agent • A user with 4000 Messages per day • Before the performance tuning • Several minutes • After the performance tuning • < 10 seconds • Improvement • Less execution time • Less required memory
A Quick Introduction to the Application • Spam filtering It deletes spam from a user's mailbox using a variety of spam-filtering rules • Forwarding of desirable email messages to a pager A user can control which email messages should be forwarded from their inbox to their pager • Mail server-independence The Email Agent works with any email server that supports the POP-3 or IMAP standards • Platform-independence The Email Agent can run on any computer platform that supports Java, making it very flexible to install
Application Scenario • Scenario • Currently, only a user, with 4,000 messages • About 1,000 spam messages • Running every five minutes • In the need of handling more users
Basic Algorithm of the Email Agent • Application startup. • Initialize logger. • Read mail server connection properties. • Read properties for sending a summary report to the user. • Initialize internal lists from configuration files. • Create and populate the EmailAgent. • Connect to the mailbox. • Remove spam from the current folder. • Get the message list from the inbox. • Determine the UIDs of these messages. • Get a list of messages that have already been examined already. • While only looking at new messages: • One at a time, review each message to determine whether or not it is spam. • If the message matches an ``allow'' file rule, let it pass through. • Otherwise, test the From, Reply-To, and Subject fields, as well as message attachments. • If a message matches a notification criteria, send a message to my pager. • Close the mailbox and store. • Send me summary information about what actions were taken. Be care of your for-loop!
Basic Configuration • Configuring the application • allow.from • allow.subject • allow.contents • reject.from • reject.subject • reject.contents
Integrating JBuilder and OptimizeIt • Installation
Finding the Performance Problems Java Performance Tuning'' by Jack Shirazi • Measure the performance of the application, using tools, and/or by instrumenting the code. • Identify the location of any bottlenecks. • Think of a hypothesis for the cause of the bottleneck. • Consider any factors that might refute your hypothesis. • Create a test to isolate the factor identified by the hypothesis. • Test the hypothesis. • Alter the application to reduce the bottleneck. • Test that the alteration improves the performance. • Repeat from step 1.
Finding the Performance Problems (Cont.) • Simplified version of the process • Instrument your code and find the major bottlenecks, using the best tools available • Fix the bottlenecks that are causing the biggest problems, don't worry about the others. If you can't fix it, avoid it • If performance is now acceptable, celebrate. If not, repeat from step 1
Finding the Performance Problems (Cont.) • Fix the bottlenecks that are causing the biggest problems and don't worry about the others • Simplified analysis example • examineContents() - 70% • extractContents() - 20% • parseAttachment() - 10%
Drill Down Your Programmatic Flow Using Contents needs a lot of time • Put this line in the last • It will be run when needed
The Second Run Built in JDK
The Third Run: Problems with Regular Expressions aMatcher.find() wastes too much time indexOf is faster Change the order!
aMatcher.find() is still slow, we use aMatcher.matches() 9,956 ms
Getting Harder Now 9,548 ms
Getting Harder Now (Cont.) 9,353 ms
Conclusions • Reducing the application runtime from over 52 seconds down to about 9.4 seconds • You really need a optimization tool, don’t you? • Now, do you agree that Programming is an ART?