Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core-http(s)] Reuse DefaultHttpClient between ServiceClient instances #12966

Merged
4 commits merged into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions sdk/core/core-client/src/httpClientCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { HttpsClient, DefaultHttpsClient } from "@azure/core-https";

let cachedHttpsClient: HttpsClient | undefined;

export function getCachedDefaultHttpsClient(): HttpsClient {
if (!cachedHttpsClient) {
cachedHttpsClient = new DefaultHttpsClient();
}

return cachedHttpsClient;
}
4 changes: 2 additions & 2 deletions sdk/core/core-client/src/serviceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import { TokenCredential } from "@azure/core-auth";
import {
DefaultHttpsClient,
HttpsClient,
PipelineRequest,
PipelineResponse,
Expand All @@ -29,6 +28,7 @@ import { isPrimitiveType } from "./utils";
import { deserializationPolicy, DeserializationPolicyOptions } from "./deserializationPolicy";
import { URL } from "./url";
import { serializationPolicy, serializationPolicyOptions } from "./serializationPolicy";
import { getCachedDefaultHttpsClient } from "./httpClientCache";

/**
* Options to be provided while creating the client.
Expand Down Expand Up @@ -104,7 +104,7 @@ export class ServiceClient {
constructor(options: ServiceClientOptions = {}) {
this._requestContentType = options.requestContentType;
this._baseUri = options.baseUri;
this._httpsClient = options.httpsClient || new DefaultHttpsClient();
this._httpsClient = options.httpsClient || getCachedDefaultHttpsClient();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't see any perceivable difference in performance when I ran the tests at 6bb44f9 with smaller durations such as 10, 30, 60 seconds.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you compare the results before and after the PR? After the PR I expected all 3 scenarios to have similar throughput, but before the PR I expect the scenario which creates a new client for each request to be much slower and it might even crash due to client port exhaustion.

Copy link
Member

@HarshaNalluru HarshaNalluru Jan 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Discussed Offline, mentioning here for visibility]

Those numbers are for "before this PR".
One more thing I realized is that the storage libraries rely on the caching already, which might have been the reason that I didn't see any difference.
Right now, I'm updating the new tests to use core-http client directly to be able to test the fix.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update:
There is a four fold improvement in the "new client per request" case in terms of the number of operations per second on my local machine.

Before the fix Size (bytes) Parallel Duration Warmup(Ops/sec) Iter 1(Ops/sec) Iter 2(Ops/sec) Iter 3(Ops/sec)
CoreHTTPSingleClientTest 10240 16 30 139.71 163.10 165.10 163.53
CoreHTTPMultiClientsInParallelTest 10240 16 30 159.72 161.41 162.01 161.63
CoreHTTPNewClientPerReqTest 10240 16 30 39.39 39.74 39.85 39.67
After the fix Size (bytes) Parallel Duration Warmup(Ops/sec) Iter 1(Ops/sec) Iter 2(Ops/sec) Iter 3(Ops/sec)
CoreHTTPSingleClientTest 10240 16 30 149.76 159.63 157.00 159.11
CoreHTTPMultiClientsInParallelTest 10240 16 30 158.98 152.44 161.63 161.34
CoreHTTPNewClientPerReqTest 10240 16 30 157.99 160.39 160.77 161.24

Before - https://github.com/HarshaNalluru/azure-sdk-for-js/tree/harshan/perf/jeff-fix-test-before
After - https://github.com/HarshaNalluru/azure-sdk-for-js/tree/harshan/perf/jeff-fix-test-after

const credentialScopes = getCredentialScopes(options);
this._pipeline =
options.pipeline ||
Expand Down
6 changes: 6 additions & 0 deletions sdk/core/core-client/test/serviceClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { getOperationArgumentValueFromParameter } from "../src/operationHelpers";
import { deserializationPolicy } from "../src/deserializationPolicy";
import { TokenCredential } from "@azure/core-auth";
import { getCachedDefaultHttpsClient } from "../src/httpClientCache";

describe("ServiceClient", function() {
describe("Auth scopes", () => {
Expand Down Expand Up @@ -801,6 +802,11 @@ describe("ServiceClient", function() {
assert.strictEqual(ex.details.message, "InvalidResourceNameBody");
}
});

it("should re-use the common instance of DefaultHttpClient", function() {
const client = new ServiceClient();
assert.strictEqual((client as any)._httpsClient, getCachedDefaultHttpsClient());
});
});

async function testSendOperationRequest(
Expand Down
4 changes: 4 additions & 0 deletions sdk/core/core-http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release History

## 1.2.2 (Unreleased)

- Cache the default `HttpClient` used when one isn't passed in to `ServiceClient`. This means that for most packages we will only create a single `HttpClient`, which will improve platform resource usage by reducing overhead. [PR 12966](https://github.com/Azure/azure-sdk-for-js/pull/12966)

## 1.2.1 (2020-12-09)

- Support custom auth scopes. Scope is a mechanism in OAuth 2.0 to limit an application's access to a user's account [PR 12752](https://github.com/Azure/azure-sdk-for-js/pull/12752)
Expand Down
15 changes: 15 additions & 0 deletions sdk/core/core-http/src/httpClientCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { HttpClient } from "./httpClient";
import { DefaultHttpClient } from "./defaultHttpClient";

let cachedHttpClient: HttpClient | undefined;

export function getCachedDefaultHttpClient(): HttpClient {
if (!cachedHttpClient) {
cachedHttpClient = new DefaultHttpClient();
}

return cachedHttpClient;
}
4 changes: 2 additions & 2 deletions sdk/core/core-http/src/serviceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license.

import { TokenCredential, isTokenCredential } from "@azure/core-auth";
import { DefaultHttpClient } from "./defaultHttpClient";
import { HttpClient } from "./httpClient";
import { HttpOperationResponse, RestResponse } from "./httpOperationResponse";
import { HttpPipelineLogger } from "./httpPipelineLogger";
Expand Down Expand Up @@ -62,6 +61,7 @@ import { disableResponseDecompressionPolicy } from "./policies/disableResponseDe
import { ndJsonPolicy } from "./policies/ndJsonPolicy";
import { XML_ATTRKEY, SerializerOptions, XML_CHARKEY } from "./util/serializer.common";
import { URL } from "./url";
import { getCachedDefaultHttpClient } from "./httpClientCache";

/**
* Options to configure a proxy for outgoing requests (Node.js only).
Expand Down Expand Up @@ -197,7 +197,7 @@ export class ServiceClient {
}

this._withCredentials = options.withCredentials || false;
this._httpClient = options.httpClient || new DefaultHttpClient();
this._httpClient = options.httpClient || getCachedDefaultHttpClient();
this._requestPolicyOptions = new RequestPolicyOptions(options.httpPipelineLogger);

let requestPolicyFactories: RequestPolicyFactory[];
Expand Down
6 changes: 6 additions & 0 deletions sdk/core/core-http/test/serviceClientTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from "../src/coreHttp";
import { ParameterPath } from "../src/operationParameter";
import * as Mappers from "./testMappers";
import { getCachedDefaultHttpClient } from "../src/httpClientCache";

describe("ServiceClient", function() {
describe("Auth scopes", () => {
Expand Down Expand Up @@ -1993,6 +1994,11 @@ describe("ServiceClient", function() {
assert.strictEqual(ex.details.message, "InvalidResourceNameBody");
}
});

it("should re-use the common instance of DefaultHttpClient", function() {
const client = new ServiceClient();
assert.strictEqual((client as any)._httpClient, getCachedDefaultHttpClient());
});
});

function stringToByteArray(str: string): Uint8Array {
Expand Down