|
2 | 2 |
|
3 | 3 | public class Main { |
4 | 4 | public static void main(String[] args) { |
5 | | - SynchroStack stack = new SynchroStack(); |
6 | | - Utils.initCSV(); |
| 5 | + Class<?>[] classes = { // classes to benchmark |
| 6 | + Stack.class, |
| 7 | + SynchroStack.class, |
| 8 | + SynchroStackFast.class |
| 9 | + }; |
7 | 10 |
|
8 | | - for (int i = 0; i < 100; i++) { |
9 | | - stack.push(i); |
| 11 | + int threadsPerMethod = 2; // threads per method |
| 12 | + |
| 13 | + // create stacks of each class |
| 14 | + Stack[] stacks = new Stack[classes.length]; |
| 15 | + for (int i = 0; i < classes.length; i++) { |
| 16 | + try { |
| 17 | + stacks[i] = (Stack) classes[i].getConstructor().newInstance(); |
| 18 | + // fill stacks with 200 elements |
| 19 | + for (int j = 0; j < 200; j++) { |
| 20 | + stacks[i].push(j); |
| 21 | + } |
| 22 | + } catch (Exception e) { |
| 23 | + e.printStackTrace(); |
| 24 | + } |
10 | 25 | } |
11 | 26 |
|
12 | | - Thread[] threads = new Thread[4]; |
| 27 | + // print type of stacks |
| 28 | + for (Stack stack : stacks) { |
| 29 | + Runnable[] methods = { |
| 30 | + () -> { // push (write operation) |
| 31 | + for (int i = 0; i < 1000; i++) { |
| 32 | + stack.push(i); |
| 33 | + } |
| 34 | + }, |
| 35 | + () -> { // pop (write operation) |
| 36 | + for (int i = 0; i < 1000; i++) { |
| 37 | + stack.pop(); |
| 38 | + } |
| 39 | + }, |
| 40 | + () -> { // equals (read operation) |
| 41 | + for (int i = 0; i < 10000; i++) { |
| 42 | + stack.equals(stack); |
| 43 | + } |
| 44 | + }, |
| 45 | + () -> { // toString (read operation) |
| 46 | + for (int i = 0; i < 100; i++) { |
| 47 | + stack.toString(); |
| 48 | + } |
| 49 | + } |
| 50 | + }; |
13 | 51 |
|
14 | | - for (int i = 0; i < 2; i++) { |
15 | | - threads[i * 2] = new Thread(() -> { |
16 | | - Utils.printRow("START - Stack 10 pushes"); |
17 | | - for (int j = 0; j < 10; j++) { |
18 | | - stack.push(j); |
| 52 | + |
| 53 | + |
| 54 | + // create threads |
| 55 | + Thread[] threads = new Thread[methods.length * threadsPerMethod]; |
| 56 | + for (int i = 0; i < methods.length; i++) { |
| 57 | + for (int j = 0; j < threadsPerMethod; j++) { |
| 58 | + int index = i * threadsPerMethod + j; |
| 59 | + threads[index] = new Thread(methods[i]); |
| 60 | + threads[index].setName("Thread-" + index); |
19 | 61 | } |
20 | | - Utils.printRow("END - Stack 10 pushes"); |
21 | | - }); |
| 62 | + } |
| 63 | + |
| 64 | + System.out.print("Benchmarking " + stack.getClass().getSimpleName()); |
22 | 65 |
|
23 | | - threads[i * 2 + 1] = new Thread(() -> { |
24 | | - Utils.printRow("START - Stack 10 pops"); |
25 | | - for (int j = 0; j < 10; j++) { |
26 | | - stack.pop(); |
27 | | - } |
28 | | - Utils.printRow("END - Stack 10 pops"); |
29 | | - }); |
30 | | - } |
| 66 | + // start threads and measure time |
| 67 | + long start = System.currentTimeMillis(); |
31 | 68 |
|
32 | | - for (int i = 0; i < threads.length; i++) { |
33 | | - threads[i].start(); |
34 | | - } |
| 69 | + for (Thread thread : threads) { |
| 70 | + thread.start(); |
| 71 | + } |
35 | 72 |
|
36 | | - for (int i = 0; i < threads.length; i++) { |
37 | | - try { |
38 | | - threads[i].join(); |
39 | | - Utils.printRow("END - Thread " + i + " join"); |
40 | | - } catch (InterruptedException e) { |
41 | | - e.printStackTrace(); |
| 73 | + for (Thread thread : threads) { |
| 74 | + try { |
| 75 | + thread.join(); |
| 76 | + } catch (InterruptedException e) { |
| 77 | + e.printStackTrace(); |
| 78 | + } |
42 | 79 | } |
| 80 | + |
| 81 | + long end = System.currentTimeMillis(); |
| 82 | + |
| 83 | + System.out.println(" - took " + (end - start) + "ms"); |
43 | 84 | } |
44 | 85 | } |
45 | 86 | } |
0 commit comments