Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
0.4.0
Browse files Browse the repository at this point in the history
projekt name change
Sub qol
benchmarks
  • Loading branch information
nothub committed Feb 9, 2021
1 parent f639e5e commit 895f80d
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 62 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
Check the [example](https://github.com/nothub/TinyPubSub/blob/master/src/test/java/cc/neckbeard/tinypubsub/example/Example.java) for a usage snippet.
A tiny and fast pubsub implementation with priorities and canceling.

Check the
[example](https://github.com/nothub/TinyEventBus/blob/master/src/test/java/cc/neckbeard/tinypubsub/example/Example.java)
for a usage snippet.

---

[benchmarks](https://github.com/nothub/TinyEventBus/tree/master/src/test/java/cc/neckbeard/tinypubsub/benchmark) (
openjdk 8, xeon e3-1230 3.30ghz):

```
pub
subs: 1_000
pubs: 1_000
47,681,726ns (47ms)
```

```
pub
subs: 100
pubs: 100_000
58,357,624ns (58ms)
```

```
reg
subs: 1_000
2,740,465ns (2ms)
```

```
del
subs: 1_000
7,297,579ns (7ms)
```
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<modelVersion>4.0.0</modelVersion>

<groupId>cc.neckbeard</groupId>
<artifactId>TinyPubSub</artifactId>
<version>0.3.1</version>
<artifactId>TinyEventBus</artifactId>
<version>0.4.0</version>

<name>${project.artifactId}</name>
<name>${project.groupId}:${project.artifactId}</name>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cc.neckbeard.tinypubsub;
package cc.neckbeard.tinyeventbus;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cc.neckbeard.tinypubsub;
package cc.neckbeard.tinyeventbus;

public interface Cancellable {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cc.neckbeard.tinypubsub;
package cc.neckbeard.tinyeventbus;

import net.jodah.typetools.TypeResolver;
import org.jetbrains.annotations.NotNull;
Expand All @@ -11,12 +11,32 @@ public class Sub<T> implements Comparable<Sub<T>> {
public final Class<?> type;
public final Consumer<T> consumer;

public Sub(int prio, Consumer<T> consumer) {
public Sub(Consumer<T> consumer, int prio) {
this.prio = prio;
this.type = TypeResolver.resolveRawArguments(Consumer.class, consumer.getClass())[0];
this.consumer = consumer;
}

public Sub(int prio, Consumer<T> consumer) {
this(consumer, 0);
}

public Sub(Consumer<T> consumer) {
this(consumer, 0);
}

public static <T> Sub<T> of(Consumer<T> consumer, int prio) {
return new Sub<>(consumer, prio);
}

public static <T> Sub<T> of(int prio, Consumer<T> consumer) {
return new Sub<>(consumer, prio);
}

public static <T> Sub<T> of(Consumer<T> consumer) {
return new Sub<>(consumer);
}

public void accept(Object o) {
//noinspection unchecked
consumer.accept((T) o);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cc.neckbeard.tinyeventbus.benchmark;

import cc.neckbeard.tinyeventbus.Bus;
import cc.neckbeard.tinyeventbus.Sub;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

@Execution(ExecutionMode.SAME_THREAD)
public class DelBenchmark {

private static final Bus bus = new Bus();

@Test
void benchmark() {

System.out.println("del");

final int subs = 1_000;
System.out.println("subs: " + subs);

List<Sub<String>> listenerContainers = new ArrayList<>();

IntStream.range(0, subs).forEach(i -> listenerContainers.add(new Sub<>(0, s -> {})));

IntStream
.range(0, subs)
.forEach(i -> bus.reg(listenerContainers.get(i)));

final long start = System.nanoTime();

IntStream
.range(0, subs)
.forEach(i -> bus.del(listenerContainers.get(i)));

final long end = System.nanoTime() - start;

System.out.printf("%,dns (%,dms)\n", end, end / 1000000);

}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package cc.neckbeard.tinypubsub.tests;
package cc.neckbeard.tinyeventbus.benchmark;

import cc.neckbeard.tinypubsub.Bus;
import cc.neckbeard.tinypubsub.Sub;
import cc.neckbeard.tinyeventbus.Bus;
import cc.neckbeard.tinyeventbus.Sub;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

@Execution(ExecutionMode.SAME_THREAD)
public class PubSubTest {
public class PubBenchmark {

private static final String event = "";

private static final Bus bus = new Bus();
private static int hits = 0;

@Test
void run() {
void benchmark() {

System.out.println("com.github.nothub TinyPubSub 8c9f424f85");
System.out.println("pub");

final int subs = 1_000;
final int pubs = 1_000;
Expand All @@ -30,20 +33,21 @@ void run() {

IntStream.range(0, subs).forEach(i -> listenerContainers.add(new Sub<>(0, s -> hits++)));

final long start = System.nanoTime();

IntStream
.range(0, subs)
.forEach(i -> bus.reg(listenerContainers.get(i)));

final long start = System.nanoTime();

IntStream
.range(0, pubs)
.forEach(i -> bus.pub(event));

final long end = System.nanoTime() - start;

System.out.println("hits: " + hits);
System.out.printf("%,dns (%,dms)", end, end / 1000000);
System.out.printf("%,dns (%,dms)\n", end, end / 1000000);

Assertions.assertEquals(subs * pubs, hits);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cc.neckbeard.tinyeventbus.benchmark;

import cc.neckbeard.tinyeventbus.Bus;
import cc.neckbeard.tinyeventbus.Sub;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

@Execution(ExecutionMode.SAME_THREAD)
public class RegBenchmark {

private static final Bus bus = new Bus();

@Test
void benchmark() {

System.out.println("reg");

final int subs = 1_000;
System.out.println("subs: " + subs);

List<Sub<String>> listenerContainers = new ArrayList<>();

IntStream.range(0, subs).forEach(i -> listenerContainers.add(new Sub<>(0, s -> {})));

final long start = System.nanoTime();

IntStream
.range(0, subs)
.forEach(i -> bus.reg(listenerContainers.get(i)));

final long end = System.nanoTime() - start;

System.out.printf("%,dns (%,dms)\n", end, end / 1000000);

}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package cc.neckbeard.tinypubsub.example;
package cc.neckbeard.tinyeventbus.example;

import cc.neckbeard.tinypubsub.Bus;
import cc.neckbeard.tinypubsub.Cancellable;
import cc.neckbeard.tinypubsub.Sub;
import cc.neckbeard.tinyeventbus.Bus;
import cc.neckbeard.tinyeventbus.Cancellable;
import cc.neckbeard.tinyeventbus.Sub;

public class Example {

private static String msg = "";

Sub<Event> first = new Sub<>(0, e -> msg = e.str);
Sub<Event> first = new Sub<>( e -> msg = e.str);

Sub<Event> second = new Sub<>(1, e -> {
if (e.str.equals("foobar")) e.cancelled = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cc.neckbeard.tinypubsub.tests;
package cc.neckbeard.tinyeventbus.tests;

class BooleanEvent {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cc.neckbeard.tinypubsub.tests;
package cc.neckbeard.tinyeventbus.tests;

import cc.neckbeard.tinypubsub.Bus;
import cc.neckbeard.tinypubsub.Sub;
import cc.neckbeard.tinyeventbus.Bus;
import cc.neckbeard.tinyeventbus.Sub;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
Expand All @@ -15,7 +15,7 @@ class InstanceTests {
private static Bus bus;
private static boolean invoked;

Sub<BooleanEvent> sub = new Sub<>(0, e -> invoked = e.value);
Sub<BooleanEvent> sub = Sub.of(e -> invoked = e.value);

@BeforeAll
static void setUp() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cc.neckbeard.tinypubsub.tests;
package cc.neckbeard.tinyeventbus.tests;

import cc.neckbeard.tinypubsub.Bus;
import cc.neckbeard.tinypubsub.Sub;
import cc.neckbeard.tinyeventbus.Bus;
import cc.neckbeard.tinyeventbus.Sub;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -14,9 +14,9 @@ class MultiTest {
private static Bus bus = new Bus();
private static int hits;

private final Sub<Object> sub = new Sub<>(0, o -> hits++);
private final Sub<Object> sub = new Sub<>(o -> hits++);

private static final Sub<Object> subStatic = new Sub<>(0, o -> hits++);
private static final Sub<Object> subStatic = Sub.of(o -> hits++);

@BeforeEach
void setUp() {
Expand All @@ -42,8 +42,8 @@ void regStatic() {

@Test
void regInline() {
bus.reg(new Sub<>(0, o -> hits++));
bus.reg(new Sub<>(0, o -> hits++));
bus.reg(new Sub<>(o -> hits++));
bus.reg(Sub.of(o -> hits++));
bus.pub(new Object());
Assertions.assertEquals(2, hits);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cc.neckbeard.tinypubsub.tests;
package cc.neckbeard.tinyeventbus.tests;

import cc.neckbeard.tinypubsub.Bus;
import cc.neckbeard.tinypubsub.Sub;
import cc.neckbeard.tinyeventbus.Bus;
import cc.neckbeard.tinyeventbus.Sub;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -11,19 +11,19 @@ class OrderTests {
private static Bus bus;
private static String str;

Sub<Object> subC1 = new Sub<>(e -> str += "C");
Sub<Object> subC2 = Sub.of(e -> str += "C");
Sub<Object> subE = new Sub<>(e -> str += "E", -2);
Sub<Object> subA = Sub.of(e -> str += "A", 2);
Sub<Object> subB = Sub.of(e -> str += "B", 1);
Sub<Object> subD = new Sub<>(e -> str += "D", -1);

@BeforeAll
static void setUp() {
bus = new Bus();
str = "";
}

Sub<Object> subC1 = new Sub<>(0, e -> str += "C");
Sub<Object> subC2 = new Sub<>(0, e -> str += "C");
Sub<Object> subE = new Sub<>(-2, e -> str += "E");
Sub<Object> subA = new Sub<>(2, e -> str += "A");
Sub<Object> subB = new Sub<>(1, e -> str += "B");
Sub<Object> subD = new Sub<>(-1, e -> str += "D");

@Test
void order() {
bus.reg(subC1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cc.neckbeard.tinypubsub.tests;
package cc.neckbeard.tinyeventbus.tests;

import cc.neckbeard.tinypubsub.Bus;
import cc.neckbeard.tinypubsub.Sub;
import cc.neckbeard.tinyeventbus.Bus;
import cc.neckbeard.tinyeventbus.Sub;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
Expand All @@ -15,7 +15,7 @@ class StaticTests {
private static Bus bus;
private static boolean invoked;

Sub<BooleanEvent> sub = new Sub<>(0, e -> invoked = e.value);
Sub<BooleanEvent> sub = new Sub<>(e -> invoked = e.value, 0);

@BeforeAll
static void setUp() {
Expand Down
Loading

0 comments on commit 895f80d

Please sign in to comment.