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

feat: execution time plugin #19

Merged
merged 13 commits into from
Apr 9, 2024
12 changes: 5 additions & 7 deletions common/lib/abstract_connection_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export abstract class AbstractConnectionPlugin implements ConnectionPlugin {
abstract getSubscribedMethods(): Set<string>;

connect<T>(hostInfo: HostInfo, props: Map<string, any>, isInitialConnection: boolean, connectFunc: () => Promise<T>): Promise<T> {
throw new Error("Method not implemented.");
return connectFunc();
}

forceConnect<T>(hostInfo: HostInfo, props: Map<string, any>, isInitialConnection: boolean, forceConnectFunc: () => Promise<T>): Promise<T> {
throw new Error("Method not implemented.");
return forceConnectFunc();
}

execute<T>(methodName: string, methodFunc: () => Promise<T>, methodArgs: any[]): Promise<T> {
Expand All @@ -41,14 +41,12 @@ export abstract class AbstractConnectionPlugin implements ConnectionPlugin {
hostListProviderService: HostListProviderService,
initHostProviderFunc: () => void
): void {
throw new Error("Method not implemented.");
initHostProviderFunc();
}

notifyConnectionChanged(changes: Set<HostChangeOptions>): OldConnectionSuggestionAction {
throw new Error("Method not implemented.");
return OldConnectionSuggestionAction.NO_OPINION;
}

notifyNodeListChanged(changes: Map<string, Set<HostChangeOptions>>): void {
throw new Error("Method not implemented.");
}
notifyNodeListChanged(changes: Map<string, Set<HostChangeOptions>>): void {}
}
4 changes: 3 additions & 1 deletion common/lib/connection_plugin_chain_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { WrapperProperties } from "./wrapper_property";
import { AwsWrapperError } from "./utils/aws_wrapper_error";
import { Messages } from "./utils/messages";
import { DefaultPlugin } from "./plugins/default_plugin";
import { ExecutionTimePluginFactory } from './plugins/execution_time_plugin';

export class PluginFactoryInfo {}

Expand All @@ -33,7 +34,8 @@ export class ConnectionPluginChainBuilder {

static readonly PLUGIN_FACTORIES = new Map<string, FactoryClass>([
["iam", IamAuthenticationPluginFactory],
["failover", FailoverPluginFactory]
["failover", FailoverPluginFactory],
["executionTime", ExecutionTimePluginFactory]
]);

getPlugins(pluginService: PluginService, props: Map<string, any>): ConnectionPlugin[] {
Expand Down
5 changes: 1 addition & 4 deletions common/lib/plugins/default_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ export class DefaultPlugin extends AbstractConnectionPlugin {

override execute<Type>(methodName: string, methodFunc: () => Type): Type {
logger.debug(Messages.get("DefaultPlugin.executingMethod", methodName));
const start = performance.now();
const res = methodFunc();
logger.debug(`Execution time for plugin ${this.id}: ${performance.now() - start} ms`);
return res;
return methodFunc();
}
}
59 changes: 59 additions & 0 deletions common/lib/plugins/execution_time_plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

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.
*/

import { logger } from "../../logutils";
import { AbstractConnectionPlugin } from "../abstract_connection_plugin";
import { ConnectionPlugin } from "../connection_plugin";
import { ConnectionPluginFactory } from "../plugin_factory";
import { PluginService } from "../plugin_service";
import { Messages } from "../utils/messages";
import { getTimeInNanos } from "../utils/utils";

export class ExecutionTimePlugin extends AbstractConnectionPlugin {
jasonlamz marked this conversation as resolved.
Show resolved Hide resolved
private static readonly subscribedMethods: Set<string> = new Set<string>(["*"]);
private static executionTime: bigint = 0n;

public override getSubscribedMethods(): Set<string> {
return ExecutionTimePlugin.subscribedMethods;
}

public override async execute<T>(methodName: string, methodFunc: () => Promise<T>, methodArgs: any[]): Promise<T> {
const startTime = getTimeInNanos();

const result = await methodFunc();

const elapsedTimeNanos = getTimeInNanos() - startTime;

// Convert from ns to ms
logger.debug(Messages.get("ExecutionTimePlugin.executionTime", methodName, (elapsedTimeNanos / 1000000n).toString()));
ExecutionTimePlugin.executionTime += elapsedTimeNanos;
return result;
}

public static resetExecutionTime(): void {
ExecutionTimePlugin.executionTime = 0n;
}

public static getTotalExecutionTime(): bigint {
return ExecutionTimePlugin.executionTime;
}
}

export class ExecutionTimePluginFactory implements ConnectionPluginFactory {
getInstance(pluginService: PluginService, properties: Map<string, any>): ConnectionPlugin {
return new ExecutionTimePlugin();
}
}
6 changes: 2 additions & 4 deletions common/lib/plugins/failover/failover_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { ClusterAwareReaderFailoverHandler } from "./reader_failover_handler";
import { SubscribedMethodHelper } from "../../utils/subsribed_method_helper";
import { HostChangeOptions } from "../../host_change_options";
import { ClusterAwareWriterFailoverHandler } from "./writer_failover_handler";
import { Messages } from "../../utils/messages";

export class FailoverPlugin extends AbstractConnectionPlugin {
private static readonly subscribedMethods: Set<string> = new Set([
Expand Down Expand Up @@ -97,13 +98,10 @@ export class FailoverPlugin extends AbstractConnectionPlugin {

override async execute<T>(methodName: string, methodFunc: () => Promise<T>): Promise<T> {
try {
const start = performance.now();
if (this.canUpdateTopology(methodName)) {
await this.updateTopology(false);
}
const res = methodFunc();
logger.debug(`Execution time for plugin ${this.id}: ${performance.now() - start} ms`);
return res;
return methodFunc();
} catch (e) {
logger.debug(e);
throw e;
Expand Down
3 changes: 2 additions & 1 deletion common/lib/utils/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
"ClusterAwareWriterFailoverHandler.taskBFailedToConnectToAnyReader": "[TaskB] Failed to connect to any reader.",
"ClusterAwareWriterFailoverHandler.standaloneNode": "[TaskB] Host %s is not yet connected to a cluster. The cluster is still being reconfigured.",
"ClusterAwareWriterFailoverHandler.taskBAttemptConnectionToNewWriter": "[TaskB] Trying to connect to a new writer: '%s'",
"ClusterAwareWriterFailoverHandler.alreadyWriter": "Current reader connection is actually a new writer connection."
"ClusterAwareWriterFailoverHandler.alreadyWriter": "Current reader connection is actually a new writer connection.",
"ExecutionTimePlugin.executionTime": "Executed %s in %s milliseconds."
jasonlamz marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion common/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function logTopology(hosts: HostInfo[], msgPrefix: string) {
}

export function getTimeInNanos() {
return performance.now();
return process.hrtime.bigint();
}

export function maskProperties(props: Map<string, any>) {
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/execution_time_plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

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.
*/

import { ExecutionTimePlugin } from "aws-wrapper-common-lib/lib/plugins/execution_time_plugin";
import { sleep } from "aws-wrapper-common-lib/lib/utils/utils";

const mockCallable = jest.fn();
const timeToSleepMs = 1000;
const timeToSleepNs = timeToSleepMs * 1000000;

describe("executionTimePluginTest", () => {
it("test_executeTime", async () => {
mockCallable.mockImplementation(async () => {
await sleep(timeToSleepMs);
return null;
});

const plugin = new ExecutionTimePlugin();

await plugin.execute("query", mockCallable, []);
jasonlamz marked this conversation as resolved.
Show resolved Hide resolved

expect(ExecutionTimePlugin.getTotalExecutionTime()).toBeGreaterThan(timeToSleepNs);

await plugin.execute("query", mockCallable, []);

expect(ExecutionTimePlugin.getTotalExecutionTime()).toBeGreaterThan(timeToSleepNs * 2);

ExecutionTimePlugin.resetExecutionTime();
expect(ExecutionTimePlugin.getTotalExecutionTime()).toEqual(0n);
});
});
Loading