Performance Measurer in Java+Aspect

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
thekashyap thekashyap is offline Offline May 8th, 2007, 3:09 am |
0
------------------------------------------------------------
Intro:
This is a small program that measures the performance of given java classes.
In snippet there are 2 classes:
1. TestClass.java contains all java classes that are being measured for performance, I've picked up Narue's Sorting Algorithms as AUT (application under test)
2. AJPerf.java: contains the aspect that actually does the work of measuring.
------------------------------------------------------------
Requirements:
Here are the tools/versions used:
- AspectJ Compiler 1.5.2 built on Friday Jun 30, 2006 at 09:30:27 GMT
Can be downloaded from here.
- java version "1.5.0_09"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_09-b03)
Java HotSpot(TM) Client VM (build 1.5.0_09-b03, mixed mode, sharing)
Can be downloaded from here.

I was using Windows NT 5.0 build 2195 Service Pack 4, but should work on all platforms due to Java.
------------------------------------------------------------
Here is how to use it for your class(s):
1. Install aspectJ compiler and JRE/JDK 1.5+
2. Look for TODO in AJPerf.java and make required modifications. Just 3 of them as I see it.
3. Compile using aspectJ compiler. (ajc)
4. Run using aspectJ runner. (aj)
5. Copy and paste the output to excel sheet. Goto Data->Text to columns...
Select "Delimited" and press Next
Check "Other" and type "#" in edit box next to it.
Click finish.
-------------------------------
Example output:
Here is what I had to do if it helps:
E:\Temp\AJPerf> set CLASSPATH=%CLASSPATH%:/home/om1/aspectj1.5/lib/aspectjlib.jar:/home/om1/aspectj1.5/lib/aspectjrt.jar:/home/om1/aspectj1.5/lib/aspectjtools.jar:/home/om1/aspectj1.5/lib/aspectjweaver.jar
E:\Temp\AJPerf> ajc -1.5 -d . *.java
E:\Temp\AJPerf> aj -classpath E:\AspectJ\lib\aspectjweaver.jar;.;E:\AspectJ\lib\aspectjrt.jar;E:\AspectJ\lib\aspectjtools.jar;E:\AspectJ\lib\aspectjlib.jar AJPerf


The OUTPUT:

