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.

WeaveTests.java 17KB

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