1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.perfidix.element;
28
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
32
33 import java.lang.annotation.ElementType;
34 import java.lang.annotation.Retention;
35 import java.lang.annotation.RetentionPolicy;
36 import java.lang.annotation.Target;
37 import java.lang.reflect.Method;
38
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.perfidix.annotation.AfterEachRun;
42 import org.perfidix.annotation.AfterLastRun;
43 import org.perfidix.annotation.BeforeEachRun;
44 import org.perfidix.annotation.BeforeFirstRun;
45 import org.perfidix.annotation.Bench;
46 import org.perfidix.annotation.BenchClass;
47 import org.perfidix.annotation.SkipBench;
48 import org.perfidix.exceptions.PerfidixMethodCheckException;
49 import static org.junit.Assert.assertFalse;
50
51
52
53
54
55
56
57 public class BenchmarkMethodTest {
58
59 private transient Object toTest;
60
61
62
63
64
65
66
67 @Before
68 public void setUp() throws Exception {
69
70
71 toTest = new TestClassCheckThisMethodAsBenchmarkable1();
72 }
73
74
75
76
77 @Test
78 public void testCheckThisMethodAsBenchmarkable1() {
79 final Object[] param = {};
80 try {
81
82 final Method[] meths = toTest.getClass().getDeclaredMethods();
83 int numOfMethods = 0;
84 for (final Method meth : meths) {
85 if (BenchmarkMethod.isBenchmarkable(meth)) {
86 final BenchmarkMethod elem = new BenchmarkMethod(meth);
87 elem.getMethodToBench().invoke(toTest, param);
88 numOfMethods++;
89 }
90 }
91 assertEquals("Number of methods should be 1", numOfMethods, 1);
92
93 } catch (final Exception e) {
94 fail(e.toString());
95 }
96 }
97
98
99
100
101 @Test
102 public void testCheckThisMethodAsBenchmarkable2() {
103
104 try {
105 final Object[] param = {};
106
107
108 toTest = new TestClassCheckThisMethodAsBenchmarkable2();
109 final Method[] meths = toTest.getClass().getDeclaredMethods();
110 int numOfMethods = 0;
111 for (final Method meth : meths) {
112 if (BenchmarkMethod.isBenchmarkable(meth)) {
113 final BenchmarkMethod elem = new BenchmarkMethod(meth);
114 elem.getMethodToBench().invoke(toTest, param);
115 numOfMethods++;
116 }
117 }
118 assertEquals("Number of methods should be 0", numOfMethods, 0);
119
120 } catch (final Exception e) {
121 fail(e.toString());
122 }
123 }
124
125
126
127
128
129
130
131
132
133 @Test(expected = PerfidixMethodCheckException.class)
134 public void testFindAndCheckAnyMethodByAnnotation() throws PerfidixMethodCheckException {
135 toTest = new TestFindAndCheckBenchClass();
136 BenchmarkMethod.findAndCheckAnyMethodByAnnotation(toTest.getClass(), ShouldOccureOnce.class);
137 }
138
139
140
141
142
143 @Test
144 public void testIsReflectedExecutable() {
145 try {
146 final Object[] param = {};
147 toTest = new TestIsReflectedExecutable();
148 final Method[] meths = toTest.getClass().getDeclaredMethods();
149 int numOfMethods = 0;
150 for (final Method meth : meths) {
151 if (BenchmarkMethod.isReflectedExecutable(meth, Bench.class)) {
152 meth.invoke(toTest, param);
153 numOfMethods++;
154 }
155 }
156 assertEquals("Number of methods should be 1", numOfMethods, 1);
157 } catch (final Exception e) {
158 fail(e.toString());
159 }
160 }
161
162
163
164
165 @Test
166 public void testFindBeforeFirstRun1() {
167 try {
168 final Object[] param = {};
169
170
171 toTest = new TestBeforeFirstRun1();
172 final Method[] meths = toTest.getClass().getDeclaredMethods();
173 BenchmarkMethod elem = null;
174 for (final Method meth : meths) {
175 if (BenchmarkMethod.isBenchmarkable(meth)) {
176 final BenchmarkMethod checkElem = new BenchmarkMethod(meth);
177
178 if (elem == null) {
179 elem = checkElem;
180 } else {
181 fail("More than one elem in TestBeforeFirstRun1 found!");
182 }
183 }
184 }
185
186 final Method[] returnedMeths = elem.findBeforeFirstRun();
187 for (Method meth : returnedMeths) {
188 meth.invoke(toTest, param);
189 }
190
191 } catch (final Exception e) {
192 fail(e.toString());
193 }
194 }
195
196
197
198
199 @Test
200 public void testFindBeforeFirstRun2() {
201 try {
202 final Object[] param = {};
203
204
205 toTest = new TestBeforeFirstRun2();
206 final Method[] meths = toTest.getClass().getDeclaredMethods();
207 BenchmarkMethod elem = null;
208 for (final Method meth : meths) {
209 if (BenchmarkMethod.isBenchmarkable(meth)) {
210 final BenchmarkMethod checkElem = new BenchmarkMethod(meth);
211
212 if (elem == null) {
213 elem = checkElem;
214 } else {
215 fail("More than one elem in TestBeforeFirstRun2 found!");
216 }
217 }
218 }
219
220 final Method[] returnedMeths = elem.findBeforeFirstRun();
221 for (Method meth : returnedMeths) {
222 meth.invoke(toTest, param);
223 }
224
225 } catch (final Exception e) {
226 fail(e.toString());
227 }
228 }
229
230
231
232
233 @Test
234 public void testFindBeforeEachRun1() {
235 try {
236 final Object[] param = {};
237
238
239 toTest = new TestBeforeEachRun1();
240 final Method[] meths = toTest.getClass().getDeclaredMethods();
241 BenchmarkMethod elem = null;
242 for (final Method meth : meths) {
243 if (BenchmarkMethod.isBenchmarkable(meth)) {
244 final BenchmarkMethod checkElem = new BenchmarkMethod(meth);
245
246 if (elem == null) {
247 elem = checkElem;
248 } else {
249 fail("More than one elem in TestBeforeEachRun1 found!");
250 }
251 }
252 }
253
254 final Method[] returnedMeths = elem.findBeforeEachRun();
255 for (Method meth : returnedMeths) {
256 meth.invoke(toTest, param);
257 }
258
259 } catch (final Exception e) {
260 fail(e.toString());
261 }
262 }
263
264
265
266
267 @Test
268 public void testFindBeforeEachRun2() {
269 try {
270 final Object[] param = {};
271
272
273 toTest = new TestBeforeEachRun2();
274 final Method[] meths = toTest.getClass().getDeclaredMethods();
275 BenchmarkMethod elem = null;
276 for (final Method meth : meths) {
277 if (BenchmarkMethod.isBenchmarkable(meth)) {
278 final BenchmarkMethod checkElem = new BenchmarkMethod(meth);
279
280 if (elem == null) {
281 elem = checkElem;
282 } else {
283 fail("More than one elem in TestBeforeFirstRun2 found!");
284 }
285 }
286 }
287
288 final Method[] returnedMeths = elem.findBeforeEachRun();
289 for (Method meth : returnedMeths) {
290 meth.invoke(toTest, param);
291 }
292
293 } catch (final Exception e) {
294 fail(e.toString());
295 }
296 }
297
298
299
300
301 @Test
302 public void testFindAfterEachRun1() {
303 try {
304 final Object[] param = {};
305
306
307 toTest = new TestAfterEachRun1();
308 final Method[] meths = toTest.getClass().getDeclaredMethods();
309 BenchmarkMethod elem = null;
310 for (final Method meth : meths) {
311 if (BenchmarkMethod.isBenchmarkable(meth)) {
312 final BenchmarkMethod checkElem = new BenchmarkMethod(meth);
313
314 if (elem == null) {
315 elem = checkElem;
316 } else {
317 fail("More than one elem in TestAfterEachRun1 found!");
318 }
319 }
320 }
321
322 final Method[] returnedMeths = elem.findAfterEachRun();
323 for (Method meth : returnedMeths) {
324 meth.invoke(toTest, param);
325 }
326
327 } catch (final Exception e) {
328 fail(e.toString());
329 }
330 }
331
332
333
334
335 @Test
336 public void testFindAfterEachRun2() {
337 try {
338 final Object[] param = {};
339
340
341 toTest = new TestAfterEachRun2();
342 final Method[] meths = toTest.getClass().getDeclaredMethods();
343 BenchmarkMethod elem = null;
344 for (final Method meth : meths) {
345 if (BenchmarkMethod.isBenchmarkable(meth)) {
346 final BenchmarkMethod checkElem = new BenchmarkMethod(meth);
347
348 if (elem == null) {
349 elem = checkElem;
350 } else {
351 fail("More than one elem in TestAfterEeachRun2 found!");
352 }
353 }
354 }
355
356 final Method[] returnedMeths = elem.findAfterEachRun();
357 for (Method meth : returnedMeths) {
358 meth.invoke(toTest, param);
359 }
360
361 } catch (final Exception e) {
362 fail(e.toString());
363 }
364 }
365
366
367
368
369 @Test
370 public void testFindAfterLastRun1() {
371 try {
372 final Object[] param = {};
373
374
375 toTest = new TestAfterLastRun1();
376 final Method[] meths = toTest.getClass().getDeclaredMethods();
377 BenchmarkMethod elem = null;
378 for (final Method meth : meths) {
379 if (BenchmarkMethod.isBenchmarkable(meth)) {
380 final BenchmarkMethod checkElem = new BenchmarkMethod(meth);
381
382 if (elem == null) {
383 elem = checkElem;
384 } else {
385 fail("More than one elem in TestAfterLastRun1 found!");
386 }
387 }
388 }
389
390 final Method[] returnedMeths = elem.findAfterLastRun();
391 for (Method meth : returnedMeths) {
392 meth.invoke(toTest, param);
393 }
394
395 } catch (final Exception e) {
396 fail(e.toString());
397 }
398 }
399
400
401
402
403 @Test
404 public void testFindAfterLastRun2() {
405 try {
406 final Object[] param = {};
407
408
409 toTest = new TestAfterLastRun2();
410 final Method[] meths = toTest.getClass().getDeclaredMethods();
411 BenchmarkMethod elem = null;
412 for (final Method meth : meths) {
413 if (BenchmarkMethod.isBenchmarkable(meth)) {
414 final BenchmarkMethod checkElem = new BenchmarkMethod(meth);
415
416 if (elem == null) {
417 elem = checkElem;
418 } else {
419 fail("More than one elem in TestAfterLastRun2 found!");
420 }
421 }
422 }
423
424 final Method[] returnedMeths = elem.findAfterLastRun();
425 for (Method meth : returnedMeths) {
426 meth.invoke(toTest, param);
427 }
428
429 } catch (final Exception e) {
430 fail(e.toString());
431 }
432 }
433
434
435
436
437 @Test
438 public void testNumberOfAnnotatedRuns() {
439 try {
440 toTest = new TestNumberOfAnnotatedRuns();
441 final Method[] meths = toTest.getClass().getDeclaredMethods();
442
443 assertEquals("4 methods should be found", 4, meths.length);
444
445 for (Method meth : meths) {
446
447 if (meth.getName().equals("bench1")) {
448 assertEquals("Check of name of method1", "bench1", meth.getName());
449 assertEquals("Check of runs of method1", 10, BenchmarkMethod
450 .getNumberOfAnnotatedRuns(meth));
451 }
452 if (meth.getName().equals("bench2")) {
453 assertEquals("Check of name of method2", "bench2", meth.getName());
454 assertEquals("Check of runs of method2", 20, BenchmarkMethod
455 .getNumberOfAnnotatedRuns(meth));
456 }
457 if (meth.getName().equals("bench3")) {
458 assertEquals("Check of name of method3", "bench3", meth.getName());
459 try {
460 BenchmarkMethod.getNumberOfAnnotatedRuns(meth);
461 fail("Must throw IllegalStateException!");
462 } catch (final IllegalArgumentException e) {
463 assertTrue("Methodexception must match a pattern", e.getMessage().startsWith("Method"));
464 }
465 }
466 if (meth.getName().equals("bench4")) {
467 assertEquals("Check of name of method2", "bench4", meth.getName());
468 assertEquals("Check of runs of method4", 20, BenchmarkMethod
469 .getNumberOfAnnotatedRuns(meth));
470 }
471 }
472
473
474
475 } catch (final Exception e) {
476 fail("Should never fail in testRuns!");
477 }
478 }
479
480 @BenchClass(runs = 20)
481 class TestNumberOfAnnotatedRuns {
482
483 @Bench(runs = 10)
484 public void bench1() {
485
486 }
487
488 public void bench2() {
489
490 }
491
492 public int bench3() {
493
494 return -1;
495 }
496
497 @Bench
498 public void bench4() {
499
500 }
501
502
503 }
504
505 class TestAfterLastRun2 {
506
507 @AfterLastRun
508 public final void afterLastRun() {
509
510 }
511
512 @Bench
513 public final void bench() {
514
515 }
516 }
517
518 class TestAfterLastRun1 {
519
520 boolean order = true;
521
522 @AfterLastRun
523 public final void afterLastRunAnno() {
524 fail("Should be ignored because of designated afterLastRun");
525 }
526
527 public final void afterLastRun() {
528 assertTrue(order);
529 order = false;
530 }
531
532 public final void afterLastRun2() {
533 assertFalse(order);
534 }
535
536 @Bench(afterLastRun = "afterLastRun, afterLastRun2")
537 public final void bench() {
538
539 }
540 }
541
542 class TestAfterEachRun2 {
543
544 @AfterEachRun
545 public final void afterEachRun() {
546
547 }
548
549 @Bench
550 public final void bench() {
551
552 }
553 }
554
555 class TestAfterEachRun1 {
556
557 boolean order = true;
558
559 @AfterEachRun
560 public final void afterEachRunAnno() {
561 fail("Should be ignored because of designated after each run");
562 }
563
564 public final void afterEachRun() {
565 assertTrue(order);
566 order = false;
567 }
568
569 public final void afterEachRun2() {
570 assertFalse(order);
571 }
572
573 @Bench(afterEachRun = "afterEachRun, afterEachRun2")
574 public final void bench() {
575
576 }
577 }
578
579 class TestBeforeEachRun2 {
580
581 @BeforeEachRun
582 public final void beforeEachRun() {
583
584 }
585
586 @Bench
587 public final void bench() {
588
589 }
590 }
591
592 class TestBeforeEachRun1 {
593
594 boolean order = true;
595
596 @BeforeEachRun
597 public final void beforeEachRunAnno() {
598 fail("Should be ignored because of designated before each run!");
599 }
600
601 public final void beforeEachRun() {
602 assertTrue(order);
603 order = false;
604 }
605
606 public final void beforeEachRun2() {
607 assertFalse(order);
608 }
609
610 @Bench(beforeEachRun = "beforeEachRun, beforeEachRun2")
611 public final void bench() {
612
613 }
614 }
615
616 class TestBeforeFirstRun2 {
617
618 @BeforeFirstRun
619 public final void beforeFirstRun() {
620
621 }
622
623 @Bench
624 public final void bench() {
625
626 }
627 }
628
629 class TestBeforeFirstRun1 {
630
631 boolean order = true;
632
633 @BeforeFirstRun
634 public final void beforeFirstRunAnno() {
635 fail("Should be ignored because of designated before first anno");
636 }
637
638 public final void beforeFirstRun() {
639 assertTrue(order);
640 order = false;
641 }
642
643 public final void beforeFirstRun2() {
644 assertFalse(order);
645 }
646
647 @Bench(beforeFirstRun = "beforeFirstRun, beforeFirstRun2")
648 public final void bench() {
649
650 }
651 }
652
653 class TestIsReflectedExecutable {
654
655 public final void paramMethod(final Object obj) {
656 fail("Only param-less methods allowed");
657 }
658
659 protected final void notPublicMethod() {
660 fail("Only methods with public identifier allowed");
661 }
662
663 public final Object returnVal() {
664 fail("Only methods without a returnVal allowed");
665 return null;
666 }
667
668 public final void shouldBeInvoked() {
669
670 }
671 }
672
673 class TestFindAndCheckBenchClass {
674
675 @ShouldOccureOnce
676 public final void benchAnno1() {
677
678 }
679
680 @ShouldOccureOnce
681 public final void benchAnno2() {
682
683 }
684 }
685
686 class TestClassCheckThisMethodAsBenchmarkable1 {
687 @Bench
688 public final void benchAnno1() {
689
690 }
691
692 public final void benchAnno2() {
693 fail("Should not be benched!");
694 }
695 }
696
697 class TestClassCheckThisMethodAsBenchmarkable2 {
698
699 @SkipBench
700 @Bench
701 public final void benchAnno3() {
702 fail("Should not be benched!");
703 }
704 }
705
706 @Retention(RetentionPolicy.RUNTIME)
707 @Target(ElementType.METHOD)
708 @interface ShouldOccureOnce {
709
710 }
711 }