Skip to content

Commit

Permalink
Add rollback benchmarks (#345)
Browse files Browse the repository at this point in the history
* Move IO to static

* Add benchmarks for rollback db functions

* Remove unnecessary thenResolve

Co-authored-by: Dmitry Zakharov <[email protected]>

---------

Co-authored-by: Dmitry Zakharov <[email protected]>
  • Loading branch information
JonoPrest and DZakh authored Nov 19, 2024
1 parent b27505a commit b27e25e
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,55 +236,58 @@ module RollBack = {

let inMemStore = InMemoryStore.make(~rollBackEventIdentifier)

await Utils.Array.awaitEach(Entities.allEntities, async entityMod => {
let module(Entity) = entityMod
let entityMod =
entityMod->(
Utils.magic: module(Entities.InternalEntity) => module(Entities.Entity with
type t = 'entity
let _ =
await Entities.allEntities
->Belt.Array.map(async entityMod => {
let module(Entity) = entityMod
let entityMod =
entityMod->(
Utils.magic: module(Entities.InternalEntity) => module(Entities.Entity with
type t = 'entity
)
)
)

let diff = await DbFunctions.sql->DbFunctions.EntityHistory.getRollbackDiff(
isUnorderedMultichainMode
? UnorderedMultichain({
reorgChainId: chainId,
safeBlockNumber: blockNumber,
})
: OrderedMultichain({
safeBlockTimestamp: blockTimestamp,
reorgChainId: chainId,
safeBlockNumber: blockNumber,
}),
~entityMod,
)
let diff = await DbFunctions.sql->DbFunctions.EntityHistory.getRollbackDiff(
isUnorderedMultichainMode
? UnorderedMultichain({
reorgChainId: chainId,
safeBlockNumber: blockNumber,
})
: OrderedMultichain({
safeBlockTimestamp: blockTimestamp,
reorgChainId: chainId,
safeBlockNumber: blockNumber,
}),
~entityMod,
)

let entityTable = inMemStore.entities->InMemoryStore.EntityTables.get(entityMod)
let entityTable = inMemStore.entities->InMemoryStore.EntityTables.get(entityMod)

diff->Belt.Array.forEach(historyRow => {
let eventIdentifier: Types.eventIdentifier = {
chainId: historyRow.current.chain_id,
blockNumber: historyRow.current.block_number,
logIndex: historyRow.current.log_index,
blockTimestamp: historyRow.current.block_timestamp,
}
switch historyRow.entityData {
| Set(entity) =>
entityTable->InMemoryTable.Entity.set(
Set(entity)->Types.mkEntityUpdate(
~eventIdentifier,
~entityId=entity->Entities.getEntityId,
),
~shouldSaveHistory=false,
)
| Delete({id}) =>
entityTable->InMemoryTable.Entity.set(
Delete->Types.mkEntityUpdate(~eventIdentifier, ~entityId=id),
~shouldSaveHistory=false,
)
}
diff->Belt.Array.forEach(historyRow => {
let eventIdentifier: Types.eventIdentifier = {
chainId: historyRow.current.chain_id,
blockNumber: historyRow.current.block_number,
logIndex: historyRow.current.log_index,
blockTimestamp: historyRow.current.block_timestamp,
}
switch historyRow.entityData {
| Set(entity) =>
entityTable->InMemoryTable.Entity.set(
Set(entity)->Types.mkEntityUpdate(
~eventIdentifier,
~entityId=entity->Entities.getEntityId,
),
~shouldSaveHistory=false,
)
| Delete({id}) =>
entityTable->InMemoryTable.Entity.set(
Delete->Types.mkEntityUpdate(~eventIdentifier, ~entityId=id),
~shouldSaveHistory=false,
)
}
})
})
})
->Promise.all

inMemStore
}
Expand Down
85 changes: 66 additions & 19 deletions codegenerator/cli/templates/static/codegen/src/db/DbFunctions.res
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@ module EntityHistory = {
~safeChainIdAndBlockNumberArray: array<chainIdAndBlockNumber>,
) => promise<unit> = "pruneStaleEntityHistory"

let rollbacksGroup = "Rollbacks"

let pruneStaleEntityHistory = async (sql, ~entityName, ~safeChainIdAndBlockNumberArray) => {
let startTime = Hrtime.makeTimer()
try await sql->pruneStaleEntityHistoryInternal(
~entityName,
~safeChainIdAndBlockNumberArray,
Expand All @@ -306,6 +309,16 @@ module EntityHistory = {
),
)
}

if Env.saveBenchmarkData {
let elapsedTimeMillis = Hrtime.timeSince(startTime)->Hrtime.toMillis->Hrtime.floatFromMillis

Benchmark.addSummaryData(
~group=rollbacksGroup,
~label=`Prune Stale History Time (ms)`,
~value=elapsedTimeMillis,
)
}
}

module Args = {
Expand Down Expand Up @@ -371,12 +384,14 @@ module EntityHistory = {
}
}

let deleteAllEntityHistoryAfterEventIdentifier = (
let deleteAllEntityHistoryAfterEventIdentifier = async (
sql,
~isUnorderedMultichainMode,
~eventIdentifier: Types.eventIdentifier,
~allEntities=Entities.allEntities,
): promise<unit> => {
): unit => {
let startTime = Hrtime.makeTimer()

let {chainId, blockNumber, blockTimestamp} = eventIdentifier
let args: Args.t = isUnorderedMultichainMode
? UnorderedMultichain({reorgChainId: chainId, safeBlockNumber: blockNumber})
Expand All @@ -386,23 +401,33 @@ module EntityHistory = {
safeBlockTimestamp: blockTimestamp,
})

allEntities
->Belt.Array.map(async entityMod => {
let module(Entity) = entityMod
try await deleteRolledBackEntityHistory(
sql,
~entityName=Entity.name,
~getFirstChangeSerial=args->Args.makeGetFirstChangeSerial(~entityName=Entity.name),
) catch {
| exn =>
exn->ErrorHandling.mkLogAndRaise(
~msg=`Failed to delete rolled back entity history`,
~logger=args->Args.getLogger(~entityName=Entity.name),
)
}
})
->Promise.all
->Promise.thenResolve(_ => ())
let _ =
await allEntities
->Belt.Array.map(async entityMod => {
let module(Entity) = entityMod
try await deleteRolledBackEntityHistory(
sql,
~entityName=Entity.name,
~getFirstChangeSerial=args->Args.makeGetFirstChangeSerial(~entityName=Entity.name),
) catch {
| exn =>
exn->ErrorHandling.mkLogAndRaise(
~msg=`Failed to delete rolled back entity history`,
~logger=args->Args.getLogger(~entityName=Entity.name),
)
}
})
->Promise.all

if Env.saveBenchmarkData {
let elapsedTimeMillis = Hrtime.timeSince(startTime)->Hrtime.toMillis->Hrtime.floatFromMillis

Benchmark.addSummaryData(
~group=rollbacksGroup,
~label=`Delete Rolled Back History Time (ms)`,
~value=elapsedTimeMillis,
)
}
}

let getRollbackDiff = async (
Expand All @@ -412,6 +437,7 @@ module EntityHistory = {
~entityMod: module(Entities.Entity with type t = entity),
) => {
let module(Entity) = entityMod
let startTime = Hrtime.makeTimer()

let diffRes = switch await getRollbackDiffInternal(
sql,
Expand All @@ -426,6 +452,16 @@ module EntityHistory = {
| res => res
}

if Env.saveBenchmarkData {
let elapsedTimeMillis = Hrtime.timeSince(startTime)->Hrtime.toMillis->Hrtime.floatFromMillis

Benchmark.addSummaryData(
~group=rollbacksGroup,
~label=`Diff Creation Time (ms)`,
~value=elapsedTimeMillis,
)
}

switch diffRes->S.parseAnyOrRaiseWith(Entity.entityHistory.schemaRows) {
| exception exn =>
exn->ErrorHandling.mkLogAndRaise(
Expand Down Expand Up @@ -461,6 +497,7 @@ module EntityHistory = {
args: Args.t,
~allEntities=Entities.allEntities,
) => {
let startTime = Hrtime.makeTimer()
let firstChangeEventPerChain = FirstChangeEventPerChain.make()

let _ =
Expand Down Expand Up @@ -501,6 +538,16 @@ module EntityHistory = {
})
->Promise.all

if Env.saveBenchmarkData {
let elapsedTimeMillis = Hrtime.timeSince(startTime)->Hrtime.toMillis->Hrtime.floatFromMillis

Benchmark.addSummaryData(
~group=rollbacksGroup,
~label=`Get First Change Event Per Chain Time (ms)`,
~value=elapsedTimeMillis,
)
}

firstChangeEventPerChain
}

Expand Down

0 comments on commit b27e25e

Please sign in to comment.