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.9KB

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