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.

WeaveTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * Noel Markham, Matthew Webster initial implementation
  11. * ******************************************************************/
  12. import java.io.BufferedOutputStream;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. /**
  17. * @author Noel Markham
  18. */
  19. public class WeaveTest {
  20. private static final String OUTPUT_PACKAGE = "out";
  21. private static final int WEAVE_FAST = 1;
  22. private static final int WEAVE_MED = 2;
  23. private static final int WEAVE_SLOW = 3;
  24. public static final String EXECUTION_SLOW = "execution(void *(..))";
  25. public static final String EXECUTION_MED = "args(" + OUTPUT_PACKAGE + ".C0) && execution(void m0(..))";
  26. public static final String EXECUTION_FAST = "within(" + OUTPUT_PACKAGE + ".C0) && execution(void m0(..))";
  27. public static final String GET_SLOW = "get(int *)";
  28. public static final String GET_MED = "target(" + OUTPUT_PACKAGE + ".C0) && get(int i0)";
  29. public static final String GET_FAST = "get(int " + OUTPUT_PACKAGE + ".C0.i0)";
  30. // Defaults, can be changed with command-line args
  31. private static int NUMCLASSES = 5;
  32. private static int ITERATIONS = 3;
  33. private static int METHODLINES = 20;
  34. private static int NUMMETHODS = 100;
  35. private static boolean INCLUDE_TEST_CLASSES = false;
  36. private static boolean TEST_GET = true;
  37. private static boolean TEST_EXECUTION = true;
  38. private static boolean ALL_POINTCUT_TESTS = true;
  39. private static boolean TEST_ONE = false;
  40. private static boolean TEST_ONE_SEARCH_ALL = false;
  41. private static boolean TEST_ALL = false;
  42. private static boolean ECHO = false;
  43. public static long[] compileTimes = new long[ITERATIONS];
  44. public static long[] executionSlowTimes = new long[ITERATIONS];
  45. public static long[] executionMedTimes = new long[ITERATIONS];
  46. public static long[] executionFastTimes = new long[ITERATIONS];
  47. public static long[] getSlowTimes = new long[ITERATIONS];
  48. public static long[] getMedTimes = new long[ITERATIONS];
  49. public static long[] getFastTimes = new long[ITERATIONS];
  50. private static final String NL = System.getProperty("line.separator", "\n");
  51. private static final File outputDir = new File(System.getProperty("user.dir") + File.separatorChar + OUTPUT_PACKAGE);
  52. static {
  53. outputDir.mkdirs();
  54. }
  55. public static void main(String[] args) throws IOException {
  56. //if (args.length > 0) parseArgs(args);
  57. parseArgs(args);
  58. if(ECHO) System.out.println("Weave-time Test");
  59. if(ECHO) System.out.println("---------------");
  60. if(ECHO) System.out.println("Number of classes: " + NUMCLASSES);
  61. if(ECHO) System.out.println("Number of methods: " + NUMMETHODS);
  62. if(ECHO) System.out.println("Number of method lines: " + METHODLINES);
  63. if(ECHO) System.out.println("Number of test iterations: " + ITERATIONS);
  64. if(ECHO)
  65. if(INCLUDE_TEST_CLASSES) System.out.println("Including advice test classes");
  66. if(ECHO)
  67. if(TEST_GET && !TEST_EXECUTION) System.out.println("Weaving only get advice");
  68. if(ECHO)
  69. if(TEST_EXECUTION && !TEST_GET) System.out.println("Weaving only execution advice");
  70. if(ECHO) {
  71. if(!ALL_POINTCUT_TESTS) {
  72. if(TEST_ONE) System.out.println("Weaving one poinctcut");
  73. if(TEST_ONE_SEARCH_ALL) System.out.println("Weaving one pointcut, searching all");
  74. if(TEST_ALL) System.out.println("Weaving all");
  75. }
  76. }
  77. if(ECHO) System.out.println();
  78. createClasses();
  79. compileClasses();
  80. boolean warm = false;
  81. if (TEST_EXECUTION) {
  82. String advice = "execution";
  83. createAspects(advice);
  84. //Warm up the weaver
  85. weaveAllAspects(advice, WEAVE_FAST);
  86. warm = true;
  87. weaveAspects(advice);
  88. }
  89. if (TEST_GET) {
  90. String advice = "get";
  91. createAspects(advice);
  92. if(!warm) weaveAllAspects(advice, WEAVE_FAST);
  93. weaveAspects(advice);
  94. }
  95. }
  96. private static void parseArgs(String[] args) {
  97. if(args == null || args.length <= 0) return;
  98. int i = 0;
  99. boolean error = false;
  100. while(i < args.length) {
  101. String arg = args[i++];
  102. try {
  103. if(arg.equals("-c")) { // Number of classes
  104. if(i < args.length)
  105. NUMCLASSES = Integer.parseInt(args[i++]);
  106. else error = true;
  107. }
  108. else if(arg.equals("-m")) { // Number of methods
  109. if(i < args.length)
  110. NUMMETHODS = Integer.parseInt(args[i++]);
  111. else error = true;
  112. }
  113. else if(arg.equals("-l")) { // Number of method lines
  114. if(i < args.length)
  115. METHODLINES = Integer.parseInt(args[i++]);
  116. else error = true;
  117. }
  118. else if(arg.equals("-i")) { // Number of iterations
  119. if(i < args.length)
  120. ITERATIONS = Integer.parseInt(args[i++]);
  121. else error = true;
  122. }
  123. else if(arg.equals("-include-tests")) {
  124. if(i < args.length) {
  125. arg = args[i++];
  126. if(arg.equalsIgnoreCase("y")) INCLUDE_TEST_CLASSES = true;
  127. else if(arg.equalsIgnoreCase("n")) INCLUDE_TEST_CLASSES = false;
  128. else error = true;
  129. }
  130. }
  131. else if(arg.equals("-advice")) {
  132. String advice = args[i++];
  133. if(advice.equals("get")){
  134. TEST_GET = true;
  135. TEST_EXECUTION = false;
  136. }
  137. else if(advice.equals("execution")){
  138. TEST_EXECUTION = true;
  139. TEST_GET = false;
  140. }
  141. else error = true;
  142. }
  143. else if(arg.equals("-pointcut")) {
  144. ALL_POINTCUT_TESTS = false;
  145. String advice = args[i++];
  146. if(advice.equals("fast")) TEST_ONE = true;
  147. else if(advice.equals("meduim")) TEST_ONE_SEARCH_ALL = true;
  148. else if(advice.equals("slow")) TEST_ALL = true;
  149. else error = true;
  150. }
  151. else if(arg.equals("-echo")) {
  152. ECHO = true;
  153. }
  154. else if (arg.equals("-help")) {
  155. usage();
  156. }
  157. else error = true;
  158. } catch (NumberFormatException e) {
  159. usage();
  160. }
  161. if(error) usage();
  162. compileTimes = new long[ITERATIONS];
  163. executionSlowTimes = new long[ITERATIONS];
  164. executionMedTimes = new long[ITERATIONS];
  165. executionFastTimes = new long[ITERATIONS];
  166. getSlowTimes = new long[ITERATIONS];
  167. getMedTimes = new long[ITERATIONS];
  168. getFastTimes = new long[ITERATIONS];
  169. }
  170. }
  171. private static void usage() {
  172. System.err.println("Usage:");
  173. System.err.println("\tjava WeaveTests [-c num_of_classes] [-m num_of_methods] " +
  174. "[-l num_of_method_lines] [-i num_of_iterations]" +
  175. "\n\t\t[-include-tests y|n] [-advice get|execution] [-pointcut fast|medium|slow] [-echo] [-help]");
  176. System.exit(-1);
  177. }
  178. /**
  179. * Will create a number of classes of the following form:
  180. *
  181. * public class C0 {
  182. *
  183. * int i0;
  184. * ...
  185. * int iN;
  186. *
  187. * void m0(C0 arg) {
  188. * i0++;
  189. * ...
  190. * iN++;
  191. * }
  192. * ...
  193. * void mN(C0 arg) {
  194. * ...
  195. * }
  196. *
  197. */
  198. public static void createClasses() throws IOException {
  199. if(ECHO) System.out.println("Creating classes");
  200. for(int classcounter = 0; classcounter < NUMCLASSES; classcounter++) {
  201. StringBuilder classfile = new StringBuilder(1000);
  202. classfile.append("// Auto-generated" + NL);
  203. classfile.append("package " + OUTPUT_PACKAGE + ";" + NL + NL);
  204. classfile.append("public class C" + classcounter + " {" + NL + NL);
  205. for(int intdeclaration = 0; intdeclaration < METHODLINES; intdeclaration++) {
  206. classfile.append("\tint i" + intdeclaration + ";" + NL);
  207. }
  208. classfile.append("\tint getter;" + NL);
  209. classfile.append(NL);
  210. for(int methodcounter = 0; methodcounter < NUMMETHODS; methodcounter++) {
  211. classfile.append("\tvoid m" + methodcounter + "(C" + classcounter + " arg) {" + NL);
  212. for(int methodbody = 0; methodbody < METHODLINES; methodbody++) {
  213. classfile.append("\t\ti" + methodbody + "++;" + NL);
  214. }
  215. classfile.append("\t}" + NL + NL);
  216. }
  217. classfile.append("}" + NL);
  218. try {
  219. File f = new File(outputDir, ("C" + classcounter + ".java"));
  220. FileOutputStream fos = new FileOutputStream(f);
  221. BufferedOutputStream bos = new BufferedOutputStream(fos);
  222. bos.write(new String(classfile).getBytes());
  223. bos.close();
  224. fos.close();
  225. } catch (IOException e) {
  226. e.printStackTrace();
  227. System.exit(-1);
  228. }
  229. }
  230. if (INCLUDE_TEST_CLASSES) {
  231. StringBuilder testFiles = new StringBuilder(1000);
  232. try {
  233. testFiles.append("// Auto generated" + NL);
  234. testFiles.append("package " + OUTPUT_PACKAGE + ";" + NL + NL);
  235. testFiles.append("public class TestGet {" + NL + NL);
  236. testFiles.append(
  237. "\tpublic static void main(String args[]) {" + NL);
  238. testFiles.append("\t\tC0 tester = new C0();" + NL);
  239. testFiles.append("\t\tint i = tester.i0;" + NL);
  240. testFiles.append("\t}" + NL);
  241. testFiles.append("}" + NL);
  242. File f = new File(outputDir, "TestGet.java");
  243. FileOutputStream fos = new FileOutputStream(f);
  244. BufferedOutputStream bos = new BufferedOutputStream(fos);
  245. bos.write(new String(testFiles).getBytes());
  246. bos.close();
  247. fos.close();
  248. } catch (IOException e) {
  249. e.printStackTrace();
  250. System.exit(-1);
  251. }
  252. testFiles = new StringBuilder(1000);
  253. try {
  254. testFiles.append("// Auto generated" + NL);
  255. testFiles.append("package " + OUTPUT_PACKAGE + ";" + NL + NL);
  256. testFiles.append("public class TestExecution {" + NL + NL);
  257. testFiles.append(
  258. "\tpublic static void main(String args[]) {" + NL);
  259. testFiles.append("\t\tC0 tester = new C0();" + NL);
  260. testFiles.append("\t\ttester.m0(tester);" + NL);
  261. testFiles.append("\t}" + NL);
  262. testFiles.append("}" + NL);
  263. File f = new File(outputDir, "TestExecution.java");
  264. FileOutputStream fos = new FileOutputStream(f);
  265. BufferedOutputStream bos = new BufferedOutputStream(fos);
  266. bos.write(new String(testFiles).getBytes());
  267. bos.close();
  268. fos.close();
  269. } catch (IOException e) {
  270. e.printStackTrace();
  271. System.exit(-1);
  272. }
  273. }
  274. StringBuilder buildList = new StringBuilder(100);
  275. for(int i = 0; i < NUMCLASSES; i++)
  276. buildList.append("C" + i + ".java" + NL);
  277. if (INCLUDE_TEST_CLASSES) {
  278. buildList.append("TestGet.java" + NL);
  279. buildList.append("TestExecution.java" + NL);
  280. }
  281. try {
  282. File f = new File(outputDir, "build.lst");
  283. FileOutputStream fos = new FileOutputStream(f);
  284. BufferedOutputStream bos = new BufferedOutputStream(fos);
  285. bos.write(new String(buildList).getBytes());
  286. bos.close();
  287. fos.close();
  288. } catch (IOException e) {
  289. e.printStackTrace();
  290. System.exit(-1);
  291. }
  292. }
  293. /**
  294. * @param adviceType Either "get" or "execution" depending on which test.
  295. *
  296. * Will create an aspect such as the following:
  297. *
  298. * public aspect GetAdviceWeaveOne {
  299. *
  300. * before() : get(int output.C0.getter) {
  301. * System.out.println("In the aspect");
  302. * }
  303. * }
  304. */
  305. public static void createAspects(String adviceType) {
  306. adviceType = adviceType.toLowerCase();
  307. if((!adviceType.equals("get")) && (!adviceType.equals("execution"))) {
  308. System.err.println("Only get and execution advice is supported");
  309. System.exit(-1);
  310. }
  311. if(ECHO) System.out.println("Creating aspects");
  312. if(ALL_POINTCUT_TESTS || TEST_ONE)
  313. createAllAspects(adviceType, WEAVE_FAST);
  314. if(ALL_POINTCUT_TESTS || TEST_ONE_SEARCH_ALL)
  315. createAllAspects(adviceType, WEAVE_MED);
  316. if(ALL_POINTCUT_TESTS || TEST_ALL)
  317. createAllAspects(adviceType, WEAVE_SLOW);
  318. }
  319. private static void createAllAspects(String adviceType, int pointcut) {
  320. StringBuilder aspectFile = new StringBuilder(1000);
  321. // Capitalises the first char in the adviceType String, and then adds "Advice" to it.
  322. String adviceName = (char)(adviceType.charAt(0) - 32) + adviceType.substring(1) + "Advice";
  323. switch(pointcut) {
  324. case WEAVE_FAST:
  325. adviceName += "WeaveFast";
  326. break;
  327. case WEAVE_MED:
  328. adviceName += "WeaveMedium";
  329. break;
  330. case WEAVE_SLOW:
  331. adviceName += "WeaveSlow";
  332. break;
  333. }
  334. aspectFile.append("// Auto-generated" + NL + NL);
  335. aspectFile.append("public aspect " + adviceName + " {" + NL + NL);
  336. aspectFile.append("\tbefore() : ");
  337. if(adviceType.equals("execution")) {
  338. switch(pointcut) {
  339. case WEAVE_FAST:
  340. aspectFile.append(EXECUTION_FAST);
  341. break;
  342. case WEAVE_MED:
  343. aspectFile.append(EXECUTION_MED);
  344. break;
  345. case WEAVE_SLOW:
  346. aspectFile.append(EXECUTION_SLOW);
  347. break;
  348. }
  349. }
  350. else {
  351. switch(pointcut) {
  352. case WEAVE_FAST:
  353. aspectFile.append(GET_FAST);
  354. break;
  355. case WEAVE_MED:
  356. aspectFile.append(GET_MED);
  357. break;
  358. case WEAVE_SLOW:
  359. aspectFile.append(GET_SLOW);
  360. break;
  361. }
  362. }
  363. aspectFile.append(" {" + NL);
  364. aspectFile.append("\t\tSystem.out.println(\"In the aspect\");" + NL);
  365. aspectFile.append("\t}" + NL);
  366. aspectFile.append("}" + NL);
  367. // Create the file
  368. try {
  369. File f = new File(outputDir, (adviceName + ".aj"));
  370. FileOutputStream fos = new FileOutputStream(f);
  371. BufferedOutputStream bos = new BufferedOutputStream(fos);
  372. bos.write(new String(aspectFile).getBytes());
  373. bos.close();
  374. fos.close();
  375. } catch (IOException e) {
  376. e.printStackTrace();
  377. System.exit(-1);
  378. }
  379. }
  380. public static void compileClasses() throws IOException {
  381. if(ECHO) System.out.print("Compiling: ");
  382. long average = 0;
  383. for (int i = 0; i < ITERATIONS; i++) {
  384. long time = performCompile();
  385. compileTimes[i] = time;
  386. average += time;
  387. }
  388. if(ECHO) System.out.println((average / ITERATIONS) + " millis");
  389. }
  390. private static long performCompile() throws IOException {
  391. String ajcargs =
  392. "-noExit -outjar " + OUTPUT_PACKAGE + File.separatorChar + "classes.jar " +
  393. "-argfile " + OUTPUT_PACKAGE + File.separatorChar + "build.lst";
  394. // split method creates a String array delimited on a space
  395. String[] parsedArgs = RunWeaveTests.split(ajcargs);
  396. long start = System.currentTimeMillis();
  397. org.aspectj.tools.ajc.Main.main(parsedArgs);
  398. long stop = System.currentTimeMillis();
  399. return stop - start;
  400. }
  401. public static void weaveAspects(String adviceType) throws IOException {
  402. adviceType = adviceType.toLowerCase();
  403. if((!adviceType.equals("get")) && (!adviceType.equals("execution"))) {
  404. System.err.println("Only get and execution advice is supported");
  405. System.exit(-1);
  406. }
  407. long average = 0;
  408. if(ECHO) System.out.println((char)(adviceType.charAt(0) - 32) + adviceType.substring(1));
  409. if (ALL_POINTCUT_TESTS || TEST_ONE) {
  410. if(ECHO) System.out.print("Weave Fast:");
  411. for (int i = 0; i < ITERATIONS; i++) {
  412. long time = weaveAllAspects(adviceType, WEAVE_FAST);
  413. if(adviceType.equals("execution")) executionFastTimes[i] = time;
  414. else getFastTimes[i] = time;
  415. average += time;
  416. if(ECHO) System.out.print(".");
  417. }
  418. if(ECHO) System.out.println(" " + (average / ITERATIONS) + " millis");
  419. }
  420. average = 0;
  421. if (ALL_POINTCUT_TESTS || TEST_ONE_SEARCH_ALL) {
  422. if(ECHO) System.out.print("Weave Medium:");
  423. for (int i = 0; i < ITERATIONS; i++) {
  424. long time = weaveAllAspects(adviceType, WEAVE_MED);
  425. if(adviceType.equals("execution")) executionMedTimes[i] = time;
  426. else getMedTimes[i] = time;
  427. average += time;
  428. if(ECHO) System.out.print(".");
  429. }
  430. if(ECHO) System.out.println(" " + (average / ITERATIONS) + " millis");
  431. }
  432. average = 0;
  433. if (ALL_POINTCUT_TESTS || TEST_ALL) {
  434. if(ECHO) System.out.print("Weave Slow:");
  435. for (int i = 0; i < ITERATIONS; i++) {
  436. long time = weaveAllAspects(adviceType, WEAVE_SLOW);
  437. if(adviceType.equals("execution")) executionSlowTimes[i] = time;
  438. else getSlowTimes[i] = time;
  439. average += time;
  440. if(ECHO) System.out.print(".");
  441. }
  442. if(ECHO) System.out.println(" " + (average / ITERATIONS) + " millis");
  443. }
  444. if(ECHO) System.out.println();
  445. }
  446. private static long weaveAllAspects(String adviceType, int pointcut) throws IOException {
  447. // Capitalises the first char in the adviceType String, to keep to Java naming convention
  448. String adviceName = (char)(adviceType.charAt(0) - 32) + adviceType.substring(1) + "Advice";
  449. switch(pointcut) {
  450. case WEAVE_FAST:
  451. adviceName += "WeaveFast";
  452. break;
  453. case WEAVE_MED:
  454. adviceName += "WeaveMedium";
  455. break;
  456. case WEAVE_SLOW:
  457. adviceName += "WeaveSlow";
  458. break;
  459. }
  460. String ajcargs =
  461. "-noExit -injars " + OUTPUT_PACKAGE + File.separatorChar + "classes.jar " +
  462. "-outjar " + OUTPUT_PACKAGE + File.separatorChar + adviceName + ".jar " +
  463. OUTPUT_PACKAGE + File.separatorChar + adviceName + ".aj";
  464. String[] parsedArgs = RunWeaveTests.split(ajcargs);
  465. long start = System.currentTimeMillis();
  466. org.aspectj.tools.ajc.Main.main(parsedArgs);
  467. long stop = System.currentTimeMillis();
  468. return stop - start;
  469. }
  470. }