提交 b9ee320f 编写于 作者: J Jake Wharton

Remove profiler.

This will be replaced by a generalized interceptor system.
上级 bbd58f07
/*
* Copyright (C) 2012 Square, 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 retrofit;
/**
* A hook allowing clients to log HTTP method times and response status codes.
*
* @author Eric Burke (eric@squareup.com)
*/
public interface Profiler<T> {
/**
* Invoked before an HTTP method call. The object returned by this method will be
* passed to {@link #afterCall} when the call returns.
* <p>
* This method gives implementers the opportunity to include information that may
* change during the server call in {@code afterCall} logic.
*/
T beforeCall();
/**
* Invoked after an HTTP method completes. This is called from the
* RestAdapter's background thread.
*
* @param requestInfo information about the originating HTTP request.
* @param elapsedTime time in milliseconds it took the HTTP request to complete.
* @param statusCode response status code.
* @param beforeCallData the data returned by the corresponding {@link #beforeCall()}.
*/
void afterCall(RequestInformation requestInfo, long elapsedTime, int statusCode,
T beforeCallData);
/** Information about the HTTP request. */
public static final class RequestInformation {
private final String method;
private final String baseUrl;
private final String relativePath;
private final long contentLength;
private final String contentType;
public RequestInformation(String method, String baseUrl, String relativePath,
long contentLength, String contentType) {
this.method = method;
this.baseUrl = baseUrl;
this.relativePath = relativePath;
this.contentLength = contentLength;
this.contentType = contentType;
}
/** Returns the HTTP method of the originating request. */
public String getMethod() {
return method;
}
/** Returns the URL to which the originating request was sent. */
public String getBaseUrl() {
return baseUrl;
}
/** Returns the path relative to the base URL to which the originating request was sent. */
public String getRelativePath() {
return relativePath;
}
/** Returns the number of bytes in the originating request. */
public long getContentLength() {
return contentLength;
}
/** Returns the content type header value of the originating request. */
public String getContentType() {
return contentType;
}
}
}
...@@ -26,7 +26,6 @@ import java.util.LinkedHashMap; ...@@ -26,7 +26,6 @@ import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import retrofit.Profiler.RequestInformation;
import retrofit.client.Client; import retrofit.client.Client;
import retrofit.client.Header; import retrofit.client.Header;
import retrofit.client.Request; import retrofit.client.Request;
...@@ -154,21 +153,19 @@ public class RestAdapter { ...@@ -154,21 +153,19 @@ public class RestAdapter {
final ErrorHandler errorHandler; final ErrorHandler errorHandler;
private final Client client; private final Client client;
private final Profiler profiler;
private RxSupport rxSupport; private RxSupport rxSupport;
volatile LogLevel logLevel; volatile LogLevel logLevel;
private RestAdapter(Endpoint server, Client client, Executor httpExecutor, private RestAdapter(Endpoint server, Client client, Executor httpExecutor,
Executor callbackExecutor, RequestInterceptor requestInterceptor, Converter converter, Executor callbackExecutor, RequestInterceptor requestInterceptor, Converter converter,
Profiler profiler, ErrorHandler errorHandler, Log log, LogLevel logLevel) { ErrorHandler errorHandler, Log log, LogLevel logLevel) {
this.server = server; this.server = server;
this.client = client; this.client = client;
this.httpExecutor = httpExecutor; this.httpExecutor = httpExecutor;
this.callbackExecutor = callbackExecutor; this.callbackExecutor = callbackExecutor;
this.requestInterceptor = requestInterceptor; this.requestInterceptor = requestInterceptor;
this.converter = converter; this.converter = converter;
this.profiler = profiler;
this.errorHandler = errorHandler; this.errorHandler = errorHandler;
this.log = log; this.log = log;
this.logLevel = logLevel; this.logLevel = logLevel;
...@@ -312,22 +309,10 @@ public class RestAdapter { ...@@ -312,22 +309,10 @@ public class RestAdapter {
request = logAndReplaceRequest("HTTP", request, args); request = logAndReplaceRequest("HTTP", request, args);
} }
Object profilerObject = null;
if (profiler != null) {
profilerObject = profiler.beforeCall();
}
long start = System.nanoTime(); long start = System.nanoTime();
Response response = client.execute(request); Response response = client.execute(request);
long elapsedTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); long elapsedTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
int statusCode = response.getStatus();
if (profiler != null) {
RequestInformation requestInfo = getRequestInfo(serverUrl, methodInfo, request);
//noinspection unchecked
profiler.afterCall(requestInfo, elapsedTime, statusCode, profilerObject);
}
if (logLevel.log()) { if (logLevel.log()) {
// Log the response data. // Log the response data.
response = logAndReplaceResponse(url, response, elapsedTime); response = logAndReplaceResponse(url, response, elapsedTime);
...@@ -335,6 +320,7 @@ public class RestAdapter { ...@@ -335,6 +320,7 @@ public class RestAdapter {
Type type = methodInfo.responseObjectType; Type type = methodInfo.responseObjectType;
int statusCode = response.getStatus();
if (statusCode >= 200 && statusCode < 300) { // 2XX == successful request if (statusCode >= 200 && statusCode < 300) { // 2XX == successful request
// Caller requested the raw Response object directly. // Caller requested the raw Response object directly.
if (type.equals(Response.class)) { if (type.equals(Response.class)) {
...@@ -509,21 +495,6 @@ public class RestAdapter { ...@@ -509,21 +495,6 @@ public class RestAdapter {
log.log("---- END ERROR"); log.log("---- END ERROR");
} }
private static Profiler.RequestInformation getRequestInfo(String serverUrl,
RestMethodInfo methodDetails, Request request) {
long contentLength = 0;
String contentType = null;
TypedOutput body = request.getBody();
if (body != null) {
contentLength = body.length();
contentType = body.mimeType();
}
return new Profiler.RequestInformation(methodDetails.requestMethod, serverUrl,
methodDetails.requestUrl, contentLength, contentType);
}
/** /**
* Build a new {@link RestAdapter}. * Build a new {@link RestAdapter}.
* <p> * <p>
...@@ -537,7 +508,6 @@ public class RestAdapter { ...@@ -537,7 +508,6 @@ public class RestAdapter {
private Executor callbackExecutor; private Executor callbackExecutor;
private RequestInterceptor requestInterceptor; private RequestInterceptor requestInterceptor;
private Converter converter; private Converter converter;
private Profiler profiler;
private ErrorHandler errorHandler; private ErrorHandler errorHandler;
private Log log; private Log log;
private LogLevel logLevel = LogLevel.NONE; private LogLevel logLevel = LogLevel.NONE;
...@@ -607,15 +577,6 @@ public class RestAdapter { ...@@ -607,15 +577,6 @@ public class RestAdapter {
return this; return this;
} }
/** Set the profiler used to measure requests. */
public Builder setProfiler(Profiler profiler) {
if (profiler == null) {
throw new NullPointerException("Profiler may not be null.");
}
this.profiler = profiler;
return this;
}
/** /**
* The error handler allows you to customize the type of exception thrown for errors on * The error handler allows you to customize the type of exception thrown for errors on
* synchronous requests. * synchronous requests.
...@@ -653,7 +614,7 @@ public class RestAdapter { ...@@ -653,7 +614,7 @@ public class RestAdapter {
} }
ensureSaneDefaults(); ensureSaneDefaults();
return new RestAdapter(endpoint, client, httpExecutor, callbackExecutor, return new RestAdapter(endpoint, client, httpExecutor, callbackExecutor,
requestInterceptor, converter, profiler, errorHandler, log, logLevel); requestInterceptor, converter, errorHandler, log, logLevel);
} }
private void ensureSaneDefaults() { private void ensureSaneDefaults() {
......
...@@ -34,7 +34,6 @@ import rx.functions.Action1; ...@@ -34,7 +34,6 @@ import rx.functions.Action1;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail; import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.same; import static org.mockito.Matchers.same;
import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.atLeastOnce;
...@@ -44,7 +43,6 @@ import static org.mockito.Mockito.spy; ...@@ -44,7 +43,6 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static retrofit.Profiler.RequestInformation;
import static retrofit.RestAdapter.LogLevel.BASIC; import static retrofit.RestAdapter.LogLevel.BASIC;
import static retrofit.RestAdapter.LogLevel.FULL; import static retrofit.RestAdapter.LogLevel.FULL;
import static retrofit.RestAdapter.LogLevel.HEADERS; import static retrofit.RestAdapter.LogLevel.HEADERS;
...@@ -90,7 +88,6 @@ public class RestAdapterTest { ...@@ -90,7 +88,6 @@ public class RestAdapterTest {
private Client mockClient; private Client mockClient;
private Executor mockRequestExecutor; private Executor mockRequestExecutor;
private Executor mockCallbackExecutor; private Executor mockCallbackExecutor;
private Profiler<Object> mockProfiler;
private Example example; private Example example;
@SuppressWarnings("unchecked") // Mock profiler type erasure. @SuppressWarnings("unchecked") // Mock profiler type erasure.
...@@ -98,13 +95,11 @@ public class RestAdapterTest { ...@@ -98,13 +95,11 @@ public class RestAdapterTest {
mockClient = mock(Client.class); mockClient = mock(Client.class);
mockRequestExecutor = spy(new SynchronousExecutor()); mockRequestExecutor = spy(new SynchronousExecutor());
mockCallbackExecutor = spy(new SynchronousExecutor()); mockCallbackExecutor = spy(new SynchronousExecutor());
mockProfiler = mock(Profiler.class);
example = new RestAdapter.Builder() // example = new RestAdapter.Builder() //
.setClient(mockClient) .setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor) .setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com") .setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.build() .build()
.create(Example.class); .create(Example.class);
} }
...@@ -124,19 +119,6 @@ public class RestAdapterTest { ...@@ -124,19 +119,6 @@ public class RestAdapterTest {
} }
} }
@Test public void profilerObjectPassThrough() throws Exception {
Object data = new Object();
when(mockProfiler.beforeCall()).thenReturn(data);
when(mockClient.execute(any(Request.class))) //
.thenReturn(new Response("http://example.com/", 200, "OK", NO_HEADERS, new TypedString("Hey")));
example.something();
verify(mockProfiler).beforeCall();
verify(mockClient).execute(any(Request.class));
verify(mockProfiler).afterCall(any(RequestInformation.class), anyInt(), eq(200), same(data));
}
@Test public void logRequestResponseBasic() throws Exception { @Test public void logRequestResponseBasic() throws Exception {
final List<String> logMessages = new ArrayList<String>(); final List<String> logMessages = new ArrayList<String>();
RestAdapter.Log log = new RestAdapter.Log() { RestAdapter.Log log = new RestAdapter.Log() {
...@@ -149,7 +131,6 @@ public class RestAdapterTest { ...@@ -149,7 +131,6 @@ public class RestAdapterTest {
.setClient(mockClient) .setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor) .setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com") .setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log) .setLog(log)
.setLogLevel(BASIC) .setLogLevel(BASIC)
.build() .build()
...@@ -176,7 +157,6 @@ public class RestAdapterTest { ...@@ -176,7 +157,6 @@ public class RestAdapterTest {
.setClient(mockClient) .setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor) .setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com") .setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log) .setLog(log)
.setLogLevel(HEADERS) .setLogLevel(HEADERS)
.build() .build()
...@@ -208,7 +188,6 @@ public class RestAdapterTest { ...@@ -208,7 +188,6 @@ public class RestAdapterTest {
.setClient(mockClient) .setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor) .setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com") .setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log) .setLog(log)
.setLogLevel(HEADERS_AND_ARGS) .setLogLevel(HEADERS_AND_ARGS)
.build() .build()
...@@ -242,7 +221,6 @@ public class RestAdapterTest { ...@@ -242,7 +221,6 @@ public class RestAdapterTest {
.setClient(mockClient) .setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor) .setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com") .setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log) .setLog(log)
.setLogLevel(FULL) .setLogLevel(FULL)
.build() .build()
...@@ -280,7 +258,6 @@ public class RestAdapterTest { ...@@ -280,7 +258,6 @@ public class RestAdapterTest {
.setClient(mockClient) .setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor) .setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com") .setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log) .setLog(log)
.setLogLevel(HEADERS_AND_ARGS) .setLogLevel(HEADERS_AND_ARGS)
.build() .build()
...@@ -319,7 +296,6 @@ public class RestAdapterTest { ...@@ -319,7 +296,6 @@ public class RestAdapterTest {
.setClient(mockClient) .setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor) .setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com") .setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log) .setLog(log)
.setLogLevel(FULL) .setLogLevel(FULL)
.build() .build()
...@@ -351,7 +327,6 @@ public class RestAdapterTest { ...@@ -351,7 +327,6 @@ public class RestAdapterTest {
.setClient(mockClient) .setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor) .setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com") .setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log) .setLog(log)
.setLogLevel(HEADERS_AND_ARGS) .setLogLevel(HEADERS_AND_ARGS)
.build() .build()
...@@ -394,7 +369,6 @@ public class RestAdapterTest { ...@@ -394,7 +369,6 @@ public class RestAdapterTest {
.setClient(mockClient) .setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor) .setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com") .setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log) .setLog(log)
.setLogLevel(FULL) .setLogLevel(FULL)
.build() .build()
...@@ -479,7 +453,6 @@ public class RestAdapterTest { ...@@ -479,7 +453,6 @@ public class RestAdapterTest {
.setClient(mockClient) .setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor) .setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com") .setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log) .setLog(log)
.setLogLevel(FULL) .setLogLevel(FULL)
.build() .build()
...@@ -521,7 +494,6 @@ public class RestAdapterTest { ...@@ -521,7 +494,6 @@ public class RestAdapterTest {
.setClient(mockClient) .setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor) .setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com") .setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log) .setLog(log)
.setLogLevel(FULL) .setLogLevel(FULL)
.build() .build()
...@@ -582,9 +554,9 @@ public class RestAdapterTest { ...@@ -582,9 +554,9 @@ public class RestAdapterTest {
} }
} }
@Test public void unexpectedExceptionThrows() { @Test public void unexpectedExceptionThrows() throws IOException {
RuntimeException exception = new RuntimeException("More breakage."); RuntimeException exception = new RuntimeException("More breakage.");
when(mockProfiler.beforeCall()).thenThrow(exception); when(mockClient.execute(any(Request.class))).thenThrow(exception);
try { try {
example.something(); example.something();
...@@ -631,7 +603,6 @@ public class RestAdapterTest { ...@@ -631,7 +603,6 @@ public class RestAdapterTest {
.setClient(mockClient) .setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor) .setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com") .setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(RestAdapter.Log.NONE) .setLog(RestAdapter.Log.NONE)
.setLogLevel(FULL) .setLogLevel(FULL)
.build() .build()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册