Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added perf tests for various container-like subscriptions #2583

Merged
merged 2 commits into from
Feb 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/main/java/rx/internal/util/SubscriptionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ public void add(final Subscription s) {
*/
@Override
public void unsubscribe() {
List<Subscription> list;
synchronized (this) {
if (unsubscribed) {
return;
}
unsubscribed = true;
list = subscriptions;
subscriptions = null;
}
// we will only get here once
unsubscribeFromAll(subscriptions);
subscriptions = null;
unsubscribeFromAll(list);
}

private static void unsubscribeFromAll(Collection<Subscription> subscriptions) {
Expand Down Expand Up @@ -119,4 +121,13 @@ private static void unsubscribeFromAll(Collection<Subscription> subscriptions) {
}
}
}
/* perf support */
public void clear() {
List<Subscription> list;
synchronized (this) {
list = subscriptions;
subscriptions = null;
}
unsubscribeFromAll(list);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package rx.subscriptions;

import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Group;
import org.openjdk.jmh.annotations.GroupThreads;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;

import rx.Subscription;

/**
* Benchmark typical composite subscription concurrent behavior.
* <p>
* gradlew benchmarks "-Pjmh=-f 1 -tu s -bm thrpt -wi 5 -i 5 -r 1 .*CompositeSubscriptionConcurrentPerf.*"
* <p>
* gradlew benchmarks "-Pjmh=-f 1 -tu ns -bm avgt -wi 5 -i 5 -r 1 .*CompositeSubscriptionConcurrentPerf.*"
*/
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@Threads(2)
@State(Scope.Group)
public class CompositeSubscriptionConcurrentPerf {
@Param({ "1", "1000", "100000" })
public int loop;

public final CompositeSubscription csub = new CompositeSubscription();
@Param({ "1", "5", "10", "20" })
public int count;

public Subscription[] values;
@Setup
public void setup() {
values = new Subscription[count * 2];
for (int i = 0; i < count * 2; i++) {
values[i] = new Subscription() {
@Override
public boolean isUnsubscribed() {
return false;
}
@Override
public void unsubscribe() {

}
};
}
}

@Group("g1")
@GroupThreads(1)
@Benchmark
public void addRemoveT1() {
CompositeSubscription csub = this.csub;
Subscription[] values = this.values;

for (int i = loop; i > 0; i--) {
for (int j = values.length - 1; j >= 0; j--) {
csub.add(values[j]);
}
for (int j = values.length - 1; j >= 0; j--) {
csub.remove(values[j]);
}
}
}
@Group("g1")
@GroupThreads(1)
@Benchmark
public void addRemoveT2() {
CompositeSubscription csub = this.csub;
Subscription[] values = this.values;

for (int i = loop; i > 0; i--) {
for (int j = values.length - 1; j >= 0; j--) {
csub.add(values[j]);
}
for (int j = values.length - 1; j >= 0; j--) {
csub.remove(values[j]);
}
}
}
@Group("g2")
@GroupThreads(1)
@Benchmark
public void addRemoveHalfT1() {
CompositeSubscription csub = this.csub;
Subscription[] values = this.values;
int n = values.length;

for (int i = loop; i > 0; i--) {
for (int j = n / 2 - 1; j >= 0; j--) {
csub.add(values[j]);
}
for (int j = n / 2 - 1; j >= 0; j--) {
csub.remove(values[j]);
}
}
}
@Group("g2")
@GroupThreads(1)
@Benchmark
public void addRemoveHalfT2() {
CompositeSubscription csub = this.csub;
Subscription[] values = this.values;
int n = values.length;

for (int i = loop; i > 0; i--) {
for (int j = n - 1; j >= n / 2; j--) {
csub.add(values[j]);
}
for (int j = n - 1; j >= n / 2; j--) {
csub.remove(values[j]);
}
}
}
@Group("g3")
@GroupThreads(1)
@Benchmark
public void addClearT1() {
CompositeSubscription csub = this.csub;
Subscription[] values = this.values;

for (int i = loop; i > 0; i--) {
for (int j = values.length - 1; j >= 0; j--) {
csub.add(values[j]);
}
csub.clear();
}
}
@Group("g3")
@GroupThreads(1)
@Benchmark
public void addClearT2() {
CompositeSubscription csub = this.csub;
Subscription[] values = this.values;

for (int i = loop; i > 0; i--) {
for (int j = values.length - 1; j >= 0; j--) {
csub.add(values[j]);
}
for (int j = values.length - 1; j >= 0; j--) {
csub.remove(values[j]);
}
}
}
@Group("g4")
@GroupThreads(1)
@Benchmark
public void addClearHalfT1() {
CompositeSubscription csub = this.csub;
Subscription[] values = this.values;
int n = values.length;

for (int i = loop; i > 0; i--) {
for (int j = n / 2 - 1; j >= 0; j--) {
csub.add(values[j]);
}
csub.clear();
}
}
@Group("g4")
@GroupThreads(1)
@Benchmark
public void addClearHalfT2() {
CompositeSubscription csub = this.csub;
Subscription[] values = this.values;
int n = values.length;

for (int i = loop; i > 0; i--) {
for (int j = n - 1; j >= n / 2; j--) {
csub.add(values[j]);
}
csub.clear();
}
}
}
118 changes: 118 additions & 0 deletions src/perf/java/rx/subscriptions/CompositeSubscriptionPerf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package rx.subscriptions;

import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;

import rx.Subscription;

/**
* Benchmark typical composite subscription single-threaded behavior.
* <p>
* gradlew benchmarks "-Pjmh=-f 1 -tu s -bm thrpt -wi 5 -i 5 -r 1 .*CompositeSubscriptionPerf.*"
* <p>
* gradlew benchmarks "-Pjmh=-f 1 -tu ns -bm avgt -wi 5 -i 5 -r 1 .*CompositeSubscriptionPerf.*"
*/
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public class CompositeSubscriptionPerf {
@State(Scope.Thread)
public static class TheState {
@Param({ "1", "1000", "100000" })
public int loop;
@Param({ "1", "5", "10", "100" })
public int count;

public final CompositeSubscription csub = new CompositeSubscription();

public Subscription[] values;
@Setup
public void setup() {
values = new Subscription[count];
for (int i = 0; i < count; i++) {
values[i] = new Subscription() {
@Override
public boolean isUnsubscribed() {
return false;
}
@Override
public void unsubscribe() {

}
};
}
}
}
@Benchmark
public void addRemove(TheState state) {
CompositeSubscription csub = state.csub;
Subscription[] values = state.values;

for (int i = state.loop; i > 0; i--) {
for (int j = values.length - 1; j >= 0; j--) {
csub.add(values[j]);
}
for (int j = values.length - 1; j >= 0; j--) {
csub.remove(values[j]);
}
}
}
@Benchmark
public void addRemoveLocal(TheState state, Blackhole bh) {
CompositeSubscription csub = new CompositeSubscription();
Subscription[] values = state.values;

for (int i = state.loop; i > 0; i--) {
for (int j = values.length - 1; j >= 0; j--) {
csub.add(values[j]);
}
for (int j = values.length - 1; j >= 0; j--) {
csub.remove(values[j]);
}
}

bh.consume(csub);
}
@Benchmark
public void addClear(TheState state) {
CompositeSubscription csub = state.csub;
Subscription[] values = state.values;

for (int i = state.loop; i > 0; i--) {
for (int j = values.length - 1; j >= 0; j--) {
csub.add(values[j]);
}
csub.clear();
}
}
@Benchmark
public void addClearLocal(TheState state, Blackhole bh) {
CompositeSubscription csub = new CompositeSubscription();
Subscription[] values = state.values;

for (int i = state.loop; i > 0; i--) {
for (int j = values.length - 1; j >= 0; j--) {
csub.add(values[j]);
}
csub.clear();
}
bh.consume(csub);
}
}
Loading