Skip to content
This repository was archived by the owner on May 14, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Kurilov committed Apr 25, 2018
2 parents 95f6deb + 82c4f64 commit 7ebf06a
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 141 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ executed concurrently also.
## Gradle

```groovy
compile group: 'com.github.akurilov', name: 'java-concurrent', version: '1.1.4'
compile group: 'com.github.akurilov', name: 'java-concurrent', version: '2.0.3'
```

## Implementing Basic Coroutine
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ allprojects {
apply plugin: "maven"
apply plugin: "signing"
group = "com.github.akurilov"
version = "2.0.2"
version = "2.0.3"
}

ext.moduleName = "${group}.concurrent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
The throttle determines the weight for each I/O task and makes the decision.
The weight is used to pass the I/O task with specific ratio for the different keys.
*/
public final class SequentialWeightsThrottle {
public final class SequentialWeightsThrottle
implements IndexThrottle {

// initial weight map (constant)
private final int[] weights;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.github.akurilov.concurrent.test.coroutine;
package com.github.akurilov.concurrent.coroutine;

import com.github.akurilov.commons.io.Input;
import com.github.akurilov.commons.io.Output;

import com.github.akurilov.concurrent.coroutine.CoroutinesExecutor;
import com.github.akurilov.concurrent.coroutine.RoundRobinOutputCoroutine;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.akurilov.concurrent.test.throttle;
package com.github.akurilov.concurrent.throttle;

import com.github.akurilov.concurrent.throttle.RateThrottle;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

Expand All @@ -20,13 +19,12 @@ public void testRate100mHzNonBatch()
throws Exception {
final double rateLimit = 0.1;
final int timeLimitSec = 50;
final RateThrottle throttle = new RateThrottle(rateLimit);
final Object subj = new Object();
final Throttle throttle = new RateThrottle(rateLimit);
final LongAdder counter = new LongAdder();
final Thread submThread = new Thread(
() -> {
while(true) {
if(throttle.tryAcquire(subj)) {
if(throttle.tryAcquire()) {
counter.increment();
} else {
LockSupport.parkNanos(1);
Expand All @@ -46,13 +44,12 @@ public void testRate10HzNonBatch()
throws Exception {
final int rateLimit = 10;
final int timeLimitSec = 10;
final RateThrottle throttle = new RateThrottle(rateLimit);
final Object subj = new Object();
final Throttle throttle = new RateThrottle(rateLimit);
final LongAdder counter = new LongAdder();
final Thread submThread = new Thread(
() -> {
while(true) {
if(throttle.tryAcquire(subj)) {
if(throttle.tryAcquire()) {
counter.increment();
} else {
LockSupport.parkNanos(1);
Expand All @@ -72,13 +69,12 @@ public void testRate100kHzNonBatch()
throws Exception {
final int rateLimit = 100_000;
final int timeLimitSec = 20;
final RateThrottle throttle = new RateThrottle(rateLimit);
final Object subj = new Object();
final Throttle throttle = new RateThrottle(rateLimit);
final LongAdder counter = new LongAdder();
final Thread submThread = new Thread(
() -> {
while(true) {
if(throttle.tryAcquire(subj)) {
if(throttle.tryAcquire()) {
counter.increment();
} else {
LockSupport.parkNanos(1);
Expand All @@ -98,14 +94,13 @@ public void testRate1HzBatch()
throws Exception {
final int rateLimit = 1;
final int timeLimitSec = 50;
final RateThrottle throttle = new RateThrottle(rateLimit);
final Object subj = new Object();
final Throttle throttle = new RateThrottle(rateLimit);
final LongAdder counter = new LongAdder();
final Thread submThread = new Thread(
() -> {
int n;
while(true) {
n = throttle.tryAcquire(subj, 10);
n = throttle.tryAcquire(10);
if(n > 0) {
counter.add(n);
} else {
Expand All @@ -126,14 +121,13 @@ public void testRate100HzBatch()
throws Exception {
final int rateLimit = 100;
final int timeLimitSec = 50;
final RateThrottle throttle = new RateThrottle(rateLimit);
final Object subj = new Object();
final Throttle throttle = new RateThrottle(rateLimit);
final LongAdder counter = new LongAdder();
final Thread submThread = new Thread(
() -> {
int n;
while(true) {
n = throttle.tryAcquire(subj, 100);
n = throttle.tryAcquire(100);
if(n > 0) {
counter.add(n);
} else {
Expand All @@ -154,14 +148,13 @@ public void testRate1MHzBatch()
throws Exception {
final int rateLimit = 1_000_000;
final int timeLimitSec = 10;
final RateThrottle throttle = new RateThrottle(rateLimit);
final Object subj = new Object();
final Throttle throttle = new RateThrottle(rateLimit);
final LongAdder counter = new LongAdder();
final Thread submThread = new Thread(
() -> {
int n;
while(true) {
n = throttle.tryAcquire(subj, 100);
n = throttle.tryAcquire(100);
if(n > 0) {
counter.add(n);
} else {
Expand All @@ -181,8 +174,7 @@ public void testRate1MHzBatch()
public void testRate1kHzBatchConcurrent() {
final int rateLimit = 1_000;
final int timeLimitSec = 20;
final Object subj = new Object();
final RateThrottle throttle = new RateThrottle(rateLimit);
final Throttle throttle = new RateThrottle(rateLimit);
final LongAdder counter = new LongAdder();
final ExecutorService execSvc = Executors.newFixedThreadPool(4);
for(int i = 0; i < 4; i ++) {
Expand All @@ -192,13 +184,13 @@ public void testRate1kHzBatchConcurrent() {
int n;
while(true) {
if(j == 0) {
if(throttle.tryAcquire(subj)) {
if(throttle.tryAcquire()) {
counter.increment();
} else {
LockSupport.parkNanos(1);
}
} else {
n = throttle.tryAcquire(subj, 1 + j);
n = throttle.tryAcquire(1 + j);
if(n > 0) {
counter.add(n);
} else {
Expand Down
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");
}
}

0 comments on commit 7ebf06a

Please sign in to comment.