提交 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;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import retrofit.Profiler.RequestInformation;
import retrofit.client.Client;
import retrofit.client.Header;
import retrofit.client.Request;
......@@ -154,21 +153,19 @@ public class RestAdapter {
final ErrorHandler errorHandler;
private final Client client;
private final Profiler profiler;
private RxSupport rxSupport;
volatile LogLevel logLevel;
private RestAdapter(Endpoint server, Client client, Executor httpExecutor,
Executor callbackExecutor, RequestInterceptor requestInterceptor, Converter converter,
Profiler profiler, ErrorHandler errorHandler, Log log, LogLevel logLevel) {
ErrorHandler errorHandler, Log log, LogLevel logLevel) {
this.server = server;
this.client = client;
this.httpExecutor = httpExecutor;
this.callbackExecutor = callbackExecutor;
this.requestInterceptor = requestInterceptor;
this.converter = converter;
this.profiler = profiler;
this.errorHandler = errorHandler;
this.log = log;
this.logLevel = logLevel;
......@@ -312,22 +309,10 @@ public class RestAdapter {
request = logAndReplaceRequest("HTTP", request, args);
}
Object profilerObject = null;
if (profiler != null) {
profilerObject = profiler.beforeCall();
}
long start = System.nanoTime();
Response response = client.execute(request);
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()) {
// Log the response data.
response = logAndReplaceResponse(url, response, elapsedTime);
......@@ -335,6 +320,7 @@ public class RestAdapter {
Type type = methodInfo.responseObjectType;
int statusCode = response.getStatus();
if (statusCode >= 200 && statusCode < 300) { // 2XX == successful request
// Caller requested the raw Response object directly.
if (type.equals(Response.class)) {
......@@ -509,21 +495,6 @@ public class RestAdapter {
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}.
* <p>
......@@ -537,7 +508,6 @@ public class RestAdapter {
private Executor callbackExecutor;
private RequestInterceptor requestInterceptor;
private Converter converter;
private Profiler profiler;
private ErrorHandler errorHandler;
private Log log;
private LogLevel logLevel = LogLevel.NONE;
......@@ -607,15 +577,6 @@ public class RestAdapter {
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
* synchronous requests.
......@@ -653,7 +614,7 @@ public class RestAdapter {
}
ensureSaneDefaults();
return new RestAdapter(endpoint, client, httpExecutor, callbackExecutor,
requestInterceptor, converter, profiler, errorHandler, log, logLevel);
requestInterceptor, converter, errorHandler, log, logLevel);
}
private void ensureSaneDefaults() {
......
......@@ -34,7 +34,6 @@ import rx.functions.Action1;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.same;
import static org.mockito.Mockito.atLeastOnce;
......@@ -44,7 +43,6 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static retrofit.Profiler.RequestInformation;
import static retrofit.RestAdapter.LogLevel.BASIC;
import static retrofit.RestAdapter.LogLevel.FULL;
import static retrofit.RestAdapter.LogLevel.HEADERS;
......@@ -90,7 +88,6 @@ public class RestAdapterTest {
private Client mockClient;
private Executor mockRequestExecutor;
private Executor mockCallbackExecutor;
private Profiler<Object> mockProfiler;
private Example example;
@SuppressWarnings("unchecked") // Mock profiler type erasure.
......@@ -98,13 +95,11 @@ public class RestAdapterTest {
mockClient = mock(Client.class);
mockRequestExecutor = spy(new SynchronousExecutor());
mockCallbackExecutor = spy(new SynchronousExecutor());
mockProfiler = mock(Profiler.class);
example = new RestAdapter.Builder() //
.setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.build()
.create(Example.class);
}
......@@ -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 {
final List<String> logMessages = new ArrayList<String>();
RestAdapter.Log log = new RestAdapter.Log() {
......@@ -149,7 +131,6 @@ public class RestAdapterTest {
.setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log)
.setLogLevel(BASIC)
.build()
......@@ -176,7 +157,6 @@ public class RestAdapterTest {
.setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log)
.setLogLevel(HEADERS)
.build()
......@@ -208,7 +188,6 @@ public class RestAdapterTest {
.setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log)
.setLogLevel(HEADERS_AND_ARGS)
.build()
......@@ -242,7 +221,6 @@ public class RestAdapterTest {
.setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log)
.setLogLevel(FULL)
.build()
......@@ -280,7 +258,6 @@ public class RestAdapterTest {
.setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log)
.setLogLevel(HEADERS_AND_ARGS)
.build()
......@@ -319,7 +296,6 @@ public class RestAdapterTest {
.setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log)
.setLogLevel(FULL)
.build()
......@@ -351,7 +327,6 @@ public class RestAdapterTest {
.setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log)
.setLogLevel(HEADERS_AND_ARGS)
.build()
......@@ -394,7 +369,6 @@ public class RestAdapterTest {
.setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log)
.setLogLevel(FULL)
.build()
......@@ -479,7 +453,6 @@ public class RestAdapterTest {
.setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log)
.setLogLevel(FULL)
.build()
......@@ -521,7 +494,6 @@ public class RestAdapterTest {
.setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(log)
.setLogLevel(FULL)
.build()
......@@ -582,9 +554,9 @@ public class RestAdapterTest {
}
}
@Test public void unexpectedExceptionThrows() {
@Test public void unexpectedExceptionThrows() throws IOException {
RuntimeException exception = new RuntimeException("More breakage.");
when(mockProfiler.beforeCall()).thenThrow(exception);
when(mockClient.execute(any(Request.class))).thenThrow(exception);
try {
example.something();
......@@ -631,7 +603,6 @@ public class RestAdapterTest {
.setClient(mockClient)
.setExecutors(mockRequestExecutor, mockCallbackExecutor)
.setEndpoint("http://example.com")
.setProfiler(mockProfiler)
.setLog(RestAdapter.Log.NONE)
.setLogLevel(FULL)
.build()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册