Java performance tests

  Abstract: Java performance tests 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="263" height="86" align="center" valign="top"> 
  </ Td> <td width="421" valign="top"> 

  Outlined in the development of the initial performance test is designed to be easily neglected, developers will be to solve a problem and "unscrupulous," the authors involved in the projects have encountered similar problems, string splicing, 
  A large number of calls and network database access and so on the performance of the system have had an impact, but we do not care about these issues, "CPU speed is changing fast," "big change in the memory," and "it seems not so slow it . " 
  There are many commercial availability of the performance testing software, such as Jprofiler, JProbe Profiler, 
  However, in the development of some of them seem distant and expensive. 
  2 goals this paper describes how to use the Java language itself in the way in the development of a performance test to find the bottleneck system, thereby improving the design and testing as far as possible not to modify the object under test. 

  </ Td> </ tr> </ table> 

  3 prior knowledge 
  Object-oriented programming through the use of modular succession abstract thinking to solve the problem domain, but not good modular solutions to all problems.    Sometimes, these problems may have emerged in a number of modules, such as journaling, in order to record each method of entering and leaving the information, you have to add in the way of each log ( "in some method"), and other information.    How to solve such problems?    These issues will be resolved in a number of function points scattered module redundancy will increase, and when the emergence of many functions in a module, code change is hard to maintain.    Therefore, the AOP (Aspect Oriented Programming) came into being.    If we say that OOP (Aobject Oriented Programming) is a concern of the vertical structure, the level of AOP is viewed from the perspective of the problem. 

  Dynamic Agency in a number of run-time interface, each category has a dynamic agent Invocation handler with the corresponding object, this object has InvocationHandler interface, dynamic Acting Deputy interface for dynamic object method call will be transferred The call Invocation handler object invoke method, dynamic agent examples, methods and targets of the implementation of the parameter object can call and get the results. 

  Talking about AOP, we will first think of log records and competence of inspection and management services, Yes, AOP solve these problems is a good way.    Based on the AOP ideas, dynamic agent to resolve the issue of a new category - Performance Test (performance testing). 

  Performance tests include the following: 

  L computing performance: It is likely that people are primarily concerned with the implementation of a simple piece of code that is used by the time 

  L memory consumption: running occupied by the memory size 

  L start time: you start procedures to the normal operation of the procedure time 

  L scalability (scalability) 

  L users aware of (perceived performance): It is not how fast the actual operation procedures, but users feel how fast running. 

  This paper gives the computing performance testing and memory consumption test possible solutions. 



  4 computing performance test 
  4.1 goals: 
  The adoption of the test can be a way of the time required to implement 

  4.2 achieved: 
  Java provides us with a System. CurrentTimeMillis () method, can be milliseconds of the current time, we were in the past must have written procedures similar to the code of the implementation of a certain code and the amount of time. 

  Long start = System.currentTimeMillis (); 

  DoSth (); 

  Long end = System.currentTimeMillis (); 

  System.out.println ( "time lasts" + (end-start) + "ms"); 


  However, in every way to write on the inside of such a code is a very boring things, we adopted the java.lang.reflect.Proxy Java and dynamic agent java.lang.reflect.InvocationHandler good solution to the above problem. 

  We want to test example is the java.util.LinkedList and java.util.ArrayList get (int index), than obviously ArrayList LinkedList efficient, because the former is random access, and the latter needs to order. 

  First, we create an interface 

  (Public interface Foo 

  Public void testArrayList (); 

  Public void testLinkedList (); 

  ) 


  We then create a test object implements this interface 

  Public class FooImpl implements Foo ( 

  Private List link = new LinkedList (); 

  Private List array = new ArrayList (); 



  Public FooImpl () ( 

  For (int i = 0; i <10000; i + +) ( 

  Array.add (new Integer (i)); 

  Link.add (new Integer (i)); 

  ) 

  ) 



  Public void testArrayList () ( 

  For (int i = 0; i <10000; i + +) 

  Array.get (i); 

  ) 



  Public void testLinkedList () ( 

  For (int i = 0; i <10000; i + +) 

  Link.get (i); 

  ) 

  ) 


  What we have to do next key step in achieving InvocationHandler Interface 

  Import java.lang.reflect.InvocationHandler; 

  Import java.lang.reflect.Method; 

  Import java.lang.reflect .*; 



  Public class Handler implements InvocationHandler ( 

  Private Object obj; 

  Public Handler (Object obj) ( 

  This.obj = obj; 

  ) 


  Public static Object newInstance (Object obj) ( 

  Object result = 
  Proxy.newProxyInstance (obj.getClass (). GetClassLoader (), obj.getClass (). GetInterfaces (), new Handler (obj)); 

  Return (result); 

  ) 



  Public Object invoke (Object proxy, Method method, Object [] args) throws Throwable ( 

  Object result; 

  Try ( 

  System.out.print ( "begin method" + method.getName () + "("); 

  For (int i = 0; args! = Null & & i <args.length; i + +) ( 

  If (i> 0) System.out.print (","); 

  System.out.print ( "" + args [i]. ToString ()); 

  ) 

  System.out.println ( ")"); 

  Long start = System.currentTimeMillis (); 

  Result = method.invoke (obj, args); 

  Long end = System.currentTimeMillis (); 

  System.out.println ( "the method" + method.getName () + "lasts" + (end-start) + "ms"); 

  ) Catch (InvocationTargetException e) ( 

  Throw e.getTargetException (); 

  ) Catch (Exception e) ( 

  Throw new RuntimeException ( "unexpected invocation exception:" + e.getMessage ()); 

  Finally () 

  System.out.println ( "end method" + method.getName ()); 

  ) 

  Return result; 

  ) 

  ) 


  Finally, we create a test client, 

  (Public class TestProxy 

  Public static void main (String [] args) ( 

  Try ( 

  Foo foo = (Foo) Handler.newInstance (new FooImpl ()); 

  Foo.testArrayList (); 

  Foo.testLinkedList (); 

  ) Catch (Exception e) ( 

  E.printStackTrace (); 

  ) 

  ) 

  ) 


  Operating results were as follows: 

  Begin method testArrayList () 

  The method testArrayList lasts 0ms 

  End method testArrayList 

  Begin method testLinkedList () 

  The method testLinkedList lasts 219ms 

  End method testLinkedList 


  The benefits of using dynamic agents is you do not need to amend the original code FooImpl, but a disadvantage is that you have to write an interface, if you did not like the original interface to achieve it. 

  4.3 expansion 
  In the above examples demonstrate the use of dynamic agents compare two methods of execution time, sometimes through a simple test to compare the one-sided, we can test the implementation of a number of objects, which calculated the worst and the best average performance.    So that we can "speed up the implementation of the procedures of the regular speed, slow speed to minimize the procedure call." 



  5 memory consumption test 
  5.1 goals 
  When a java application is running, a lot needs to consume memory factors exist, such as object, loading categories, such as threads.    Here only consider the subject of proceedings in the amount of virtual machine heap space, so that we can use Runtime category freeMemory () and totalMemory () method. 

  5.2 Implementation 
  To facilitate period, we have added a category of the current memory consumption. 

  (Class Memory 

  Public static long used () ( 

  Runtime.getRuntime long total = (). TotalMemory (); 

  Runtime.getRuntime long free = (). FreeMemory (); 

  Return (total-free); 

  ) 

  ) 


  Then amend Handler category invoke () method. 

  Public Object invoke (Object proxy, Method method, Object [] args) throws Throwable ( 

  Object result; 

  Try ( 

  System.out.print ( "begin method" + method.getName () + "("); 

  For (int i = 0; args! = Null & & i <args.length; i + +) ( 

  If (i> 0) System.out.print (","); 

  System.out.print ( "" + args [i]. ToString ()); 

  ) 

  System.out.println ( ")"); 

  Memory.used long start = (); 

  Result = method.invoke (obj, args); 

  Memory.used long end = (); 

  System.out.println ( "memory increased by" + (end-start) + "bytes"); 

  ) Catch (InvocationTargetException e) ( 

  Throw e.getTargetException (); 

  ) Catch (Exception e) ( 

  Throw new RuntimeException ( "unexpected invocation exception:" + e.getMessage ()); 

  Finally () 

  System.out.println ( "end method" + method.getName ()); 

  ) 

  Return result; 

  ) 


  At the same time we have also done a test case of a change, testing the same obvious problem, compared to a length of 1000 ArrayList and HashMap space of the size, interfaces, accomplish the following: 

  (Public interface MemoConsumer 

  Public void creatArray (); 

  Public void creatHashMap (); 

  ) 

  Public class MemoConsumerImpl implements MemoConsumer ( 

  ArrayList arr = null; 

  HashMap hash = null; 



  Public void creatArray () ( 

  Arr = new ArrayList (1000); 

  ) 

  Public void creatHashMap () ( 

  Hash = new HashMap (1000); 

  ) 

  ) 


  Test client code as follows: 

  MemoConsumer arrayMemo = (MemoConsumer) Handler.newInstance (new MemoConsumerImpl ()); 

  ArrayMemo.creatArray (); 

  ArrayMemo.creatHashMap (); 


  Test results are as follows: 

  Begin method creatArray () 

  Memory increased by 4400bytes 

  End method creatArray 

  Begin method creatHashMap () 

  Memory increased by 4480bytes 

  End method creatHashMap 


  Results at a glance, we can see that we only need to amend invoke () method, and simple implementation of client calls on it. 

  6 CONCLUSION 
  AOP through decomposition concerns and OOP complement each other so that the process more concise easy to understand, the Java language itself provides the dynamic agents help, we could easily decomposed concern, and achieved good results.    But test object must achieve interface to a certain extent, limit the use of dynamic agents, can learn from the Spring CGlib used in the past for not achieving any of creating dynamic interface agent. 

  </ Td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply