Skip to content

Commit

Permalink
Merge pull request #70 from DanielYWoo/deprecate_jdk_8
Browse files Browse the repository at this point in the history
deprecate jdk8
  • Loading branch information
DanielYWoo committed Nov 18, 2023
2 parents 1d838cb + c6abddd commit bb733fb
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 167 deletions.
Binary file added docs/1.xlsx
Binary file not shown.
Binary file added docs/2.xlsx
Binary file not shown.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>cn.danielw</groupId>
<artifactId>fast-object-pool</artifactId>
<packaging>jar</packaging>
<version>2.2.1</version>
<version>2.3.0</version>
<description>An extremely fast object pool with zero dependencies</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -56,7 +56,7 @@
<dependency>
<groupId>com.conversantmedia</groupId>
<artifactId>disruptor</artifactId>
<version>1.2.15</version>
<version>1.2.21</version>
<!-- This is optional, if you want the best performance you need to manually add disruptor into your dependency -->
<optional>true</optional>
</dependency>
Expand Down Expand Up @@ -99,8 +99,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>8</source>
<target>8</target>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
Expand All @@ -121,7 +121,7 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<source>8</source>
<source>11</source>
</configuration>
<executions>
<execution>
Expand Down
4 changes: 3 additions & 1 deletion src/benchmark/BaseWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ public abstract class BaseWorker extends Thread {
private final int id;
private final long loop;
protected final int borrowsPerLoop;
protected int simulateBlockingMs;
long err = 0;

public BaseWorker(Benchmark benchmark, int id, int borrowsPerLoop, long loop) {
public BaseWorker(Benchmark benchmark, int id, int borrowsPerLoop, long loop, int simulateBlockingMs) {
this.benchmark = benchmark;
this.id = id;
this.loop = loop;
this.borrowsPerLoop = borrowsPerLoop;
this.simulateBlockingMs = simulateBlockingMs;
}

@Override public void run() {
Expand Down
6 changes: 2 additions & 4 deletions src/benchmark/Benchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public BenchmarkResult testAndPrint() throws InterruptedException {
stats += statsAvgRespTime[i];
}
double latency = stats / workerCount;
System.out.println("Average Response Time:" + new DecimalFormat("0.0000").format(latency));
// System.out.println("Average Response Time:" + new DecimalFormat("0.0000").format(latency));

stats = 0;
for (int i = 0; i < workerCount; i++) {
Expand All @@ -52,10 +52,8 @@ public BenchmarkResult testAndPrint() throws InterruptedException {

double throughput = (double) loop * workerCount / (t2 - t1);

System.out.println("Error Ratio:" + new DecimalFormat("0.00%").format(errRate));
System.out.println("Throughput Per Second:" + new DecimalFormat("0").format(throughput) + "K");
System.out.println("Objects created:" + created.get());
System.out.println();
// System.out.println("Objects created:" + created.get());
return new BenchmarkResult(name, workerCount, borrowsPerLoop, loop, created.get(), errRate, throughput, latency);
}

Expand Down
32 changes: 22 additions & 10 deletions src/benchmark/BenchmarkCommons.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,31 @@
*/
public class BenchmarkCommons extends Benchmark {

BenchmarkCommons(int workerCount, int borrowsPerLoop, int loop) throws Exception {
BenchmarkCommons(int workerCount, int borrowsPerLoop, int loop, int simulateBlockingMs) {
super("common-pool", workerCount, borrowsPerLoop, loop);
GenericObjectPool<StringBuilder> pool = new GenericObjectPool<>(new PooledObjectFactory<StringBuilder>() {
@Override public PooledObject<StringBuilder> makeObject() throws Exception {
GenericObjectPool<StringBuilder> pool = new GenericObjectPool<>(new PooledObjectFactory<>() {
@Override
public PooledObject<StringBuilder> makeObject() {
created.incrementAndGet();
return new DefaultPooledObject<>(new StringBuilder());
}

@Override public void destroyObject(PooledObject<StringBuilder> pooledObject) throws Exception { }
@Override
public void destroyObject(PooledObject<StringBuilder> pooledObject) {
}

@Override public boolean validateObject(PooledObject<StringBuilder> pooledObject) { return false; }
@Override
public boolean validateObject(PooledObject<StringBuilder> pooledObject) {
return false;
}

@Override public void activateObject(PooledObject<StringBuilder> pooledObject) throws Exception { }
@Override
public void activateObject(PooledObject<StringBuilder> pooledObject) {
}

@Override public void passivateObject(PooledObject<StringBuilder> pooledObject) throws Exception { }
@Override
public void passivateObject(PooledObject<StringBuilder> pooledObject) {
}
});
pool.setMinIdle(256);
pool.setMaxIdle(256);
Expand All @@ -34,16 +44,16 @@ public class BenchmarkCommons extends Benchmark {

this.workers = new Worker[workerCount];
for (int i = 0; i < workerCount; i++) {
workers[i] = new Worker(this, i, loop, borrowsPerLoop, pool);
workers[i] = new Worker(this, i, loop, borrowsPerLoop, simulateBlockingMs, pool);
}
}

private static class Worker extends BaseWorker {

private final GenericObjectPool<StringBuilder> pool;

Worker(Benchmark benchmark, int id, int loop, int borrowsPerLoop, GenericObjectPool<StringBuilder> pool) {
super(benchmark, id, borrowsPerLoop, loop);
Worker(Benchmark benchmark, int id, int loop, int borrowsPerLoop, int simulateBlockingMs, GenericObjectPool<StringBuilder> pool) {
super(benchmark, id, borrowsPerLoop, loop, simulateBlockingMs);
this.pool = pool;
}

Expand All @@ -52,6 +62,8 @@ private static class Worker extends BaseWorker {
try {
for (int i = 0; i < borrowsPerLoop; i++) {
StringBuilder obj = pool.borrowObject(10);
obj.append("X");
if (simulateBlockingMs > 0) Thread.sleep(simulateBlockingMs); // simulate thread blocking
list.add(obj);
}
} catch (Exception e) {
Expand Down
14 changes: 7 additions & 7 deletions src/benchmark/BenchmarkFastObjectPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@
*/
public class BenchmarkFastObjectPool extends Benchmark {

BenchmarkFastObjectPool(String name, int workerCount, int borrows, int loop) throws InterruptedException {
BenchmarkFastObjectPool(String name, int workerCount, int borrows, int loop, int simulateBlockingMs) {
super(name, workerCount, borrows, loop);
PoolConfig config = new PoolConfig();
config.setPartitionSize(16);
config.setPartitionSize(32);
config.setMaxSize(16);
config.setMinSize(16);
config.setMaxIdleMilliseconds(60 * 1000 * 5);
config.setMaxWaitMilliseconds(10);


ObjectFactory<StringBuilder> factory = new ObjectFactory<StringBuilder>() {
ObjectFactory<StringBuilder> factory = new ObjectFactory<>() {
@Override
public StringBuilder create() {
created.incrementAndGet();
Expand All @@ -38,16 +37,16 @@ public boolean validate(StringBuilder o) {
ObjectPool<StringBuilder> pool = new ObjectPool<>(config, factory);
workers = new Worker[workerCount];
for (int i = 0; i < workerCount; i++) {
workers[i] = new Worker(this, i, borrows, loop, pool);
workers[i] = new Worker(this, i, borrows, loop, simulateBlockingMs, pool);
}
}

protected static class Worker extends BaseWorker {

private final ObjectPool<StringBuilder> pool;

Worker(Benchmark benchmark, int id, int borrows, long loop, ObjectPool<StringBuilder> pool) {
super(benchmark, id, borrows, loop);
Worker(Benchmark benchmark, int id, int borrows, long loop, int simulateBlockingMs, ObjectPool<StringBuilder> pool) {
super(benchmark, id, borrows, loop, simulateBlockingMs);
this.pool = pool;
}

Expand All @@ -57,6 +56,7 @@ protected static class Worker extends BaseWorker {
for (int i = 0; i < borrowsPerLoop; i++) {
Poolable<StringBuilder> obj = pool.borrowObject(false);
obj.getObject().append("X");
if (simulateBlockingMs > 0) Thread.sleep(simulateBlockingMs); // simulate thread blocking
list.add(obj);
}
} catch (Exception e) {
Expand Down
10 changes: 4 additions & 6 deletions src/benchmark/BenchmarkFastObjectPoolDisruptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@
import cn.danielw.fop.ObjectFactory;
import cn.danielw.fop.PoolConfig;

import java.util.concurrent.ThreadPoolExecutor;

/**
* @author Daniel
*/
public class BenchmarkFastObjectPoolDisruptor extends Benchmark {

BenchmarkFastObjectPoolDisruptor(int workerCount, int borrows, int loop) throws InterruptedException {
BenchmarkFastObjectPoolDisruptor(int workerCount, int borrows, int loop, int simulateBlockingMs) {
super("fop", workerCount, borrows, loop);
PoolConfig config = new PoolConfig();
config.setPartitionSize(16);
config.setPartitionSize(32);
config.setMaxSize(16);
config.setMinSize(16);
config.setMaxIdleMilliseconds(60 * 1000 * 5);
config.setMaxWaitMilliseconds(10);

ObjectFactory<StringBuilder> factory = new ObjectFactory<StringBuilder>() {
ObjectFactory<StringBuilder> factory = new ObjectFactory<>() {
@Override
public StringBuilder create() {
created.incrementAndGet();
Expand All @@ -35,7 +33,7 @@ public boolean validate(StringBuilder o) {
DisruptorObjectPool<StringBuilder> pool = new DisruptorObjectPool<>(config, factory);
workers = new BenchmarkFastObjectPool.Worker[workerCount];
for (int i = 0; i < workerCount; i++) {
workers[i] = new BenchmarkFastObjectPool.Worker(this, i, borrows, loop, pool);
workers[i] = new BenchmarkFastObjectPool.Worker(this, i, borrows, loop, simulateBlockingMs, pool);
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/benchmark/BenchmarkFurious.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import nf.fr.eraasoft.pool.ObjectPool;
import nf.fr.eraasoft.pool.PoolException;
import nf.fr.eraasoft.pool.PoolSettings;
import nf.fr.eraasoft.pool.PoolableObjectBase;

Expand All @@ -11,16 +10,17 @@
*/
public class BenchmarkFurious extends Benchmark {

BenchmarkFurious(int workerCount, int borrows, int loop) throws InterruptedException {
BenchmarkFurious(int workerCount, int borrows, int loop, int simulateBlockingMs) {
super("furious", workerCount, borrows, loop);

// Create your PoolSettings with an instance of PoolableObject
PoolSettings<StringBuilder> poolSettings = new PoolSettings<>(new PoolableObjectBase<StringBuilder>() {
PoolSettings<StringBuilder> poolSettings = new PoolSettings<>(new PoolableObjectBase<>() {
@Override
public StringBuilder make() {
created.incrementAndGet();
return new StringBuilder();
}

@Override
public void activate(StringBuilder t) {
t.setLength(0);
Expand All @@ -34,16 +34,16 @@ public void activate(StringBuilder t) {

workers = new Worker[workerCount];
for (int i = 0; i < workerCount; i++) {
workers[i] = new Worker(this, i, borrows, loop, pool);
workers[i] = new Worker(this, i, borrows, loop, simulateBlockingMs, pool);
}
}

private static class Worker extends BaseWorker {

private final ObjectPool<StringBuilder> pool;

Worker(Benchmark benchmark, int id, int borrows, long loop, ObjectPool<StringBuilder> pool) {
super(benchmark, id, borrows, loop);
Worker(Benchmark benchmark, int id, int borrows, long loop, int simulateBlockingMs, ObjectPool<StringBuilder> pool) {
super(benchmark, id, borrows, loop, simulateBlockingMs);
this.pool = pool;
}

Expand All @@ -53,6 +53,7 @@ private static class Worker extends BaseWorker {
for (int i = 0; i < borrowsPerLoop; i++) {
StringBuilder obj = pool.getObj(); // NO timeout at method level
obj.append("X");
if (simulateBlockingMs > 0) Thread.sleep(simulateBlockingMs); // simulate thread blocking
list.add(obj);
}
} catch (Exception e) {
Expand Down
30 changes: 20 additions & 10 deletions src/benchmark/BenchmarkResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,27 @@ public BenchmarkResult(String poolName,

@Override
public String toString() {
return poolName +
"," + threads +
"," + borrows +
"," + loops +
"," + created +
"," + plotLegend() +
"," + errorRate +
"," + avgThroughput;
// return poolName +
// "," + threads +
// "," + borrows +
// "," + loops +
// "," + created +
// "," + plotLegend() +
// "," + errorRate +
// "," + avgThroughput;
return threads + "/" + poolName + "," + errorRate + "," + avgThroughput;
}

private String plotLegend() {
return poolName + "/" + threads;
public String getPoolName() {
return poolName;
}

public double getErrorRate() {
return errorRate;
}

public double getAvgThroughput() {
return avgThroughput;
}

}
22 changes: 7 additions & 15 deletions src/benchmark/BenchmarkStormpot.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,25 @@ public void release() {
}


BenchmarkStormpot(int workerCount, int borrows, int loop) throws InterruptedException {
BenchmarkStormpot(int workerCount, int borrows, int loop, int simulateBlockingMs) {
super("Stormpot", workerCount, borrows, loop);

Config<MyPoolable> config = new Config<>().setAllocator(new Allocator<MyPoolable>() {
@Override
public MyPoolable allocate(Slot slot) throws Exception {
public MyPoolable allocate(Slot slot) {
created.incrementAndGet();
return new MyPoolable(slot);
}

@Override
public void deallocate(MyPoolable x) throws Exception {
public void deallocate(MyPoolable x) {

}
}).setSize(256);
/*
config.setExpiration(new Expiration<MyPoolable>() {
@Override
public boolean hasExpired(SlotInfo<? extends MyPoolable> slotInfo) throws Exception {
// how to support max idle?
return slotInfo.getAgeMillis() > 60 * 10000 * 5;
}
});
*/
Pool<MyPoolable> pool = new BlazePool<>(config);
workers = new Worker[workerCount];
for (int i = 0; i < workerCount; i++) {
workers[i] = new Worker(this, i, borrows, loop, pool);
workers[i] = new Worker(this, i, borrows, loop, simulateBlockingMs, pool);
}
System.out.println("slots:" + slots.size());
}
Expand All @@ -73,8 +64,8 @@ private static class Worker extends BaseWorker {
private static final Timeout TIMEOUT = new Timeout(10, TimeUnit.MILLISECONDS);
private final Pool<MyPoolable> pool;

Worker(Benchmark benchmark, int id, int borrows, int loop, Pool<MyPoolable> pool) {
super(benchmark, id, borrows, loop);
Worker(Benchmark benchmark, int id, int borrows, int loop, int simulateBlockingMs, Pool<MyPoolable> pool) {
super(benchmark, id, borrows, loop, simulateBlockingMs);
this.pool = pool;
}

Expand All @@ -84,6 +75,7 @@ private static class Worker extends BaseWorker {
for (int i = 0; i < borrowsPerLoop; i++) {
MyPoolable obj = pool.claim(TIMEOUT);
obj.getTest().append("x");
if (simulateBlockingMs > 0) Thread.sleep(simulateBlockingMs); // simulate thread blocking
list.add(obj);
}
} catch (Exception e) {
Expand Down
Loading

0 comments on commit bb733fb

Please sign in to comment.