Skip to content

Instantly share code, notes, and snippets.

@codefromthecrypt
Created November 8, 2013 22:54
Show Gist options
  • Save codefromthecrypt/7378992 to your computer and use it in GitHub Desktop.
Save codefromthecrypt/7378992 to your computer and use it in GitHub Desktop.
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