Presentation is loading. Please wait.

Presentation is loading. Please wait.

Optimizing for the Java™ Servlet API & Database Access SSE USTC Qing Ding.

Similar presentations


Presentation on theme: "Optimizing for the Java™ Servlet API & Database Access SSE USTC Qing Ding."— Presentation transcript:

1 Optimizing for the Java™ Servlet API & Database Access SSE USTC Qing Ding

2 Agenda Servlet Synchronization &Thread Model Servlet Performance Measurement Optimization Areas – Synchronization – Objects creation – Page caching technique – Avoid converstion Servlet optimization techniques Optimizing Data Access

3 A Review of Java Servlet Architecture Servlet: An object that implements javax.servlet.Servlet Servlets get executed in a servlet container init() and destroy() get called once The container receives requests and invokes the servlet’s service() method New thread created for each request HttpServlet provides useful starting point

4 Servlet Programming Is Different Servlets are long-lived – Memory management matters! – Out-of-memory errors Most created objects are short-lived – Heap thrashing can kill performance Very high thread load – Under-synchronize: erratic bugs – Over-synchronize: performance suffers

5 Servlet Synchronization &Thread Model

6 Concurrency Issues on a Servlet The service() method of a servlet instance can be invoked by multiple clients (multiple threads) Servlet programmer has to deal with concurrency issue – shared data needs to be protected – this is called “servlet synchronization” 2 options for servlet synchronization – use of synchronized block – use of SingleThreadModel

7 Many Threads, One Servlet Instance

8 Use of synchronized block Synchronized blocks are used to guarantee only one thread at a time can execute within a section of code synchronized(this) { myNumber = counter + 1; counter = myNumber; }... synchronized(this) { counter = counter - 1 ; }

9 SingleThreadModel Interface Servlets can also implement javax.servlet.SingleThreadModel The server will manage a pool of servlet instances Guaranteed there will only be one thread per instance This could be overkill in many instances Public class SingleThreadModelServlet extends HttpServlet implements SingleThreadModel {... }

10 SingleThreadModel

11 Best Practice Recommendation Do use synchronized block whenever possible – SingleThreadModel is expensive (performance wise)

12 Invoker Servlet

13 What is Invoke Servlet? Executes anonymous servlet classes that have not been defined in a web.xml file Mostly used for debugging and testing purpose

14 How to Use Invoke Servlet?

15

16 Servlet Performance Measurement

17 Basic Measurements: Pages/Second Test with an external program Apache ab is a good start Test with simultaneous requests Browse website during test to look

18 Basic Measurements: Seconds/Page Test from within your program Check System.currentTimeMillis() at start/end of critical sections Report to System.err, but not too frequently Good: Track min/avg/max Better: Keep counts for time ranges

19 Other Useful Measurements Database requests per page System load average Heap usage: Runtime.totalMemory()- Runtime.freeMemory() – Too high: Risk out-of-memory problems – Too low: Not caching enough

20 Synchronization

21 Symptom: Wrong output under load Diagnosis: Unsynchronized changes to shared data Solution: Don’t use shared data – Be wary of instance data – Use the stack: Arguments and locals are safer Solution: Synchronize access to shared data

22 Over-Synchronization Symptom: Many threads waiting on a single monitor Diagnosis: Excessive synchronization Solution: Reduce synchronization – Use local variables and arguments

23 Object Creation

24 The “Textbook” Approach String buildForm(args) { StringBuffer b = new StringBuffer(); b.append(....); return b.toString(); } Two heap allocations One heap allocations

25 Minimize Object Creation void buildForm(StringBuffer b, args) { b.append(....); } No heap allocations unless StringBuffer needs to resize its internal storage

26 Reducing Heap Activity Matters Reduced garbage collection Heap allocation is synchronized Reducing heap activity leads to big performance gains

27 Page Caching Technique

28 Cache Page Components Page CachePiece Cache Data Cache

29 Piece Cache HTML/XML output is built up from pieces Don’t rebuild pieces unnecessarily Store pieces as bytes whenever possible

30 Cache Database Results

31 Basic Fast Servlet class FastServlet extends HttpServlet { void doGet(request, response) { String uri=request.getRequestURI(); Servable servable; if(!cache.contains(uri)) { servable=genericResponse(uri); cache.put(uri, servable); } servable=cache.get(uri); OutputStream out; out=response.getOutputStream(); servable.emit(out); }

32 Page Cache class FastServlet extends HttpServlet { void doGet(request, response) { String uri=request.getRequestURI(); Servable servable; if(!cache.contains(uri)) { servable=genericResponse(uri); cache.put(uri, servable); } servable=cache.get(uri); OutputStream out; out=response.getOutputStream(); servable.emit(socket); } Cache implements HashMap

33 What Goes Into the Page Cache?

34 class FastServlet extends HttpServlet { void doGet(request, response) { String uri=request.getRequestURI(); Servable servable; if(!cache.contains(uri)) { servable=genericResponse(uri); cache.put(uri, servable); }

35 Servable Interface Servable is something that can be sent as an HTTP response interface Servable { void emit(OutputStream); String getContentType(); }

36 Text Servable = “Page” Page: Servable that holds a text response Create page at top level, pass down into methods that add text Page is mostly a StringBuffer Use placeholder objects for dynamic elements

37 Page Class class Page implements Servable { void append(String); void append(int); void append(double); void append(Object o); void addDynamic(Servable) { //Store placeholder at current pos } void emit(OutputStream) { //Write bytes to socket, //Invoke emit() for each placeholder }

38 Placeholders for Performance Want to avoid rebuilding pages Solution: cache full pages But, what about dynamic elements? Placeholder objects get invoked each time a page is served Result: Most page-building is avoided, but dynamic elements are always fresh

39 Avoid Conversions

40 Optimization: Avoid Conversions Converting chars to bytes is very slow Always use OutputStream, never PrintWriter Store bytes, not characters

41 Basic Fast Servlet class FastServlet extends HttpServlet { void doGet(request, response) { String uri=request.getRequestURI(); Servable servable; if(!cache.contains(uri)) { servable=genericResponse(uri); cache.put(uri, servable); } servable=cache.get(uri); OutputStream out; out=response.getOutputStream(); servable.emit(out); }

42 Performance Improvement Tips (Quoted from precisejava.com)

43 Generic Coding Techniques in service() Method Use StringBuffer over String and + Use print() over println() Use ServletOutputStream over PrintWriter Initialize PrintWriter with proper buffer size Flush data partly for large amount of data Minimize the synchronization block Set the content length

44 One-time Resource Allocation and Deallocation Use init() method for one time (per servlet instance) resource allocation – Database connection – Socket Use destroy() method for resource

45 Caching Techniques Use init() to cache static data Utilizing browser caching Caching dynamic data at the server Utilizing HTTPSession and ServletContext objects as cache

46 Use init() method as Cache init() method is called only once service() method gets many times Create and cache static data in init() method once Dynamic data get generated in service() Useful when large amount of static data need to be returned repeatedly

47 Example: Bad Practice public void service(HttpServletRequest req, HttpServletRespnse res) throws ServletException, IOException { res.setContentType(“text/html”); Printwriter out = req.getWriter(); out.print(" ”); out.print(" Hello world ”); out.print(“ ”); // send the dynamic data here out.print(“ ”); out.print(" ”);

48 Example: Good Practice public class servlet extends HttpServlet { byte[] header;byte[] navbar;byte[] footer;byte[] otherStaticData; public void init(ServletConfig config) throws ServletException{ //create all the static data here StringBuffer sb = new StringBuffer(); // better to initialize the StringBuffer with to improve performance sb.append(" ”); sb.append(" Hello world ”); sb.append(“ ”); header = sb.toString().getBytes(); // do same for navbar if its data is static // do same for footer if its data is static } public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); ServletOutputStream out = res.getOutputStream(); out.write(header); out.write(navbar); // write dynamic data here out.write(footer); }

49 Utilize Browser Caching Browser has the page in its cache Servlet implements getLastModified() method (of HTTPServlet class) – Browser send “if modified since” header – Servlet engine calls getLastModified() – If not modified, servlet engine responds with “Not Modified” (304 status code) Avoids calling service() if the output content has not changed Useful when client hits reload on his browser

50 Caching Data at the Server Cache servlet's output at the server – Can send it to different clients Servlet implements getLastModified() method (of HTTPServlet class) – Servlet engine calls getLastModified()

51 Utilizing HTTPSession and ServletConext Objects as Cache HTTPSession – Shared by multiple requests per session ServletContext – Shared by all users using the application

52 Choose Right Session Mechanism Options – HttpSession – Hidden fields – Cookies – URL rewriting Factors to consider – Amount of session data – Number of concurrent users

53 Use and Optimize Thread Pool Servlet engine creates a thread for each request by default Avoid “creation” and “destruction” of threads by having a thread pool Set minimum and maximum number of thread pool based on # of concurrent users – Product dependent feature

54 Control HTTPSession Remove session explicitly using HTTPSession.invalidate() – Otherwise session object stays in memory until timeout Set optimal session timeout value Optimize serialization of Session object via Transient

55 Optimize Database Access (Quoted from precisejava.com)

56 Choose Right Driver Use Type 4 driver (Java driver) whenever possible – Best performance since no conversion of JDBC calls to ODBC or native calls Between Type 1 and Type 2, use Type 2

57 Optimize Connection Properties Set optimal row pre-fetch value Use connection pool Control transaction Choose optimal isolation level Close connection when closed

58 Set Optimal Row Pre-fetch Value Depends on application Example code java.util.Properties props = new java.util.Properties(); props.put("defaultRowPrefetch","30"); props.put("defaultBatchValue","5"); Connection con = DriverManger.getConnection(url, props);

59 Use Connection Pool Open/close DB connection takes time Pool holds several open connections Components get connection from pool Pool should expire/reopen connections Pool should support multiple DBs

60 Control Transaction Do batch transaction instead of Autocommitting (commit call after each Statement execution) – Reduce the number of commit calls to the database

61 Example: Batch Transaction try{ connection.setAutoCommit(false); PreparedStatement ps = connection.preareStatement( "UPDATE employee SET Address=? WHERE name=?"); ps.setString(1,"Austin"); ps.setString(2,"RR"); ps.executeUpdate(); PreparedStatement ps1= connection.prepareStatement( "UPDATE account SET salary=? WHERE name=?"); ps1.setDouble(1, ); ps1.setString(2,"RR"); ps1.executeUpdate(); connection.commit(); connection.setAutoCommit(true); } catch(SQLException e){ connection.rollback(); } finally{ if(ps != null){ ps.close();} if(ps1 != null){ps1.close();} if(connection != null){connection.close();} }

62 Choose Optimum Isolation Level 5 Isolation levels – TRANSACTION_NONE – TRANSACTION_READ_COMMITTED – TRANSACTION_READ_UNCOMMITTED – TRANSACTION_REPEATABLE_READ

63 Close Connection When Finished Let garbarge collector to recollect memory as soon as possible

64 Optimization with Statement Choose right Statement interface Do batch update Do batch retrieval using Statement Close Statement when finished

65 Choose Right Statement Interface 3 Statement interfaces – Statement – PreparedStatement – CallableStatement Use PreparedStatement when a Statement is executed repeatedly with different input value Use CallableStatement when a single request has complext Statements

66 Do Batch Update Reduces number of JDBC calls Example statement.addBatch( "sql query1"); statement.addBatch(" sql query2"); statement.addBatch(" sql query3"); statement.executeBatch();

67 Resources Java performance documents – java.sun.com/docs/performance J2EE performance improvement tips – Servlet cache schemes


Download ppt "Optimizing for the Java™ Servlet API & Database Access SSE USTC Qing Ding."

Similar presentations


Ads by Google