Function Name#Total Time#Number of calls#Time per call
void in.kash.test.TestClass.SortSelection.push_down(Integer[], int, int)#23483420#2900#8097.0
void in.kash.test.TestClass.SortInsertion.shell_sort(Integer[], int)#971352#100#9713.0
void in.kash.test.TestClass.SortInsertion.insertion_sort(Integer[], int)#2366772#200#11833.0
void in.kash.test.TestClass.SortExchange.quicksort(Integer[], int, int)#1239543#100#12395.0
void in.kash.test.TestClass.SortExchange.quicksort(Integer[], int)#21682929#100#216829.0
void in.kash.test.TestClass.SortExchange.bubble_sort(Integer[], int)#843949#100#8439.0
void in.kash.test.TestClass.main(String[])#210874287#1#2.10874287E8
void in.kash.test.TestClass.SortSelection.selection_sort(Integer[], int)#1347096#100#13470.0
void in.kash.test.TestClass.SortSelection.heapsort(Integer[], int)#44056719#100#440567.0
E:\Temp\AJPerf>
Quick reply to this message  
Java Syntax
  1. //There are 2 files below
  2. //Classes whose performance is being measured are in TestClass.java
  3. //AJPerf is the aspect class that measures the performance.
  4.  
  5. /*
  6.  *
  7.  * Filename : AJPerf.java
  8.  * Author : Kashyap Bhatt
  9.  * History : Created on May 4, 2007
  10.  *
  11.  * export CLASSPATH=$CLASSPATH:/home/om1/aspectj1.5/lib/aspectjlib.jar:/home/om1/aspectj1.5/lib/aspectjrt.jar:/home/om1/aspectj1.5/lib/aspectjtools.jar:/home/om1/aspectj1.5/lib/aspectjweaver.jar
  12.  * export PATH=$PATH:~om1/aspectj1.5/bin/
  13.  * ajc -1.5 -d . *.java
  14.  * aj -classpath E:\AspectJ\lib\aspectjweaver.jar;.;E:\AspectJ\lib\aspectjrt.jar;E:\AspectJ\lib\aspectjtools.jar;E:\AspectJ\lib\aspectjlib.jar AJPerf
  15.  *
  16.  */
  17.  
  18. //TODO: import classes of your application.
  19. import in.kash.test.* ;
  20. import in.kash.test.TestClass.* ;
  21.  
  22. import java.util.Stack ;
  23. import java.util.HashMap ;
  24.  
  25. aspect AJPerf {
  26.  
  27. //TODO: Change pointcut myClass(), but putting names of your classes.
  28. pointcut myClass(): within(TestClass) || within(SortInsertion)
  29. || within(SortExchange) || within(SortSelection) ;
  30.  
  31. pointcut myMethod(): myClass() && execution(* *(..)) ;
  32.  
  33. pointcut myConstructor(): myClass() && execution(new(..)) ;
  34.  
  35. before (): myMethod() || myConstructor() {
  36. traceEntry( "" + thisJoinPointStaticPart.getSignature() ) ;
  37. }
  38.  
  39. after(): myMethod() || myConstructor() {
  40. traceExit( "" + thisJoinPointStaticPart.getSignature() ) ;
  41. }
  42.  
  43. public static class MethodPerfInfo {
  44. public long mTotalTime ;
  45. public long mNumCalls ;
  46. MethodPerfInfo() { mTotalTime = 0; mNumCalls = 0; }
  47. public String toString() { return "mTotalTime = " + mTotalTime + " mNumCalls = " + mNumCalls + "\n" ; }
  48. }
  49.  
  50. private static Stack<Long> mTimeStack = new Stack<Long>() ;
  51. private static HashMap<String, MethodPerfInfo> mOutMap = new HashMap<String, MethodPerfInfo>(1000) ;
  52.  
  53. void traceEntry( String s ) {
  54. //sop("==>" + s) ;
  55. mTimeStack.push(new Long(System.nanoTime())) ;
  56. if( ! mOutMap.containsKey(s) )
  57. mOutMap.put( s, new MethodPerfInfo() ) ;
  58. }
  59.  
  60. void traceExit( String s ) {
  61. //sop("<==" + s) ;
  62. MethodPerfInfo obj = mOutMap.get( s ) ;
  63. obj.mTotalTime += System.nanoTime() - (mTimeStack.pop()).longValue() ;
  64. obj.mNumCalls++ ;
  65. mOutMap.put( s, obj ) ;
  66. }
  67.  
  68. private static void sop( String s ) { System.out.println(s) ; }
  69.  
  70. public static void main(String[] args) {
  71. //TODO: call your applications's main here.
  72. TestClass.main(args) ;
  73.  
  74. sop( "\n\nThe OUTPUT:\n" ) ;
  75. Object[] func_names = mOutMap.keySet().toArray() ;
  76. sop("Function Name#Total Time(nano)#Number of calls#Time per call(nano)" ) ;
  77. for( int i = 0; i < func_names.length; i++ ) {
  78. MethodPerfInfo obj = mOutMap.get(func_names[i]) ;
  79. sop(func_names[i] + "#" + obj.mTotalTime + "#" + obj.mNumCalls + "#" + Double.toString(obj.mTotalTime/obj.mNumCalls) ) ;
  80. }
  81. }
  82. }
  83.  
  84. //--------------------End of AJPerf.java-------------------
  85.  
  86. /*
  87.  * Filename : TestClass.java
  88.  * Author : Kashyap Bhatt
  89.  * History : Created on May 4, 2007
  90.  */
  91. package in.kash.test;
  92.  
  93. import java.math.*;
  94.  
  95. public class TestClass {
  96.  
  97. public static void main ( String[] args ) {
  98. Integer[] a = new Integer[20];
  99. Integer[] b ;
  100.  
  101. for( int xxx = 0; xxx < 100; xxx++ ) {
  102. for ( int i = 0; i < 20; i++ ) {
  103. a[i] = new Integer( (int)(Math.random() * 100) );
  104. }
  105. //save a
  106. b = a ;
  107.  
  108. /*System.out.println ( "Before: " );
  109.  
  110.   for ( int i = 0; i < a.length; i++ ) {
  111.   System.out.print ( i + " " );
  112.   }
  113.   System.out.println ( "" );*/
  114.  
  115. SortInsertion.insertion_sort( a, a.length ) ;
  116.  
  117. a = b ;
  118. SortInsertion.shell_sort( a, a.length ) ;
  119.  
  120. a = b ;
  121. SortExchange.bubble_sort( a, a.length ) ;
  122. a = b ;
  123. SortExchange.quicksort( a, a.length ) ;
  124.  
  125. a = b ;
  126. SortSelection.selection_sort( a, a.length ) ;
  127. a = b ;
  128. SortSelection.heapsort( a, a.length ) ;
  129.  
  130. /*System.out.println ( "After: " );
  131.   for ( int i = 0; i < a.length; i++ ) {
  132.   System.out.print ( i + " " );
  133.   }
  134.   System.out.println ( "" );*/
  135. }
  136. }
  137.  
  138. public TestClass() {
  139. //System.out.println( "Inside TestClass()" ) ;
  140. }
  141.  
  142.  
  143. /*
  144.   * Source: http://www.daniweb.com/code/snippet77.html
  145.   * modified to make it NON-generic.
  146.   */
  147.  
  148. public static class SortInsertion {
  149. // Insertion sort
  150. public static void insertion_sort ( Integer[] list, int size ) {
  151. for ( int i = 1; i < size; i++ ) {
  152. Integer save = list[i];
  153. int j = i;
  154.  
  155. while ( j >= 1 && save.compareTo ( list[j - 1] ) < 0 ) {
  156. list[j] = list[j - 1];
  157. --j;
  158. }
  159.  
  160. list[j] = save;
  161. }
  162. }
  163.  
  164. // Shell sort
  165. public static
  166. void shell_sort ( Integer[] list, int size ) {
  167. int h = 1;
  168.  
  169. while ( h <= size / 9 ) {
  170. h = 3 * h + 1;
  171. }
  172.  
  173. while ( h > 0 ) {
  174. for ( int i = h; i < size; i++ ) {
  175. Integer save = list[i];
  176. int j = i;
  177.  
  178. while ( j >= h && save.compareTo ( list[j - h] ) < 0 ) {
  179. list[j] = list[j - h];
  180. j -= h;
  181. }
  182.  
  183. list[j] = save;
  184. }
  185.  
  186. h /= 3;
  187. }
  188. }
  189. }
  190.  
  191. public static class SortExchange {
  192. // Bubble sort
  193. public static
  194. void bubble_sort ( Integer[] list, int size ) {
  195. int bound = size - 1;
  196.  
  197. while ( bound > 0 ) {
  198. int new_bound = 0;
  199.  
  200. for ( int i = 0; i < bound; i++ ) {
  201. if ( list[i].compareTo ( list[i + 1] ) > 0 ) {
  202. Integer save = list[i];
  203. list[i] = list[i + 1];
  204. list[i + 1] = save;
  205.  
  206. new_bound = i;
  207. }
  208. }
  209.  
  210. bound = new_bound;
  211. }
  212. }
  213.  
  214. // Quicksort
  215. private static void quicksort ( Integer[] list, int l, int r ) {
  216. int[] stack = new int[50];
  217. int m, top = 0;
  218. Integer pivot;
  219.  
  220. while ( true ) {
  221. while ( r > l + 10 ) {
  222. // Median of three partition
  223. m = ( l + r ) / 2;
  224.  
  225. if ( list[l].compareTo ( list[m] ) > 0 ) {
  226. Integer save = list[l];
  227. list[l] = list[m];
  228. list[m] = save;
  229. }
  230. if ( list[l].compareTo ( list[r] ) > 0 ) {
  231. Integer save = list[l];
  232. list[l] = list[r];
  233. list[r] = save;
  234. }
  235. if ( list[m].compareTo ( list[r] ) > 0 ) {
  236. Integer save = list[m];
  237. list[m] = list[r];
  238. list[r] = save;
  239. }
  240.  
  241. if ( r - l <= 2 ) {
  242. break;
  243. }
  244.  
  245. pivot = list[m];
  246. list[m] = list[r - 1];
  247. list[r - 1] = pivot;
  248.  
  249. int i = l;
  250. int j = r - 1;
  251.  
  252. while ( true ) {
  253. while ( list[++i].compareTo ( pivot ) < 0 ) {
  254. // No body
  255. }
  256. while ( list[--j].compareTo ( pivot ) > 0 ) {
  257. // No body
  258. }
  259.  
  260. if ( j < i ) {
  261. break;
  262. }
  263.  
  264. Integer save = list[i];
  265. list[i] = list[j];
  266. list[j] = save;
  267. }
  268.  
  269. pivot = list[i];
  270. list[i] = list[r - 1];
  271. list[r - 1] = pivot;
  272.  
  273. // Simulated recursive calls
  274. if ( j - l > r - i ) {
  275. stack[top++] = l;
  276. stack[top++] = j;
  277. l = i + 1;
  278. } else {
  279. stack[top++] = i + 1;
  280. stack[top++] = r;
  281. r = j;
  282. }
  283. }
  284.  
  285. if ( top == 0 ) {
  286. break;
  287. }
  288.  
  289. r = stack[--top];
  290. l = stack[--top];
  291. }
  292. }
  293.  
  294. public static
  295. void quicksort ( Integer[] list, int size ) {
  296. quicksort ( list, 0, size - 1 );
  297. SortInsertion.insertion_sort ( list, size );
  298. }
  299. }
  300.  
  301. public static class SortSelection {
  302. // Selection sort
  303. public static
  304. void selection_sort ( Integer[] list, int size ) {
  305. for ( int i = 0; i < size - 1; i++ ) {
  306. int min = i;
  307.  
  308. for ( int j = i + 1; j < size; j++ ) {
  309. if ( list[j].compareTo ( list[min] ) < 0 ) {
  310. min = j;
  311. }
  312. }
  313.  
  314. if ( min != i ) {
  315. Integer save = list[i];
  316. list[i] = list[min];
  317. list[min] = save;
  318. }
  319. }
  320. }
  321.  
  322. // Heapsort
  323. private static void push_down ( Integer[] list, int l, int r ) {
  324. int i, j;
  325.  
  326. for ( i = l; ( j = i * 2 + 1 ) <= r; i = j ) {
  327. if ( j + 1 <= r && list[j + 1].compareTo ( list[j] ) > 0 ) {
  328. ++j;
  329. }
  330.  
  331. Integer save = list[i];
  332. list[i] = list[j];
  333. list[j] = save;
  334. }
  335.  
  336. while ( true ) {
  337. j = ( i - 1 ) / 2;
  338.  
  339. if ( j < l || j == i || list[j].compareTo ( list[i] ) > 0 ) {
  340. break;
  341. }
  342.  
  343. Integer save = list[i];
  344. list[i] = list[j];
  345. list[j] = save;
  346.  
  347. i = j;
  348. }
  349. }
  350.  
  351. public static
  352. void heapsort ( Integer[] list, int size ) {
  353. if ( size-- < 2 ) {
  354. return;
  355. }
  356.  
  357. int i;
  358.  
  359. for ( i = ( size - 1 ) / 2; i >= 0; i-- ) {
  360. push_down ( list, i, size );
  361. }
  362.  
  363. while ( size > 0 ) {
  364. Integer save = list[size];
  365. list[size] = list[0];
  366. list[0] = save;
  367.  
  368. push_down ( list, 0, --size );
  369. }
  370. }
  371. }
  372.  
  373. }
