Commit ce0771ef authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

Test time for different Sorted Linked List Implementations

parent f7a214e2
...@@ -5,20 +5,14 @@ ...@@ -5,20 +5,14 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3f4b2924-17d7-4794-bacf-d51c4f0be8bb" name="Changes" comment="Concurrent Sorted Linked List Implementations"> <list default="true" id="3f4b2924-17d7-4794-bacf-d51c4f0be8bb" name="Changes" comment="Concurrent Sorted Linked List Implementations">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/test/java/AddThread.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/test/java/ContainThread.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/test/java/RandomSeq.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/test/java/RemoveThread.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/test/java/TestThread.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/README.MD" afterDir="false" /> <change beforePath="$PROJECT_DIR$/README.MD" beforeDir="false" afterPath="$PROJECT_DIR$/README.MD" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/test/java/SyncListTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/SyncListTest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/Entry.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/LockList.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/RWLockList.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/SortList.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/SyncList.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/SyncListTest.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
...@@ -46,18 +40,27 @@ ...@@ -46,18 +40,27 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"ASKED_ADD_EXTERNAL_FILES": "true", &quot;ASKED_ADD_EXTERNAL_FILES&quot;: &quot;true&quot;,
"RunOnceActivity.OpenProjectViewOnStart": "true", &quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
"RunOnceActivity.ShowReadmeOnStart": "true", &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
"SHARE_PROJECT_CONFIGURATION_FILES": "true", &quot;SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
"project.structure.last.edited": "Modules", &quot;project.structure.last.edited&quot;: &quot;Modules&quot;,
"project.structure.proportion": "0.0", &quot;project.structure.proportion&quot;: &quot;0.0&quot;,
"project.structure.side.proportion": "0.0" &quot;project.structure.side.proportion&quot;: &quot;0.0&quot;
} }
}]]></component> }</component>
<component name="RunManager"> <component name="RunManager" selected="JUnit.SyncListTest.testRun">
<configuration name="SyncListTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="SortLinkedList" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="SyncListTest" />
<option name="TEST_OBJECT" value="class" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="SyncListTest.testAddList" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true"> <configuration name="SyncListTest.testAddList" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="SortLinkedList" /> <module name="SortLinkedList" />
<option name="PACKAGE_NAME" value="" /> <option name="PACKAGE_NAME" value="" />
...@@ -68,8 +71,31 @@ ...@@ -68,8 +71,31 @@
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="SyncListTest.testRandSeq" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="SortLinkedList" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="SyncListTest" />
<option name="METHOD_NAME" value="testRandSeq" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="SyncListTest.testRun" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="SortLinkedList" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="SyncListTest" />
<option name="METHOD_NAME" value="testRun" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="JUnit.SyncListTest.testRun" />
<item itemvalue="JUnit.SyncListTest.testRandSeq" />
<item itemvalue="JUnit.SyncListTest" />
<item itemvalue="JUnit.SyncListTest.testAddList" /> <item itemvalue="JUnit.SyncListTest.testAddList" />
</list> </list>
</recent_temporary> </recent_temporary>
...@@ -83,6 +109,14 @@ ...@@ -83,6 +109,14 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1679470917075</updated> <updated>1679470917075</updated>
</task> </task>
<task id="LOCAL-00001" summary="Concurrent Sorted Linked List Implementations">
<created>1679492089742</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1679492089742</updated>
</task>
<option name="localTasksCounter" value="2" />
<servers /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <component name="Vcs.Log.Tabs.Properties">
......
...@@ -9,6 +9,10 @@ ...@@ -9,6 +9,10 @@
* Structured Lock (synchronized). * Structured Lock (synchronized).
* Unstructured Lock (ReentrantLock, ReentrantReadWriteLock). * Unstructured Lock (ReentrantLock, ReentrantReadWriteLock).
* Test different implementations' performance. * Test different implementations' performance.
* Test time for different Sorted Linked List Implementations.
## TO-DO
* Test throughput (operations per second) for different Sorted Linked List Implementations. ## Ref
[Concurrent Programming in Java](https://www.coursera.org/learn/concurrent-programming-in-java/home/week/1)
[Locking Java](https://web.archive.org/web/20200809031812/http://www.ibm.com/developerworks/java/library/j-jtp10264)
\ No newline at end of file
public class AddThread extends TestThread implements Runnable {
public AddThread(RandomSeq seq, int seqPart, SortList setList) {
super(seq, seqPart, setList);
}
@Override
public void run() {
for (int i = 0; i < nums.length; i++) {
list.add(nums[i]);
}
}
}
public class ContainThread extends TestThread implements Runnable{
public ContainThread(RandomSeq seq, int seqPart, SortList setList) {
super(seq, seqPart, setList);
}
@Override
public void run() {
for (int i = 0; i < nums.length; i++) {
list.contain(nums[i]);
}
}
}
import java.util.Random;
public class RandomSeq {
int seed;
Random random;
int maxNum;
public RandomSeq(int setseed ,int setmaxNum) {
random = new Random(setseed);
this.seed = setseed;
this.maxNum = setmaxNum;
}
public int next(){
return random.nextInt(maxNum);
}
}
public class RemoveThread extends TestThread implements Runnable{
public RemoveThread(RandomSeq seq, int seqPart, SortList setList) {
super(seq, seqPart, setList);
}
@Override
public void run() {
for (int i = 0; i < nums.length; i++) {
list.remove(nums[i]);
}
}
}
\ No newline at end of file
import junit.framework.TestCase; import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.List;
public class SyncListTest extends TestCase { public class SyncListTest extends TestCase {
public void testAddList(){ public void testAddList(){
SyncList syncList = new SyncList(); SyncList syncList = new SyncList();
// syncList.remove(Integer.MAX_VALUE);
syncList.add(1); syncList.add(1);
syncList.add(2); syncList.add(2);
syncList.add(3); syncList.add(3);
...@@ -14,9 +17,84 @@ public class SyncListTest extends TestCase { ...@@ -14,9 +17,84 @@ public class SyncListTest extends TestCase {
System.out.println(syncList.contain(5)); System.out.println(syncList.contain(5));
System.out.println(syncList.contain(2)); System.out.println(syncList.contain(2));
syncList.remove(3); syncList.remove(3);
}
public void testRandSeq() {
RandomSeq randomSeq = new RandomSeq(0, 80_000);
for (int i = 0; i < 10; i++) {
System.out.print(randomSeq.next() + " ");
}
}
int randLen = 20_000;
public void testHelp(SortList list, String label) {
RandomSeq seq = new RandomSeq(0, 80_000);
List<Thread> addThreads = new ArrayList<>();
List<Thread> containThreads = new ArrayList<>();
List<Thread> removeThreads = new ArrayList<>();
for (int i = 0; i < 8; i++) {
AddThread addThread = new AddThread(seq, randLen / 8, list);
ContainThread containThread = new ContainThread(seq, randLen / 8, list);
RemoveThread removeThread = new RemoveThread(seq, randLen / 8, list);
Thread threadA = new Thread(addThread);
addThreads.add(threadA);
Thread threadC = new Thread(containThread);
containThreads.add(threadC);
Thread threadR = new Thread(removeThread);
removeThreads.add(threadR);
}
long startA = System.currentTimeMillis();
addThreads.stream().forEach(e -> e.start() );
addThreads.stream().forEach(e -> {
try {
e.join();
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
});
long endA = System.currentTimeMillis() - startA;
System.out.println("ADD "+label+" execution task: "+endA+" ms");
long startC = System.currentTimeMillis();
containThreads.stream().forEach(e -> e.start() );
containThreads.stream().forEach(e -> {
try {
e.join();
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
});
long endC = System.currentTimeMillis() - startC;
System.out.println("Contain "+label+" execution task: "+endC+" ms");
syncList.printList(); long startR = System.currentTimeMillis();
removeThreads.stream().forEach(e -> e.start() );
removeThreads.stream().forEach(e -> {
try {
e.join();
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
});
long endR = System.currentTimeMillis() - startR;
System.out.println("Remove "+label+" execution task: "+endR+" ms");
}
public void testRun(){
SyncList syncList = new SyncList();
testHelp(syncList,"Synchronization");
System.out.println("==============");
RWLockList rwLockList = new RWLockList();
testHelp(rwLockList, "RWLock");
System.out.println("==============");
LockList list = new LockList();
testHelp(list,"Lock");
} }
} }
public abstract class TestThread {
SortList list;
Integer [] nums;
public TestThread(RandomSeq seq, int seqPart, SortList setList) {
this.list = setList;
this.nums = new Integer[seqPart];
for (int i=0;i<nums.length;i++){
nums[i] = seq.next();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment