perf: 'await' on OCPP request handlers only when necessary
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 30 Jan 2024 16:19:53 +0000 (17:19 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 30 Jan 2024 16:19:53 +0000 (17:19 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts
src/charging-station/ocpp/2.0/OCPP20ResponseService.ts
tests/utils/Utils.test.ts

index 6ed7115c6be65b0aebd9facfa2428db101234b51..c8bc557c2a6a70ab9f3771e5412d071991113a91 100644 (file)
@@ -103,6 +103,7 @@ import {
   convertToInt,
   formatDurationMilliSeconds,
   getRandomInteger,
+  isAsyncFunction,
   isEmptyArray,
   isNotEmptyArray,
   isNotEmptyString,
@@ -624,10 +625,12 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
           this.validatePayload(chargingStation, commandName, commandPayload)
           // Call the method to build the response
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-          response = (await this.incomingRequestHandlers.get(commandName)!(
-            chargingStation,
-            commandPayload
-          )) as ResType
+          const incomingRequestHandler = this.incomingRequestHandlers.get(commandName)!
+          if (isAsyncFunction(incomingRequestHandler)) {
+            response = (await incomingRequestHandler(chargingStation, commandPayload)) as ResType
+          } else {
+            response = incomingRequestHandler(chargingStation, commandPayload) as ResType
+          }
         } catch (error) {
           // Log
           logger.error(
index 11be84936caf98fd77ab3306910a2108dc3d55cf..be35a592c143086ea7ea228743ccd0f4fabcedb0 100644 (file)
@@ -51,7 +51,7 @@ import {
   type SetChargingProfileResponse,
   type UnlockConnectorResponse
 } from '../../../types/index.js'
-import { Constants, convertToInt, logger } from '../../../utils/index.js'
+import { Constants, convertToInt, isAsyncFunction, logger } from '../../../utils/index.js'
 import { OCPPResponseService } from '../OCPPResponseService.js'
 
 const moduleName = 'OCPP16ResponseService'
@@ -445,7 +445,18 @@ export class OCPP16ResponseService extends OCPPResponseService {
         try {
           this.validatePayload(chargingStation, commandName, payload)
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-          await this.responseHandlers.get(commandName)!(chargingStation, payload, requestPayload)
+          const responseHandler = this.responseHandlers.get(commandName)!
+          if (isAsyncFunction(responseHandler)) {
+            await responseHandler(chargingStation, payload, requestPayload)
+          } else {
+            (
+              responseHandler as (
+                chargingStation: ChargingStation,
+                payload: JsonType,
+                requestPayload?: JsonType
+              ) => void
+            )(chargingStation, payload, requestPayload)
+          }
         } catch (error) {
           logger.error(
             `${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`,
index 67f3a61b492529ca0a2ae2bb487200035ff59569..5f6a029d8eb15f574858b11d870c281371aea2f2 100644 (file)
@@ -13,7 +13,7 @@ import {
   OCPP20IncomingRequestCommand,
   OCPPVersion
 } from '../../../types/index.js'
-import { logger } from '../../../utils/index.js'
+import { isAsyncFunction, logger } from '../../../utils/index.js'
 import { OCPPIncomingRequestService } from '../OCPPIncomingRequestService.js'
 
 const moduleName = 'OCPP20IncomingRequestService'
@@ -91,10 +91,12 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
           this.validatePayload(chargingStation, commandName, commandPayload)
           // Call the method to build the response
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-          response = (await this.incomingRequestHandlers.get(commandName)!(
-            chargingStation,
-            commandPayload
-          )) as ResType
+          const incomingRequestHandler = this.incomingRequestHandlers.get(commandName)!
+          if (isAsyncFunction(incomingRequestHandler)) {
+            response = (await incomingRequestHandler(chargingStation, commandPayload)) as ResType
+          } else {
+            response = incomingRequestHandler(chargingStation, commandPayload) as ResType
+          }
         } catch (error) {
           // Log
           logger.error(
index bb12b0069c7fedbab970755c629b984de486cab8..d468260b5346f55a9e0a97cecaf7922436cbac95 100644 (file)
@@ -19,7 +19,7 @@ import {
   RegistrationStatusEnumType,
   type ResponseHandler
 } from '../../../types/index.js'
-import { logger } from '../../../utils/index.js'
+import { isAsyncFunction, logger } from '../../../utils/index.js'
 import { OCPPResponseService } from '../OCPPResponseService.js'
 
 const moduleName = 'OCPP20ResponseService'
@@ -118,7 +118,18 @@ export class OCPP20ResponseService extends OCPPResponseService {
         try {
           this.validatePayload(chargingStation, commandName, payload)
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-          await this.responseHandlers.get(commandName)!(chargingStation, payload, requestPayload)
+          const responseHandler = this.responseHandlers.get(commandName)!
+          if (isAsyncFunction(responseHandler)) {
+            await responseHandler(chargingStation, payload, requestPayload)
+          } else {
+            (
+              responseHandler as (
+                chargingStation: ChargingStation,
+                payload: JsonType,
+                requestPayload?: JsonType
+              ) => void
+            )(chargingStation, payload, requestPayload)
+          }
         } catch (error) {
           logger.error(
             `${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`,
index bef31dd97eaf0512db552841410be0c4def5841d..6db4e36472b7d0905f731b41ff32061fce2e136c 100644 (file)
@@ -18,6 +18,7 @@ import {
   getRandomInteger,
   hasOwnProp,
   isArraySorted,
+  isAsyncFunction,
   isEmptyArray,
   isEmptyObject,
   isEmptyString,
@@ -248,6 +249,82 @@ await describe('Utils test suite', async () => {
     expect(isObject(new WeakSet())).toBe(true)
   })
 
+  await it('Verify isAsyncFunction()', () => {
+    expect(isAsyncFunction(null)).toBe(false)
+    expect(isAsyncFunction(undefined)).toBe(false)
+    expect(isAsyncFunction(true)).toBe(false)
+    expect(isAsyncFunction(false)).toBe(false)
+    expect(isAsyncFunction(0)).toBe(false)
+    expect(isAsyncFunction('')).toBe(false)
+    expect(isAsyncFunction([])).toBe(false)
+    expect(isAsyncFunction(new Date())).toBe(false)
+    // eslint-disable-next-line prefer-regex-literals
+    expect(isAsyncFunction(new RegExp('[a-z]', 'i'))).toBe(false)
+    expect(isAsyncFunction(new Error())).toBe(false)
+    expect(isAsyncFunction(new Map())).toBe(false)
+    expect(isAsyncFunction(new Set())).toBe(false)
+    expect(isAsyncFunction(new WeakMap())).toBe(false)
+    expect(isAsyncFunction(new WeakSet())).toBe(false)
+    expect(isAsyncFunction(new Int8Array())).toBe(false)
+    expect(isAsyncFunction(new Uint8Array())).toBe(false)
+    expect(isAsyncFunction(new Uint8ClampedArray())).toBe(false)
+    expect(isAsyncFunction(new Int16Array())).toBe(false)
+    expect(isAsyncFunction(new Uint16Array())).toBe(false)
+    expect(isAsyncFunction(new Int32Array())).toBe(false)
+    expect(isAsyncFunction(new Uint32Array())).toBe(false)
+    expect(isAsyncFunction(new Float32Array())).toBe(false)
+    expect(isAsyncFunction(new Float64Array())).toBe(false)
+    expect(isAsyncFunction(new BigInt64Array())).toBe(false)
+    expect(isAsyncFunction(new BigUint64Array())).toBe(false)
+    // eslint-disable-next-line @typescript-eslint/no-empty-function
+    expect(isAsyncFunction(new Promise(() => {}))).toBe(false)
+    expect(isAsyncFunction(new WeakRef({}))).toBe(false)
+    // eslint-disable-next-line @typescript-eslint/no-empty-function
+    expect(isAsyncFunction(new FinalizationRegistry(() => {}))).toBe(false)
+    expect(isAsyncFunction(new ArrayBuffer(16))).toBe(false)
+    expect(isAsyncFunction(new SharedArrayBuffer(16))).toBe(false)
+    expect(isAsyncFunction(new DataView(new ArrayBuffer(16)))).toBe(false)
+    expect(isAsyncFunction({})).toBe(false)
+    expect(isAsyncFunction({ a: 1 })).toBe(false)
+    // eslint-disable-next-line @typescript-eslint/no-empty-function
+    expect(isAsyncFunction(() => {})).toBe(false)
+    // eslint-disable-next-line @typescript-eslint/no-empty-function
+    expect(isAsyncFunction(function () {})).toBe(false)
+    // eslint-disable-next-line @typescript-eslint/no-empty-function
+    expect(isAsyncFunction(function named () {})).toBe(false)
+    // eslint-disable-next-line @typescript-eslint/no-empty-function
+    expect(isAsyncFunction(async () => {})).toBe(true)
+    // eslint-disable-next-line @typescript-eslint/no-empty-function
+    expect(isAsyncFunction(async function () {})).toBe(true)
+    // eslint-disable-next-line @typescript-eslint/no-empty-function
+    expect(isAsyncFunction(async function named () {})).toBe(true)
+    class TestClass {
+      // eslint-disable-next-line @typescript-eslint/no-empty-function
+      public testSync (): void {}
+      // eslint-disable-next-line @typescript-eslint/no-empty-function
+      public async testAsync (): Promise<void> {}
+      // eslint-disable-next-line @typescript-eslint/no-empty-function
+      public testArrowSync = (): void => {}
+      // eslint-disable-next-line @typescript-eslint/no-empty-function
+      public testArrowAsync = async (): Promise<void> => {}
+      // eslint-disable-next-line @typescript-eslint/no-empty-function
+      public static testStaticSync (): void {}
+      // eslint-disable-next-line @typescript-eslint/no-empty-function
+      public static async testStaticAsync (): Promise<void> {}
+    }
+    const testClass = new TestClass()
+    // eslint-disable-next-line @typescript-eslint/unbound-method
+    expect(isAsyncFunction(testClass.testSync)).toBe(false)
+    // eslint-disable-next-line @typescript-eslint/unbound-method
+    expect(isAsyncFunction(testClass.testAsync)).toBe(true)
+    expect(isAsyncFunction(testClass.testArrowSync)).toBe(false)
+    expect(isAsyncFunction(testClass.testArrowAsync)).toBe(true)
+    // eslint-disable-next-line @typescript-eslint/unbound-method
+    expect(isAsyncFunction(TestClass.testStaticSync)).toBe(false)
+    // eslint-disable-next-line @typescript-eslint/unbound-method
+    expect(isAsyncFunction(TestClass.testStaticAsync)).toBe(true)
+  })
+
   await it('Verify clone()', () => {
     const obj = { 1: 1 }
     expect(clone(obj)).toStrictEqual(obj)