-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMppRunner.java
More file actions
86 lines (72 loc) · 3.45 KB
/
Copy pathMppRunner.java
File metadata and controls
86 lines (72 loc) · 3.45 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.*;
import java.util.concurrent.*;
class MppRunner {
private static final Random random = new Random();
private static void demoLocalOperations(MppRunnerCode hybridObj) {
for (int i = 0; i < 5; i++) {
String key = "user_" + (random.nextInt(3) + 1);
if (random.nextBoolean()) {
int val = random.nextInt(900) + 100;
hybridObj.localWrite(key, val);
} else {
hybridObj.localRead(key);
}
try { Thread.sleep((long) (random.nextDouble() * 100)); } catch (InterruptedException ignored) {}
}
}
private static void demoCriticalOperation(MppRunnerCode hybridObj) {
for (int i = 0; i < 5; i++) {
hybridObj.criticalUpdate(1);
try { Thread.sleep((long) (random.nextDouble() * 100)); } catch (InterruptedException ignored) {}
}
}
private static void demoHybridOperation(MppRunnerCode hybridObj, int threadId) {
for (int i = 0; i < 5; i++) {
String key = "resource_" + threadId;
int val = random.nextInt(1001) + 1000;
hybridObj.hybridOperation(key, val);
try { Thread.sleep((long) (random.nextDouble() * 100)); } catch (InterruptedException ignored) {}
}
}
public static void runTest(int numThreads, MppRunnerCode hybridObj) {
List<Thread> threads = new ArrayList<>();
int partitionSize = numThreads / 3;
for (int i = 0; i < partitionSize; i++) {
final int index = i; // Capture the index for each thread
threads.add(new Thread(() -> demoLocalOperations(hybridObj)));
}
for (int i = 0; i < partitionSize; i++) {
final int index = i; // Capture the index for each thread
threads.add(new Thread(() -> demoCriticalOperation(hybridObj)));
}
for (int i = 0; i < partitionSize; i++) {
final int index = i; // Capture the index for each thread
threads.add(new Thread(() -> demoHybridOperation(hybridObj, index)));
}
while (threads.size() < numThreads) {
final int index = threads.size(); // Use the current thread size as the index
threads.add(new Thread(() -> demoHybridOperation(hybridObj, index)));
}
long startTime = System.currentTimeMillis();
threads.forEach(Thread::start);
threads.forEach(t -> {
try { t.join(); } catch (InterruptedException ignored) {}
});
long endTime = System.currentTimeMillis();
int totalLocalSum = hybridObj.getTotalLocalSum();
int difference = hybridObj.getGlobalCounter() - totalLocalSum;
System.out.println("Test with " + numThreads + " threads completed in " + (endTime - startTime) + " ms");
System.out.println("Final Global Counter = " + hybridObj.getGlobalCounter());
System.out.println("Total Local Sum = " + totalLocalSum);
System.out.println("Difference = " + difference);
hybridObj.printStats();
System.out.println("----------------------------------------");
}
public static void main(String[] args) {
int[] threadCounts = {1, 2, 4, 8, 16, 32, 64};
for (int count : threadCounts) {
MppRunnerCode hybridObj = new MppRunnerCode(4); // Fixed instantiation
runTest(count, hybridObj);
}
}
}