Skip to content

Commit

Permalink
feat: CommonDaoCfg.debugValidationTimeThreshhold
Browse files Browse the repository at this point in the history
to debug joi taking >500ms on average on some payloads
  • Loading branch information
kirillgroshkov committed Nov 13, 2024
1 parent e22e604 commit 2a90121
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/commondao/common.dao.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
AnyObject,
BaseDBEntity,
CommonLogger,
ErrorMode,
NumberOfMilliseconds,
Promisable,
UnixTimestamp,
ZodError,
Expand Down Expand Up @@ -146,6 +148,17 @@ export interface CommonDaoCfg<
*/
validateOnSave?: boolean

/**
* Defaults to undefined == disabled.
* If set - enable the monitoring of validation time and will alert on crossing the threshold.
*/
debugValidationTimeThreshhold?: NumberOfMilliseconds

/**
* Called when debugValidationTimeThreshhold is crossed.
*/
onValidationTimeThreshold?: (info: AnyObject) => void

/**
* Defaults to false.
* Setting it to true will set saveMethod to `insert` for save/saveBatch, which will
Expand Down
16 changes: 16 additions & 0 deletions src/commondao/common.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,23 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
})
} else {
// Joi
const start = performance.now()
const vr = getValidationResult(obj, schema, objectName)
const end = performance.now()
const tookMillis = end - start

if (
this.cfg.debugValidationTimeThreshhold &&
tookMillis >= this.cfg.debugValidationTimeThreshhold
) {
this.cfg.onValidationTimeThreshold?.({
tookMillis,
error: !!vr.error,
table,
obj,
})
}

error = vr.error
convertedValue = vr.value
}
Expand Down

0 comments on commit 2a90121

Please sign in to comment.