Skip to content

Commit

Permalink
Performance Improvements for Azure Core (#12890)
Browse files Browse the repository at this point in the history
* Miscellaneous small perf investigations in azure-core in draft form.

* Fix HttpResponseBodyDecoder unit tests

* Revert error checking logic

* Fix some checkstyle issues and a test failure.

* Compile two frequently parsed regex's into a Pattern to save CPU time

* Hack in some code that tries to minimise the amount of times we serialize / deserialize to / from String, instead trying to use byte[] for input and ByteArrayOutputStream for output.

* Fix afterburner versioning

* Convert object to byte array

* Remove duplicate serializer methods

* Update references to old method

* Add afterburner module to xml mapper

* Reuse same Jackson ObjectMappers for all swagger interface methods in a single RestProxy instance

* Improve code that enables object mapper reuse

* Fix test and checkstyle issues

* Remove unused jsonwrapper code

* Fix unit tests

* Use HashMap instead of ConcurrentHashMap

* Remove references to jsonwrapper in module-info.java

* Fixing spotbugs / checkstyle issues.

* Rename one newly introduced SerializerAdapter method to avoid incompatibilities.

* More cleanup

* Some code cleanups based on review feedback

* Remove unused import

* Removing an exception I added

* Update code paths that were creating unnecessary container types

* Merge setScheme and setHost into a single API

* Fixes for failing tests

* Change interface defaults to fix ServiceBus test issues

* Fix linting issue

* Remove AfterBurner dependency from Azure Core

* Add unreleased version tag

* Add missing ;

* Update serializeRaw to use OutputStream serializer method

* Fix possible incorrect sizes

* Fix Form Recognizer tests that were failing

* Revert Form Recognizer version changes

* Fix CI

* Add accidentally deleted version tag

Co-authored-by: Srikanta <srnagar@microsoft.com>
Co-authored-by: alzimmermsft <48699787+alzimmermsft@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 20, 2020
1 parent 18cbeed commit 76fc7e8
Show file tree
Hide file tree
Showing 29 changed files with 639 additions and 375 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2115,7 +2115,7 @@
<Match>
<Package name="~com\.azure\.resourcemanager(\.[^.]+)*\.samples(\.[^.]+)*"/>
<Bug pattern="REC_CATCH_EXCEPTION,
RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE,
RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE,
RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
</Match>

Expand Down Expand Up @@ -2224,4 +2224,9 @@
<Bug pattern="UI_INHERITANCE_UNSAFE_GETRESOURCE"/>
</Or>
</Match>

<Match>
<Bug pattern="EI_EXPOSE_REP"/>
<Class name="com.azure.core.implementation.AccessibleByteArrayOutputStream"/>
</Match>
</FindBugsFilter>
1 change: 1 addition & 0 deletions eng/versioning/version_client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ com.microsoft.azure:spring-data-cosmosdb;3.0.0-beta.1;3.0.0-beta.1
# Format;
# unreleased_<groupId>:<artifactId>;dependency-version
# note: The unreleased dependencies will not be manipulated with the automatic PR creation code.
unreleased_com.azure:azure-core;1.8.0-beta.1
unreleased_com.azure:azure-messaging-servicebus;7.0.0-beta.5
unreleased_com.azure:azure-security-keyvault-keys;4.3.0-beta.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
Expand Down Expand Up @@ -439,7 +440,11 @@ public String aggregateAsString() {
}
}

return new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
try {
return outputStream.toString("UTF-8");
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private Mono<Map<String, String>> extractResponseData(final HttpResponse respons
bytesRead = gis.read(buffer, position, buffer.length);
}

content = new String(output.toByteArray(), StandardCharsets.UTF_8);
content = output.toString("UTF-8");
} catch (IOException e) {
throw logger.logExceptionAsWarning(Exceptions.propagate(e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* A collection of headers on an HTTP request or response.
*/
public class HttpHeaders implements Iterable<HttpHeader> {
private final Map<String, HttpHeader> headers = new ConcurrentHashMap<>();
private final Map<String, HttpHeader> headers = new HashMap<>();

/**
* Create an empty HttpHeaders instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import reactor.core.publisher.Mono;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Locale;
import java.util.Set;
Expand Down Expand Up @@ -138,7 +138,7 @@ private Mono<Void> logRequest(final ClientLogger logger, final HttpRequest reque
.append("-byte body:")
.append(System.lineSeparator())
.append(prettyPrintIfNeeded(logger, contentType,
new String(outputStream.toByteArray(), StandardCharsets.UTF_8)))
convertStreamToString(outputStream, logger)))
.append(System.lineSeparator())
.append("--> END ")
.append(request.getHttpMethod())
Expand Down Expand Up @@ -217,7 +217,7 @@ private Mono<HttpResponse> logResponse(final ClientLogger logger, final HttpResp
responseLogMessage.append("Response body:")
.append(System.lineSeparator())
.append(prettyPrintIfNeeded(logger, contentTypeHeader,
new String(outputStream.toByteArray(), StandardCharsets.UTF_8)))
convertStreamToString(outputStream, logger)))
.append(System.lineSeparator())
.append("<-- END HTTP");

Expand Down Expand Up @@ -372,4 +372,15 @@ private boolean shouldBodyBeLogged(String contentTypeHeader, long contentLength)
&& contentLength != 0
&& contentLength < MAX_BODY_LOG_SIZE;
}

/*
* Helper function which converts a ByteArrayOutputStream to a String without duplicating the internal buffer.
*/
private static String convertStreamToString(ByteArrayOutputStream stream, ClientLogger logger) {
try {
return stream.toString("UTF-8");
} catch (UnsupportedEncodingException ex) {
throw logger.logExceptionAsError(new RuntimeException(ex));
}
}
}
Loading

0 comments on commit 76fc7e8

Please sign in to comment.