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

Constructor allocation #855

Merged
merged 2 commits into from
Aug 7, 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
4 changes: 2 additions & 2 deletions hystrix-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ jar {
jmh {
fork = 10
iterations = 3
jmhVersion = '1.9.3'
//profilers = ['gc']
jmhVersion = '1.10.3'
profilers = ['gc']
threads = 8
warmup = '1s'
warmupBatchSize = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/**
* Copyright 2015 Netflix, Inc.
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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 com.netflix.hystrix.perf;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPool;
import com.netflix.hystrix.HystrixThreadPoolKey;
import com.netflix.hystrix.HystrixThreadPoolProperties;
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.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import rx.Observable;
import rx.schedulers.Schedulers;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class CommandGroupKeyExecutionPerfTest {

@State(Scope.Thread)
public static class CommandState {
HystrixCommand<Integer> command;

static class TestCommand extends HystrixCommand<Integer> {
TestCommand() {
super(HystrixCommandGroupKey.Factory.asKey("PERF"));
}

@Override
protected Integer run() throws Exception {
return 1;
}

@Override
protected Integer getFallback() {
return 2;
}
}

@Setup(Level.Invocation)
public void setUp() {
command = new TestCommand();
}
}

@State(Scope.Benchmark)
public static class ExecutorState {
ExecutorService executorService;

@Setup
public void setUp() {
executorService = Executors.newFixedThreadPool(100);
}

@TearDown
public void tearDown() {
List<Runnable> runnables = executorService.shutdownNow();
}
}

@State(Scope.Benchmark)
public static class ThreadPoolState {
HystrixThreadPool hystrixThreadPool;

@Setup
public void setUp() {
hystrixThreadPool = new HystrixThreadPool.HystrixThreadPoolDefault(
HystrixThreadPoolKey.Factory.asKey("PERF")
, HystrixThreadPoolProperties.Setter().withCoreSize(100));
}

@TearDown
public void tearDown() {
hystrixThreadPool.getExecutor().shutdownNow();
}
}


@Benchmark
@BenchmarkMode({Mode.Throughput})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public Integer baselineExecute() {
return 1;
}

@Benchmark
@BenchmarkMode({Mode.Throughput})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public Integer baselineQueue(ExecutorState state) throws InterruptedException, ExecutionException{
try {
return state.executorService.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return 1;
}
}).get();
} catch (Throwable t) {
return 2;
}
}

@Benchmark
@BenchmarkMode({Mode.Throughput})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public Integer baselineSyncObserve() throws InterruptedException {
Observable<Integer> syncObservable = Observable.just(1);

try {
return syncObservable.toBlocking().first();
} catch (Throwable t) {
return 2;
}
}

@Benchmark
@BenchmarkMode({Mode.Throughput})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public Integer baselineAsyncComputationObserve() throws InterruptedException {
Observable<Integer> asyncObservable = Observable.just(1).subscribeOn(Schedulers.computation());

try {
return asyncObservable.toBlocking().first();
} catch (Throwable t) {
return 2;
}
}

@Benchmark
@BenchmarkMode({Mode.Throughput})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public Integer baselineAsyncCustomThreadPoolObserve(ThreadPoolState state) {
Observable<Integer> asyncObservable = Observable.just(1).subscribeOn(state.hystrixThreadPool.getScheduler());
try {
return asyncObservable.toBlocking().first();
} catch (Throwable t) {
return 2;
}
}

@Benchmark
@BenchmarkMode({Mode.Throughput})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public Integer hystrixExecute(CommandState state) {
return state.command.execute();
}

@Benchmark
@BenchmarkMode({Mode.Throughput})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public Integer hystrixQueue(CommandState state) {
try {
return state.command.queue().get();
} catch (Throwable t) {
return 2;
}
}

@Benchmark
@BenchmarkMode({Mode.Throughput})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public Integer hystrixObserve(CommandState state) {
return state.command.observe().toBlocking().first();
}

@Benchmark
@BenchmarkMode({Mode.Throughput})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public Integer hystrixToObservable(CommandState state) {
return state.command.toObservable().toBlocking().first();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class CommandExecutionPerfTest {
public class CommandSetterExecutionPerfTest {

@State(Scope.Thread)
public static class CommandState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.netflix.hystrix;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

Expand Down Expand Up @@ -52,8 +53,7 @@ public abstract class HystrixCommand<R> extends AbstractCommand<R> implements Hy
* common business purpose etc.
*/
protected HystrixCommand(HystrixCommandGroupKey group) {
// use 'null' to specify use the default
this(new Setter(group));
super(group, null, null, null, null, null, null, null, null, null, null, null);
}


Expand All @@ -71,7 +71,7 @@ protected HystrixCommand(HystrixCommandGroupKey group) {
* {@link HystrixThreadPoolKey} used to identify the thread pool in which a {@link HystrixCommand} executes.
*/
protected HystrixCommand(HystrixCommandGroupKey group, HystrixThreadPoolKey threadPool) {
this(new Setter(group).andThreadPoolKey(threadPool));
super(group, null, threadPool, null, null, null, null, null, null, null, null, null);
}

/**
Expand All @@ -88,7 +88,7 @@ protected HystrixCommand(HystrixCommandGroupKey group, HystrixThreadPoolKey thre
* Time in milliseconds at which point the calling thread will timeout (using {@link Future#get}) and walk away from the executing thread.
*/
protected HystrixCommand(HystrixCommandGroupKey group, int executionIsolationThreadTimeoutInMilliseconds) {
this(new Setter(group).andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(executionIsolationThreadTimeoutInMilliseconds)));
super(group, null, null, null, null, HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(executionIsolationThreadTimeoutInMilliseconds), null, null, null, null, null, null);
}

/**
Expand All @@ -102,12 +102,12 @@ protected HystrixCommand(HystrixCommandGroupKey group, int executionIsolationThr
* The {@link HystrixCommandGroupKey} is used to represent a common relationship between commands. For example, a library or team name, the system all related commands interact with,
* common business purpose etc.
* @param threadPool
* {@link HystrixThreadPoolKey} used to identify the thread pool in which a {@link HystrixCommand} executes.
* {@link HystrixThreadPool} used to identify the thread pool in which a {@link HystrixCommand} executes.
* @param executionIsolationThreadTimeoutInMilliseconds
* Time in milliseconds at which point the calling thread will timeout (using {@link Future#get}) and walk away from the executing thread.
*/
protected HystrixCommand(HystrixCommandGroupKey group, HystrixThreadPoolKey threadPool, int executionIsolationThreadTimeoutInMilliseconds) {
this(new Setter(group).andThreadPoolKey(threadPool).andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(executionIsolationThreadTimeoutInMilliseconds)));
super(group, null, threadPool, null, null, HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(executionIsolationThreadTimeoutInMilliseconds), null, null, null, null, null, null);
}

/**
Expand Down