0
peter_budo peter_budo is offline Offline | May 8th, 2007
You didn't elaborated what you try to achive

Expecting people to use some uknown/custome libraries in the CLASSPATH

You are missing even basic explanation what each sort method is about, what it does

Good code, but you loosing on documentation grounds
 
0
thekashyap thekashyap is offline Offline | May 8th, 2007
@Peter:

Thanks.

>> You didn't elaborated what you try to achive
From Intro "This is a small program that measures the performance of given java classes."
One can use it when they wanna find out what to optimize in their code by looking at figures.

>> Expecting people to use some uknown/custome libraries in the CLASSPATH
That's an example (intentionally put in there to help others remember that they need to add aspect's jar files to their classpath). None of the jar files there are "unknown/custom" all are aspectJ's whose link is also posted.
What ppl are supposed to do is
"3. Compile using aspectJ compiler. (ajc)
4. Run using aspectJ runner. (aj)"

>> You are missing even basic explanation what each sort method is about, what it does
the methods themselves are irrelevant (not even my code as mentioned in code as well as explanation, along with link to the source and credit to author).
But you're right, I haven't put enough comments in code that I wrote. It doesn't seem possible to edit it now, so hope ppl can understand, it's pretty simple.
 
0
thekashyap thekashyap is offline Offline | May 8th, 2007
BTW: This thread was teh trigger for me to write this snippet. May be you'll get context (what is this program trying to achieve) from there as well.
 
 

Message:


Similar Threads
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC