This repository was archived by the owner on May 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/akurilov/java-concurrent
- Loading branch information
Showing
7 changed files
with
118 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 1 addition & 4 deletions
5
...outine/RoundRobinOutputCoroutineTest.java → ...outine/RoundRobinOutputCoroutineTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 0 additions & 110 deletions
110
...test/java/com/github/akurilov/concurrent/test/throttle/SequentialWeightsThrottleTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/test/java/com/github/akurilov/concurrent/throttle/SequentialWeightsThrottleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.github.akurilov.concurrent.throttle; | ||
|
||
import org.junit.Test; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.LongAdder; | ||
import java.util.concurrent.locks.LockSupport; | ||
|
||
/** | ||
Created by andrey on 06.11.16. | ||
*/ | ||
|
||
public class SequentialWeightsThrottleTest { | ||
|
||
private static final int WRITE = 0; | ||
private static final int READ = 1; | ||
|
||
private final int[] weights = new int[] { | ||
80, | ||
20 | ||
}; | ||
private final LongAdder[] resultCounters = new LongAdder[] { | ||
new LongAdder(), | ||
new LongAdder() | ||
}; | ||
|
||
private final SequentialWeightsThrottle wt = new SequentialWeightsThrottle(weights); | ||
|
||
private final class SubmTask | ||
implements Runnable { | ||
private final int origin; | ||
public SubmTask(final int origin) { | ||
this.origin = origin; | ||
} | ||
@Override | ||
public final void run() { | ||
while(true) { | ||
if(wt.tryAcquire(origin)) { | ||
resultCounters[origin].increment(); | ||
} else { | ||
LockSupport.parkNanos(1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testRequestApprovalFor() | ||
throws Exception { | ||
final ExecutorService es = Executors.newFixedThreadPool(2); | ||
es.submit(new SubmTask(WRITE)); | ||
es.submit(new SubmTask(READ)); | ||
es.awaitTermination(10, TimeUnit.SECONDS); | ||
es.shutdownNow(); | ||
final double writes = resultCounters[WRITE].sum(); | ||
final long reads = resultCounters[READ].sum(); | ||
assertEquals(80/20, writes / reads, 0.01); | ||
System.out.println("Write rate: " + writes / 10 + " Hz, read rate: " + reads / 10 + " Hz"); | ||
} | ||
|
||
private final class BatchSubmTask | ||
implements Runnable { | ||
private final int origin; | ||
public BatchSubmTask(final int origin) { | ||
this.origin = origin; | ||
} | ||
@Override | ||
public final void run() { | ||
int n; | ||
while(true) { | ||
n = wt.tryAcquire(origin, 128); | ||
if(n > 0) { | ||
resultCounters[origin].add(n); | ||
} else { | ||
LockSupport.parkNanos(1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testRequestBatchApprovalFor() | ||
throws Exception { | ||
final ExecutorService es = Executors.newFixedThreadPool(2); | ||
es.submit(new BatchSubmTask(WRITE)); | ||
es.submit(new BatchSubmTask(READ)); | ||
es.awaitTermination(10, TimeUnit.SECONDS); | ||
es.shutdownNow(); | ||
final double writes = resultCounters[WRITE].sum(); | ||
final long reads = resultCounters[READ].sum(); | ||
assertEquals(80/20, writes / reads, 0.01); | ||
System.out.println("Write rate: " + writes / 10 + " Hz, read rate: " + reads / 10 + " Hz"); | ||
} | ||
} |