1 package ch.hslu.exercises.sw11.ex1.mergesort;
2
3 import org.junit.jupiter.api.Disabled;
4 import org.junit.jupiter.api.Test;
5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory;
7
8 import java.time.Duration;
9 import java.time.Instant;
10 import java.util.Arrays;
11 import java.util.Random;
12 import java.util.stream.IntStream;
13
14 import static org.junit.jupiter.api.Assertions.*;
15
16 class MergesortTaskTest {
17 private final int[] testArray = new int[15_000_000];
18 private static final Logger LOG = LoggerFactory.getLogger(MergesortTaskTest.class);
19
20 @Test
21 @Disabled
22 void testMergeSortLogic() {
23 int[] array = new int[1000];
24 Random random = new Random(0);
25 for (int i = 0; i < array.length; i++) {
26 array[i] = random.nextInt();
27 }
28 int[] sorted = new int[1000];
29 random = new Random(0);
30 for (int i = 0; i < sorted.length; i++) {
31 sorted[i] = random.nextInt();
32 }
33 Arrays.parallelSort(sorted);
34
35 MergesortTask mergesortTask = new MergesortTask(array);
36 mergesortTask.invoke();
37
38 assertArrayEquals(sorted, array);
39 }
40
41 @Test
42 @Disabled
43 void testMergeSortPerformance() {
44
45 initTestArray();
46 MergesortTask mergesortTask = new MergesortTask(testArray, 50);
47 mergesortTask.invoke();
48
49 IntStream.of(testArray.length / Runtime.getRuntime().availableProcessors()).forEach(this::testMergesortTaskwithThreshold);
50
51 testMergesortRecursive();
52
53 }
54
55 private void testMergesortRecursive() {
56 long start;
57 long stop;
58
59 initTestArray();
60 start = System.currentTimeMillis();
61 MergesortRecursive.mergeSort(testArray);
62 stop = System.currentTimeMillis();
63 Duration duration1 = Duration.ofMillis(stop - start);
64
65 initTestArray();
66 start = System.currentTimeMillis();
67 MergesortRecursive.mergeSort(testArray);
68 stop = System.currentTimeMillis();
69 Duration duration2 = Duration.ofMillis(stop - start);
70
71 initTestArray();
72 start = System.currentTimeMillis();
73 MergesortRecursive.mergeSort(testArray);
74 stop = System.currentTimeMillis();
75 Duration duration3 = Duration.ofMillis(stop - start);
76
77 Duration averageDuration = duration1.plus(duration2).plus(duration3).dividedBy(3);
78
79 LOG.info("Duration Mergesort Recursive : {} msec.", averageDuration.toMillis());
80 }
81
82 private void testMergesortTaskwithThreshold(int threshold) {
83 Instant start;
84 initTestArray();
85 start = Instant.now();
86 executeMergeSortTask(threshold);
87 Duration duration1 = Duration.between(start, Instant.now());
88
89 initTestArray();
90 start = Instant.now();
91 executeMergeSortTask(threshold);
92 Duration duration2 = Duration.between(start, Instant.now());
93
94 initTestArray();
95 start = Instant.now();
96 executeMergeSortTask(threshold);
97 Duration duration3 = Duration.between(start, Instant.now());
98
99 Duration averageDuration = duration1.plus(duration2).plus(duration3).dividedBy(3);
100
101 LOG.info("Duration with THRESHOLD = {}: {} msec.", threshold, averageDuration.toMillis());
102 }
103
104 private void executeMergeSortTask(int threshold) {
105 MergesortTask mergesortTask = new MergesortTask(testArray, threshold);
106 mergesortTask.invoke();
107 }
108
109 private void initTestArray() {
110 Random random = new Random(0);
111 for (int i = 0; i < testArray.length; i++) {
112 testArray[i] = random.nextInt();
113 }
114 }
115
116 }