Skip to content

Commit

Permalink
Major refactoring (initial support for mutable Datasets, wavelets, up…
Browse files Browse the repository at this point in the history
…grade to NB 21 and Java 21,...)
  • Loading branch information
didalgolab authored and Mariusz Bernacki committed May 5, 2024
1 parent f8bd316 commit cbde480
Show file tree
Hide file tree
Showing 109 changed files with 10,609 additions and 279 deletions.
15 changes: 0 additions & 15 deletions application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,6 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>9.5</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
<version>9.5</version>
</dependency>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-tree</artifactId>
<version>9.5</version>
</dependency>
<dependency>
<groupId>org.netbeans.modules</groupId>
<artifactId>org-netbeans-core-startup-base</artifactId>
Expand Down
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();
}
}
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();
}
}

This file was deleted.

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;
}
}
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];
}
}
}
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];
}
}
}
Loading

0 comments on commit cbde480

Please sign in to comment.