Created
November 8, 2013 22:54
-
-
Save codefromthecrypt/7378992 to your computer and use it in GitHub Desktop.
retrofit caliper test w/ 1.2.2 https://microbenchmarks.appspot.com/runs/05515427-5bcc-4f37-b2b9-690f011d9d0f#r:scenario.benchmarkSpec.methodName
w/ 1.2.3-SNAPSHOT https://microbenchmarks.appspot.com/runs/5d67625f-2846-4cd4-b668-e497cabe1c2f#r:scenario.benchmarkSpec.methodName
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
package playground.caliper; | |
import com.google.caliper.Benchmark; | |
import com.google.common.base.Charsets; | |
import java.io.IOException; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.concurrent.Executor; | |
import retrofit.RestAdapter; | |
import retrofit.client.Client; | |
import retrofit.client.Header; | |
import retrofit.client.Request; | |
import retrofit.client.Response; | |
import retrofit.http.GET; | |
import retrofit.http.Path; | |
import retrofit.mime.TypedByteArray; | |
import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor; | |
/** tests the relative performance of wiring vs usage w/o taking into account network. */ | |
public class RetrofitBench { | |
private static final String API_URL = "https://api.github.com"; | |
static class Contributor { | |
String login; | |
int contributions; | |
} | |
interface GitHub { | |
@GET("/repos/{owner}/{repo}/contributors") | |
List<Contributor> contributors(@Path("owner") String owner, @Path("repo") String repo); | |
} | |
@Benchmark public int initializeRetrofit(int reps) { | |
int hashCode = 0; | |
for (int i = 0; i < reps; ++i) { | |
hashCode |= retrofitGitHub().hashCode(); | |
} | |
return hashCode; | |
} | |
@Benchmark public int useRetrofit(int reps) { | |
int size = 0; | |
for (int i = 0; i < reps; ++i) { | |
size |= retrofitGitHub.contributors("foo", "bar").size(); | |
} | |
return size; | |
} | |
private final GitHub retrofitGitHub = retrofitGitHub(); | |
private static GitHub retrofitGitHub() { | |
return new RestAdapter.Builder() // | |
.setServer(API_URL) // | |
.setClient(RetrofitClient.INSTANCE) // | |
.setExecutors(SAME_THREAD, SAME_THREAD) // | |
.build().create(GitHub.class); | |
} | |
private final static Executor SAME_THREAD = sameThreadExecutor(); | |
private static enum RetrofitClient implements Client { | |
INSTANCE; | |
@Override public Response execute(Request request) throws IOException { | |
return retrofitResponse; | |
} | |
} | |
private static final Response retrofitResponse = | |
new Response(200, "OK", Collections.<Header>emptyList(), | |
new TypedByteArray("application/json", "[]".getBytes(Charsets.UTF_8))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment