Skip to content

Commit b59e801

Browse files
committed
add benchmarks util for sync primitives
1 parent 16ee38d commit b59e801

File tree

4 files changed

+248
-85
lines changed

4 files changed

+248
-85
lines changed

.gitignore

+54-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,54 @@
1-
# compiled classes
2-
*.class
3-
# vm crash logs
4-
hs_err_pid*
1+
# Credit to: erayaraz10@
2+
# https://medium.com/@erayaraz10/gitignore-file-examples-and-description-d2fecbd5697
3+
4+
# Ignore all .log files
5+
*.log
6+
7+
# Ignore the local.properties file
8+
local.properties
9+
10+
# Ignore all files in the bin and config directories
11+
bin/
12+
config/
13+
14+
# Ignore all .impex and .hmc.xml files
15+
*.impex
16+
*.hmc.xml
17+
18+
# Ignore the .idea directory created by IntelliJ IDEA
19+
.idea/
20+
21+
# Ignore all .class files generated by the build process
22+
**/*.class
23+
24+
*.classpath
25+
**/*.iml
26+
rebel.xml
27+
build.xml
28+
Generated*.java
29+
platformhome.properties
30+
*testclasses.xml
31+
extensioninfo.xsd
32+
*hmc.jar
33+
hmc.xsd
34+
items.xsd
35+
beans.xsd
36+
ruleset.xml
37+
base.properties
38+
39+
# Addon specific copy folders
40+
**/_ui/addons
41+
**/views/addons
42+
**/tld/addons
43+
**/tags/addons
44+
**/messages/addons
45+
*_bof.jar
46+
**/web/commonwebsrc
47+
**/.settings/*.prefs
48+
!**/.settings/org.eclipse.core.resources.prefs
49+
!**/.settings/org.eclipse.jdt.core.prefs
50+
!**/.settings/org.eclipse.jdt.ui.prefs
51+
**/.idea/*
52+
**/eclipsebin/*
53+
**/.DS_Store
54+
/**/node_modules/

Concurrency/SharedState.java

