-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactoring (initial support for mutable Datasets, wavelets, up…
…grade to NB 21 and Java 21,...)
- Loading branch information
1 parent
f8bd316
commit cbde480
Showing
109 changed files
with
10,609 additions
and
279 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
53 changes: 53 additions & 0 deletions
53
chartsy-benchmarking/src/main/java/one/chartsy/benchmarking/IndexOfBenchmark.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,53 @@ | ||
package one.chartsy.benchmarking; | ||
|
||
import org.openjdk.jmh.annotations.*; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
@State(Scope.Benchmark) | ||
public class IndexOfBenchmark { | ||
|
||
private long nextWrite; | ||
private int offset; | ||
|
||
private static final long mask = (1 << 19) - 1; | ||
private static final int mask2 = (1 << 19) - 1; | ||
|
||
@Setup(Level.Iteration) | ||
public void setUp() { | ||
ThreadLocalRandom random = ThreadLocalRandom.current(); | ||
nextWrite = random.nextLong(); | ||
offset = random.nextInt(100); | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
public int testIndexOf1() { | ||
return indexOf1(nextWrite, offset); | ||
} | ||
|
||
private static int indexOf1(long nextWrite, int offset) { | ||
return (int) ((nextWrite - offset - 1) & mask); | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
public int testIndexOf2() { | ||
return indexOf2(nextWrite, offset); | ||
} | ||
|
||
private static int indexOf2(long nextWrite, int offset) { | ||
return ((int)nextWrite - offset - 1) & mask2; | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
org.openjdk.jmh.runner.options.Options opt = new org.openjdk.jmh.runner.options.OptionsBuilder() | ||
.include(IndexOfBenchmark.class.getSimpleName()) | ||
.forks(1) | ||
.build(); | ||
|
||
new org.openjdk.jmh.runner.Runner(opt).run(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
chartsy-benchmarking/src/main/java/one/chartsy/benchmarking/IndexOfBenchmarkTest.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,44 @@ | ||
package one.chartsy.benchmarking; | ||
|
||
import org.openjdk.jmh.annotations.*; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class IndexOfBenchmarkTest { | ||
|
||
@State(Scope.Thread) | ||
public static class BenchmarkState { | ||
long nextWrite = ThreadLocalRandom.current().nextLong(); | ||
int offset = ThreadLocalRandom.current().nextInt(); | ||
long maskLong = (1L << ThreadLocalRandom.current().nextInt(30)) - 1; | ||
int maskInt = (int) maskLong; | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
public int testIndexOf1(BenchmarkState state) { | ||
return (int) ((state.nextWrite - state.offset - 1) & state.maskLong); | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
public int testIndexOf2(BenchmarkState state) { | ||
return ((int) state.nextWrite - state.offset - 1) & state.maskInt; | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
org.openjdk.jmh.runner.options.Options opt = new org.openjdk.jmh.runner.options.OptionsBuilder() | ||
.include(IndexOfBenchmarkTest.class.getSimpleName()) | ||
.mode(org.openjdk.jmh.annotations.Mode.AverageTime) | ||
.timeUnit(TimeUnit.NANOSECONDS) | ||
.warmupIterations(1) | ||
.measurementIterations(5) | ||
.forks(1) | ||
.build(); | ||
|
||
new org.openjdk.jmh.runner.Runner(opt).run(); | ||
} | ||
} |
10 changes: 0 additions & 10 deletions
10
chartsy-benchmarking/src/main/java/one/chartsy/benchmarking/PriorityMapTest.java
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
...enchmarking/src/main/java/one/chartsy/benchmarking/RingBufferArrayIndexBenchmarkTest.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,32 @@ | ||
/* | ||
* Copyright 2024 Mariusz Bernacki <[email protected]> | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package one.chartsy.benchmarking; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
public class RingBufferArrayIndexBenchmarkTest { | ||
|
||
private static final long mask = (1 << 19) - 1; | ||
private static final int mask2 = (1 << 19) - 1; | ||
|
||
public static void main(String[] args) { | ||
ThreadLocalRandom r = ThreadLocalRandom.current(); | ||
for (long i = 0; i < 1000_000_000; i++) { | ||
long nextWrite = r.nextLong(); | ||
int v1 = indexOf1(nextWrite, 0); | ||
int v2 = indexOf2(nextWrite, 0); | ||
if (v1 != v2) | ||
System.out.println("DIFF: "+v1+" != "+v2); | ||
} | ||
} | ||
|
||
private static int indexOf1(long nextWrite, int offset) { | ||
return (int) ((nextWrite - offset - 1) & mask); | ||
} | ||
|
||
private static int indexOf2(long nextWrite, int offset) { | ||
return ((int)nextWrite - offset - 1) & mask2; | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
chartsy-benchmarking/src/main/java/one/chartsy/benchmarking/RingBufferBenchmark.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,105 @@ | ||
/* | ||
* Copyright 2024 Mariusz Bernacki <[email protected]> | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package one.chartsy.benchmarking; | ||
|
||
import one.chartsy.Candle; | ||
import one.chartsy.base.RingBuffer; | ||
import one.chartsy.base.function.IndexedConsumer; | ||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
|
||
@State(Scope.Thread) | ||
public class RingBufferBenchmark implements Consumer<Object>, IndexedConsumer<Object> { | ||
|
||
private RingBuffer<Candle> ringBuffer; | ||
private Candle[] preallocatedRandoms; | ||
private int index; | ||
private final int getIndex = ThreadLocalRandom.current().nextInt(RING_CAPACITY); | ||
private static final int PREALLOC_SIZE = 100_000_000; | ||
private static final int RING_CAPACITY = 1000; | ||
|
||
@Setup(Level.Trial) | ||
public void setUp() { | ||
ringBuffer = new RingBuffer<>(RING_CAPACITY); | ||
preallocatedRandoms = new Candle[PREALLOC_SIZE]; | ||
ThreadLocalRandom random = ThreadLocalRandom.current(); | ||
long time = System.currentTimeMillis(); | ||
for (int i = 0; i < PREALLOC_SIZE; i++) { | ||
preallocatedRandoms[i] = Candle.of(time++, random.nextDouble(), random.nextDouble(), random.nextDouble(), random.nextDouble()); | ||
ringBuffer.add(Candle.of(time++, random.nextDouble(), random.nextDouble(), random.nextDouble(), random.nextDouble())); | ||
} | ||
index = 0; | ||
} | ||
|
||
private int totalHash; | ||
|
||
@Override | ||
public void accept(Object o) { | ||
totalHash += o.hashCode(); | ||
} | ||
|
||
@Override | ||
public void accept(Object o, int index) { | ||
totalHash += o.hashCode(); | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
public void addAndGet(Blackhole blackhole) { | ||
ringBuffer.add(preallocatedRandoms[index]); | ||
if (++index >= PREALLOC_SIZE) | ||
index = 0; | ||
//index = (index + 1) % PREALLOC_SIZE; // wrap around to use the preallocated array circularly | ||
//return ringBuffer.get(getIndex); | ||
ringBuffer.forEach(this); // 168733,830 ± 3145,072 ops/s | ||
//ringBuffer.forEach2(this); // 174781,318 ± 1579,128 ops/s | ||
//ringBuffer.forEachIndexed(this); // 175122,853 ± 1216,645 ops/s | ||
} | ||
|
||
//@Benchmark | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
public long newInstance() { | ||
return new RingBuffer<>().stream().count(); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
org.openjdk.jmh.runner.options.Options opt = new org.openjdk.jmh.runner.options.OptionsBuilder() | ||
.include(RingBufferBenchmark.class.getSimpleName()) | ||
.forks(1) | ||
.build(); | ||
|
||
new org.openjdk.jmh.runner.Runner(opt).run(); | ||
} | ||
|
||
public static class RingBuffer0<T> { | ||
private final Object[] elements; | ||
private int head = 0; | ||
private int tail = 0; | ||
private final int maxSize; | ||
|
||
public RingBuffer0(int maxSize) { | ||
this.maxSize = maxSize; | ||
this.elements = new Object[maxSize]; | ||
} | ||
|
||
public void add(T element) { | ||
elements[tail] = element; | ||
tail = (tail + 1) % maxSize; | ||
if (tail == head) { | ||
head = (head + 1) % maxSize; // Overwrite scenario | ||
} | ||
} | ||
|
||
public T get(int index) { | ||
return (T) elements[(head + index) % maxSize]; | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...hmarking/src/main/java/one/chartsy/benchmarking/RingBufferOfDoubleAddAndGetBenchmark.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,85 @@ | ||
/* | ||
* Copyright 2024 Mariusz Bernacki <[email protected]> | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package one.chartsy.benchmarking; | ||
|
||
import one.chartsy.base.RingBuffer; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
|
||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@State(Scope.Benchmark) | ||
public class RingBufferOfDoubleAddAndGetBenchmark { | ||
|
||
private RingBuffer.OfDouble ringBuffer; | ||
private double[] preallocatedRandoms; | ||
private int index; | ||
private static final int PREALLOC_SIZE = 10_000_000; | ||
private static final int RING_CAPACITY = 1000; | ||
|
||
@Setup(Level.Trial) | ||
public void setUp() { | ||
ringBuffer = new RingBuffer.OfDouble(RING_CAPACITY); | ||
preallocatedRandoms = new double[PREALLOC_SIZE]; | ||
ThreadLocalRandom random = ThreadLocalRandom.current(); | ||
for (int i = 0; i < PREALLOC_SIZE; i++) { | ||
preallocatedRandoms[i] = random.nextDouble(); | ||
ringBuffer.add(random.nextDouble()); | ||
} | ||
index = 0; | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
public double testAdd() { | ||
ringBuffer.add(preallocatedRandoms[index]); | ||
if (++index >= PREALLOC_SIZE) | ||
index = 0; | ||
//index = (index + 1) % PREALLOC_SIZE; // wrap around to use the preallocated array circularly | ||
//return ringBuffer.get(1000); | ||
return ringBuffer.stream().sum(); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
org.openjdk.jmh.runner.options.Options opt = new org.openjdk.jmh.runner.options.OptionsBuilder() | ||
.include(RingBufferOfDoubleAddAndGetBenchmark.class.getSimpleName()) | ||
.forks(1) | ||
.build(); | ||
|
||
new org.openjdk.jmh.runner.Runner(opt).run(); | ||
} | ||
|
||
public static class RingBuffer0<T> { | ||
private final Object[] elements; | ||
private int head = 0; | ||
private int tail = 0; | ||
private final int maxSize; | ||
|
||
public RingBuffer0(int maxSize) { | ||
this.maxSize = maxSize; | ||
this.elements = new Object[maxSize]; | ||
} | ||
|
||
public void add(T element) { | ||
elements[tail] = element; | ||
tail = (tail + 1) % maxSize; | ||
if (tail == head) { | ||
head = (head + 1) % maxSize; // Overwrite scenario | ||
} | ||
} | ||
|
||
public T get(int index) { | ||
return (T) elements[(head + index) % maxSize]; | ||
} | ||
} | ||
} |
Oops, something went wrong.