Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LazyTjp.aj 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. public class LazyTjp {
  2. private static final int N = 10000000;
  3. // if lazy tjp is working, then calling the advice that uses thisJoinPoint should
  4. // take at least this much longer than using an if pcd to bypass the advice
  5. private static final double minimumRatio = 1.8; // was 8 but jvm seems to be improving all the time!!
  6. public static void main(String[] args) {
  7. Trace.enabled = false;
  8. double tOff = timeIt(); // throw the first result out for warm-up
  9. tOff = timeIt();
  10. Trace.enabled = true;
  11. double tOn = timeIt();
  12. Trace.enabled = false;
  13. double tEasy = timeIt0();
  14. double tGone = timeIt1();
  15. System.out.println("tOff: " + tOff + ", tOn: " + tOn + ", tEasy: " + tEasy + ", tGone: " + tGone);
  16. System.out.println("ratio: " + tOn/tOff);
  17. Trace.enabled = false;
  18. double tOff2 = timeIt2();
  19. tOff2 = timeIt2();
  20. Trace.enabled = true;
  21. double tOn2 = timeIt2();
  22. System.out.println("tOff2: " + tOff2 + ", tOn2: " + tOn2);
  23. System.out.println("ratio2: " + tOn2/tOff2);
  24. if (tOn/tOff < minimumRatio) {
  25. throw new IllegalStateException("tOn/tOff = " + tOn/tOff + " < " + minimumRatio);
  26. }
  27. }
  28. public static double timeIt() {
  29. long start = System.currentTimeMillis();
  30. for (int i=0; i < N; i++) {
  31. doit(i);
  32. }
  33. long stop = System.currentTimeMillis();
  34. return (stop-start)/1000.0;
  35. }
  36. private static int doit(int x) {
  37. return x+1;
  38. }
  39. public static double timeIt0() {
  40. long start = System.currentTimeMillis();
  41. for (int i=0; i < N; i++) {
  42. doit0(i);
  43. }
  44. long stop = System.currentTimeMillis();
  45. return (stop-start)/1000.0;
  46. }
  47. private static int doit0(int x) {
  48. return x+1;
  49. }
  50. public static double timeIt1() {
  51. long start = System.currentTimeMillis();
  52. for (int i=0; i < N; i++) {
  53. doit1(i);
  54. }
  55. long stop = System.currentTimeMillis();
  56. return (stop-start)/1000.0;
  57. }
  58. private static int doit1(int x) {
  59. return x+1;
  60. }
  61. public static double timeIt2() {
  62. long start = System.currentTimeMillis();
  63. for (int i=0; i < N; i++) {
  64. doit2(i);
  65. }
  66. long stop = System.currentTimeMillis();
  67. return (stop-start)/1000.0;
  68. }
  69. private static int doit2(int x) {
  70. return x+1;
  71. }
  72. private static int doit3(int x) {
  73. return x+1;
  74. }
  75. }
  76. aspect Trace {
  77. public static boolean enabled = false;
  78. public static int counter = 0;
  79. pointcut traced(): if (enabled) && execution(* LazyTjp.doit(..));
  80. before(): traced() {
  81. Object[] args = thisJoinPoint.getArgs();
  82. counter += args.length;
  83. }
  84. before(): execution(* LazyTjp.doit0(..)) {
  85. counter += 1;
  86. }
  87. pointcut traced2(): if (enabled) && execution(* LazyTjp.doit2(..));
  88. before(): traced2() {
  89. Object[] args = thisJoinPoint.getArgs();
  90. counter += args.length;
  91. }
  92. after() returning: traced2() {
  93. Object[] args = thisJoinPoint.getArgs();
  94. counter += args.length;
  95. }
  96. pointcut traced3(): if (enabled) && execution(* LazyTjp.doit3(..));
  97. before(): traced3() {
  98. Object[] args = thisJoinPoint.getArgs();
  99. counter += args.length;
  100. }
  101. Object around(): traced3() { // expect Xlint warning in -XlazyTjp mode
  102. return proceed();
  103. }
  104. }