-81
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import java.util.concurrent.ExecutorService;
2+
import java.util.concurrent.Executors;
3+
import java.util.concurrent.locks.ReadWriteLock;
4+
import java.util.concurrent.locks.ReentrantReadWriteLock;
5+
6+
public class SharedStateMultiReader {
7+
// shared resource
8+
private volatile int data;
9+
// lock for the resource
10+
ReadWriteLock rwLock = new ReentrantReadWriteLock();
11+
12+
private static final int READ_CYCLES = (int)1e6;
13+
private static final int WRITE_CYCLES = (int)1e2;
14+
private static final int READER_THREADS = 100; // read heavy systems
15+
private static final int WRITER_THREADS = 10;
16+
17+
public SharedStateMultiReader(int initialData) {
18+
this.data = initialData;
19+
}
20+
21+
/**
22+
* Retrieves the data from a private static variable.
23+
* Representing a shared resource
24+
*
25+
* @return The data value stored
26+
*/
27+
private int getData() {
28+
rwLock.readLock().lock();
29+
int value = 0;
30+
try {
31+
value = data;
32+
} finally {
33+
rwLock.readLock().unlock();
34+
}
35+
return value;
36+
}
37+
38+
/**
39+
* Updates the value of the private static variable 'data'.
40+
*/
41+
private void updateData() {
42+
rwLock.writeLock().lock();
43+
try {
44+
data += 1;
45+
} finally {
46+
rwLock.writeLock().unlock();
47+
}
48+
}
49+
50+
public static void main(String ...args) throws InterruptedException {
51+
final long startTime = System.nanoTime();
52+
SharedStateMultiReader sharedState = new SharedStateMultiReader(0);
53+
Thread []readers = new Thread[READER_THREADS];
54+
for(int readerId = 0; readerId < READER_THREADS; readerId++){
55+
final int threadId = readerId;
56+
readers[threadId] = new Thread(new Runnable() {
57+
@Override
58+
public void run() {
59+
for(int cycles = 0; cycles < READ_CYCLES; cycles++) {
60+
int value = sharedState.getData();
61+
// to keep I/O low to influence perf
62+
if(cycles % (READ_CYCLES/10) == 0){
63+
// System.out.format("read threadId: %d read_value: %d\n", threadId, value);
64+
// System.out.flush();
65+
}
66+
}
67+
System.out.format("\nread threadId: %d finished", threadId);
68+
System.out.flush();
69+
}
70+
});
71+
readers[threadId].start();
72+
}
73+
Thread []writers = new Thread[WRITER_THREADS];
74+
for(int writerId = 0; writerId < WRITER_THREADS; writerId++){
75+
final int threadId = writerId;
76+
writers[threadId] = new Thread(new Runnable() {
77+
@Override
78+
public void run() {
79+
for(int cycles = 0; cycles < WRITE_CYCLES; cycles++) {
80+
sharedState.updateData();
81+
if(cycles % (WRITE_CYCLES/10) == 0){
82+
// int value = sharedState.getData();
83+
// System.out.format("write threadId: %d write_value: %d\n", threadId, value);
84+
// System.out.flush();
85+
}
86+
}
87+
System.out.format("\nwrite threadId: %d finished", threadId);
88+
System.out.flush();
89+
}
90+
});
91+
writers[threadId].start();
92+
}
93+
94+
for(int readerId = 0; readerId < READER_THREADS; readerId++){
95+
readers[readerId].join();
96+
}
97+
for(int writerId = 0; writerId < WRITER_THREADS; writerId++){
98+
writers[writerId].join();
99+
}
100+
final long duration = System.nanoTime() - startTime;
101+
System.out.println("SharedStateMultiReader time taken(sec): " + duration/(1e9));
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
public class SharedStateReaderWriter {
2+
// shared resource
3+
private volatile int data;
4+
// lock for the resource
5+
Object rwLock = new Object();
6+
7+
private static final int READ_CYCLES = (int)1e6;
8+
private static final int WRITE_CYCLES = (int)1e2;
9+
private static final int READER_THREADS = 100; // read heavy systems
10+
private static final int WRITER_THREADS = 10;
11+
12+
public SharedStateReaderWriter(int initialData) {
13+
this.data = initialData;
14+
}
15+
16+
/**
17+
* Retrieves the data from a private static variable.
18+
* Representing a shared resource
19+
*
20+
* @return The data value stored
21+
*/
22+
private int getData() {
23+
int result = 0;
24+
synchronized(rwLock){
25+
result = data;
26+
}
27+
return result;
28+
}
29+
30+
/**
31+
* Updates the value of the private static variable 'data'.
32+
*/
33+
private void updateData() {
34+
synchronized(rwLock){
35+
data += 1;
36+
}
37+
}
38+
39+
public static void main(String ...args) throws InterruptedException {
40+
final long startTime = System.nanoTime();
41+
SharedStateReaderWriter sharedState = new SharedStateReaderWriter(0);
42+
Thread []readers = new Thread[READER_THREADS];
43+
for(int readerId = 0; readerId < READER_THREADS; readerId++){
44+
final int threadId = readerId;
45+
readers[threadId] = new Thread(new Runnable() {
46+
@Override
47+
public void run() {
48+
for(int cycles = 0; cycles < READ_CYCLES; cycles++) {
49+
int value = sharedState.getData();
50+
// to keep I/O low to influence perf
51+
if(cycles % (READ_CYCLES/10) == 0){
52+
// System.out.format("read threadId: %d read_value: %d\n", threadId, value);
53+
// System.out.flush();
54+
}
55+
}
56+
System.out.format("\nread threadId: %d finished", threadId);
57+
System.out.flush();
58+
}
59+
});
60+
readers[threadId].start();
61+
}
62+
Thread []writers = new Thread[WRITER_THREADS];
63+
for(int writerId = 0; writerId < WRITER_THREADS; writerId++){
64+
final int threadId = writerId;
65+
writers[threadId] = new Thread(new Runnable() {
66+
@Override
67+
public void run() {
68+
for(int cycles = 0; cycles < WRITE_CYCLES; cycles++) {
69+
sharedState.updateData();
70+
if(cycles % (WRITE_CYCLES/10) == 0){
71+
int value = sharedState.getData();
72+
// System.out.format("write threadId: %d write_value: %d\n", threadId, value);
73+
// System.out.flush();
74+
}
75+
}
76+
System.out.format("\nwrite threadId: %d finished", threadId);
77+
System.out.flush();
78+
}
79+
});
80+
writers[threadId].start();
81+
}
82+
for(int readerId = 0; readerId < READER_THREADS; readerId++){
83+
readers[readerId].join();
84+
}
85+
for(int writerId = 0; writerId < WRITER_THREADS; writerId++){
86+
writers[writerId].join();
87+
}
88+
final long duration = System.nanoTime() - startTime;
89+
System.out.println("SharedStateReaderWriter time taken(sec): " + duration/(1e9));
90+
}
91+
}

0 commit comments

Comments
 (0)