You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Bench.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package javassist;
  2. import junit.framework.*;
  3. import javassist.expr.*;
  4. import javassist.compiler.*;
  5. public class Bench extends JvstTestRoot {
  6. public void testProceed() throws Exception {
  7. CtClass cc = sloader.get("test.BenchProceed");
  8. CtMethod m1 = cc.getDeclaredMethod("p");
  9. m1.instrument(new ExprEditor() {
  10. public void edit(MethodCall m) throws CannotCompileException {
  11. if (m.getMethodName().equals("calc"))
  12. m.replace("{ before($args); $_ = $proceed($$); }");
  13. }
  14. });
  15. CtMethod m2 = cc.getDeclaredMethod("q");
  16. m2.instrument(new ExprEditor() {
  17. public void edit(MethodCall m) throws CannotCompileException {
  18. if (m.getMethodName().equals("calc"))
  19. m.replace("{ $_ = ($r)replace($args); }");
  20. }
  21. });
  22. CtMethod m3 = cc.getDeclaredMethod("s");
  23. m3.instrument(new ExprEditor() {
  24. public void edit(MethodCall m) throws CannotCompileException {
  25. if (m.getMethodName().equals("calc2"))
  26. m.replace(
  27. "{ long start = System.currentTimeMillis();"
  28. + "$_ = $proceed($$);"
  29. + "long elapsed = System.currentTimeMillis() - start;"
  30. + "System.out.println(elapsed); }");
  31. }
  32. });
  33. CtMethod m4 = cc.getDeclaredMethod("t");
  34. m4.instrument(new ExprEditor() {
  35. public void edit(MethodCall m) throws CannotCompileException {
  36. if (m.getMethodName().equals("calc2"))
  37. m.replace(
  38. "{ long start = System.currentTimeMillis();"
  39. + "$_ = $proceed($$);"
  40. + "System.out.println(System.currentTimeMillis() - start);"
  41. + "}");
  42. }
  43. });
  44. cc.writeFile();
  45. Object obj = make(cc.getName());
  46. int ptime = invoke(obj, "p");
  47. int qtime = invoke(obj, "q");
  48. System.out.println("time: (p) " + ptime + ", (q) " + qtime);
  49. System.out.println("s:");
  50. invoke(obj, "s");
  51. System.out.println("t:");
  52. invoke(obj, "t");
  53. assertTrue(ptime < qtime);
  54. }
  55. public void testProceedNew() throws Exception {
  56. CtClass cc = sloader.get("test.BenchProceedNew");
  57. CtMethod m1 = cc.getDeclaredMethod("jvst0");
  58. m1.instrument(new ExprEditor() {
  59. public void edit(NewExpr m) throws CannotCompileException {
  60. m.replace("{ $_ = $proceed($$); }");
  61. }
  62. });
  63. CtMethod m2 = cc.getDeclaredMethod("jvst2");
  64. m2.instrument(new ExprEditor() {
  65. public void edit(NewExpr m) throws CannotCompileException {
  66. m.replace("{ $_ = $proceed($$); }");
  67. }
  68. });
  69. cc.writeFile();
  70. Object obj = make(cc.getName());
  71. int qtime = invoke(obj, "jvst0");
  72. int ptime = invoke(obj, "org0");
  73. System.out.println("time: (org0) " + ptime + ", (jvst0) " + qtime);
  74. qtime = invoke(obj, "jvst2");
  75. ptime = invoke(obj, "org2");
  76. System.out.println("time: (org2) " + ptime + ", (jvst2) " + qtime);
  77. }
  78. public void testStaticMethod() throws Exception {
  79. CtClass cc = sloader.get("test.BenchStaticMethod");
  80. CtMethod m1 = cc.getDeclaredMethod("test");
  81. m1.instrument(new ExprEditor() {
  82. public void edit(MethodCall m) throws CannotCompileException {
  83. if (m.getMethodName().equals("foo"))
  84. m.replace("{ num += $1; $_ = $proceed($$); }");
  85. }
  86. });
  87. cc.writeFile();
  88. Object obj = make(cc.getName());
  89. int qtime = invoke(obj, "test");
  90. int ptime = invoke(obj, "orgTest");
  91. System.out.println(
  92. "BenchStaticMethod time: (org) " + ptime + ", (jvst) " + qtime);
  93. }
  94. public void testStaticField() throws Exception {
  95. System.out.println(sloader);
  96. Javac jc = new Javac(sloader.get("test.StaticField"));
  97. long t0 = System.currentTimeMillis();
  98. for (int i = 0; i < 100; i++)
  99. jc.compileStmnt("{ int counter = 0; counter++; }");
  100. t0 = System.currentTimeMillis() - t0;
  101. System.out.println("local variable: " + (t0 * 10) + " usec");
  102. long t = System.currentTimeMillis();
  103. for (int i = 0; i < 100; i++)
  104. jc.compileStmnt("{ test.StaticField.counter++; }");
  105. t = System.currentTimeMillis() - t;
  106. System.out.println("StaticField: " + (t * 10) + " usec");
  107. long t2 = System.currentTimeMillis();
  108. for (int i = 0; i < 100; i++)
  109. jc.compileStmnt("{ test.StaticField#counter++; }");
  110. t2 = System.currentTimeMillis() - t2;
  111. System.out.println("StaticField with #: " + (t2 * 10) + " usec");
  112. long t3 = System.currentTimeMillis();
  113. for (int i = 0; i < 100; i++)
  114. jc.compileStmnt("{ StaticField.counter2++; }");
  115. t3 = System.currentTimeMillis() - t3;
  116. System.out.println("StaticField without package: " + (t3 * 10) + " usec");
  117. long t4 = System.currentTimeMillis();
  118. for (int i = 0; i < 100; i++)
  119. jc.compileStmnt("{ test.StaticField.counter++; }");
  120. t4 = System.currentTimeMillis() - t4;
  121. System.out.println("StaticField: " + (t4 * 10) + " usec");
  122. long t5 = System.currentTimeMillis();
  123. for (int i = 0; i < 100; i++)
  124. jc.compileStmnt("{ System.out.println(); }");
  125. t5 = System.currentTimeMillis() - t5;
  126. System.out.println("println: " + (t5 * 10) + " usec");
  127. }
  128. public static Test suite() {
  129. TestSuite suite = new TestSuite("Benchmark Tests");
  130. suite.addTestSuite(Bench.class);
  131. suite.addTestSuite(testproxy.ProxyFactoryPerformanceTest.class);
  132. return suite;
  133. }
  134. }