-
Notifications
You must be signed in to change notification settings - Fork 3
DO NOT MERGE: Performance audit #121
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { ApplicationChangeset, IApplicationRepository } from "@grants-stack-indexer/repository"; | ||
import { performanceLogger } from "@grants-stack-indexer/shared"; | ||
|
||
import { ChangesetHandler } from "../types/index.js"; | ||
|
||
|
@@ -33,11 +34,35 @@ export const createApplicationHandlers = ( | |
}) satisfies ChangesetHandler<"UpdateApplication">, | ||
|
||
IncrementApplicationDonationStats: (async (changeset, txConnection): Promise<void> => { | ||
const startTime = performance.now(); | ||
const { chainId, roundId, applicationId, amountInUsd } = changeset.args; | ||
await repository.incrementApplicationDonationStats( | ||
{ chainId, roundId, id: applicationId }, | ||
amountInUsd, | ||
txConnection, | ||
); | ||
const endTime = performance.now(); | ||
const duration = endTime - startTime; | ||
|
||
// Get current application stats for logging | ||
const application = await repository.getApplicationById(applicationId, chainId, roundId); | ||
|
||
performanceLogger.logMetric({ | ||
timestamp: new Date().toISOString(), | ||
eventType: "Application", | ||
operation: "IncrementApplicationDonationStats", | ||
duration, | ||
totalTime: duration, | ||
chainId, | ||
roundId, | ||
applicationId, | ||
amountInUsd, | ||
uniqueDonorsCount: application?.uniqueDonorsCount, | ||
totalDonationsCount: application?.totalDonationsCount, | ||
details: { | ||
totalAmountDonatedInUsd: application?.totalAmountDonatedInUsd, | ||
status: application?.status, | ||
}, | ||
}); | ||
Comment on lines
+47
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Extra DB round‑trip could negate the performance gain Immediately after const application = await repository.getApplicationById(...); If the repository method that increments stats can return the updated row (e.g. |
||
}) satisfies ChangesetHandler<"IncrementApplicationDonationStats">, | ||
}); |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,5 @@ | ||||||||||||||||||||||||||||||||||||||||||
import { DonationChangeset, IDonationRepository } from "@grants-stack-indexer/repository"; | ||||||||||||||||||||||||||||||||||||||||||
import { performanceLogger } from "@grants-stack-indexer/shared"; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
import { ChangesetHandler } from "../types/index.js"; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
|
@@ -18,10 +19,58 @@ export type DonationHandlers = { | |||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||
export const createDonationHandlers = (repository: IDonationRepository): DonationHandlers => ({ | ||||||||||||||||||||||||||||||||||||||||||
InsertDonation: (async (changeset, txConnection): Promise<void> => { | ||||||||||||||||||||||||||||||||||||||||||
const startTime = performance.now(); | ||||||||||||||||||||||||||||||||||||||||||
await repository.insertDonation(changeset.args.donation, txConnection); | ||||||||||||||||||||||||||||||||||||||||||
const endTime = performance.now(); | ||||||||||||||||||||||||||||||||||||||||||
const duration = endTime - startTime; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
performanceLogger.logMetric({ | ||||||||||||||||||||||||||||||||||||||||||
timestamp: new Date().toISOString(), | ||||||||||||||||||||||||||||||||||||||||||
eventType: "Donation", | ||||||||||||||||||||||||||||||||||||||||||
operation: "InsertDonation", | ||||||||||||||||||||||||||||||||||||||||||
duration, | ||||||||||||||||||||||||||||||||||||||||||
totalTime: duration, | ||||||||||||||||||||||||||||||||||||||||||
blockNumber: Number(changeset.args.donation.blockNumber), | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+22
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same timer import issue as above Add an explicit import of |
||||||||||||||||||||||||||||||||||||||||||
transactionHash: changeset.args.donation.transactionHash, | ||||||||||||||||||||||||||||||||||||||||||
chainId: changeset.args.donation.chainId, | ||||||||||||||||||||||||||||||||||||||||||
roundId: changeset.args.donation.roundId, | ||||||||||||||||||||||||||||||||||||||||||
applicationId: changeset.args.donation.applicationId || undefined, | ||||||||||||||||||||||||||||||||||||||||||
donorAddress: changeset.args.donation.donorAddress, | ||||||||||||||||||||||||||||||||||||||||||
recipientAddress: changeset.args.donation.recipientAddress, | ||||||||||||||||||||||||||||||||||||||||||
amount: changeset.args.donation.amount.toString(), | ||||||||||||||||||||||||||||||||||||||||||
amountInUsd: changeset.args.donation.amountInUsd, | ||||||||||||||||||||||||||||||||||||||||||
details: { | ||||||||||||||||||||||||||||||||||||||||||
tokenAddress: changeset.args.donation.tokenAddress, | ||||||||||||||||||||||||||||||||||||||||||
amountInRoundMatchToken: changeset.args.donation.amountInRoundMatchToken.toString(), | ||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+40
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Potential loss of precision when logging
-amountInUsd: changeset.args.donation.amountInUsd,
+amountInUsd: changeset.args.donation.amountInUsd?.toString(), The same applies in 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
}) satisfies ChangesetHandler<"InsertDonation">, | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
InsertManyDonations: (async (changeset, txConnection): Promise<void> => { | ||||||||||||||||||||||||||||||||||||||||||
const startTime = performance.now(); | ||||||||||||||||||||||||||||||||||||||||||
await repository.insertManyDonations(changeset.args.donations, txConnection); | ||||||||||||||||||||||||||||||||||||||||||
const endTime = performance.now(); | ||||||||||||||||||||||||||||||||||||||||||
const duration = endTime - startTime; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
const firstDonation = changeset.args.donations[0]; | ||||||||||||||||||||||||||||||||||||||||||
performanceLogger.logMetric({ | ||||||||||||||||||||||||||||||||||||||||||
timestamp: new Date().toISOString(), | ||||||||||||||||||||||||||||||||||||||||||
eventType: "Donation", | ||||||||||||||||||||||||||||||||||||||||||
operation: "InsertManyDonations", | ||||||||||||||||||||||||||||||||||||||||||
duration, | ||||||||||||||||||||||||||||||||||||||||||
totalTime: duration, | ||||||||||||||||||||||||||||||||||||||||||
blockNumber: firstDonation ? Number(firstDonation.blockNumber) : undefined, | ||||||||||||||||||||||||||||||||||||||||||
transactionHash: firstDonation?.transactionHash, | ||||||||||||||||||||||||||||||||||||||||||
chainId: firstDonation?.chainId, | ||||||||||||||||||||||||||||||||||||||||||
roundId: firstDonation?.roundId, | ||||||||||||||||||||||||||||||||||||||||||
amount: firstDonation ? firstDonation.amount.toString() : undefined, | ||||||||||||||||||||||||||||||||||||||||||
amountInUsd: firstDonation?.amountInUsd, | ||||||||||||||||||||||||||||||||||||||||||
details: { | ||||||||||||||||||||||||||||||||||||||||||
count: changeset.args.donations.length, | ||||||||||||||||||||||||||||||||||||||||||
totalAmountInUsd: changeset.args.donations | ||||||||||||||||||||||||||||||||||||||||||
.reduce((sum, d) => sum + Number(d.amountInUsd), 0) | ||||||||||||||||||||||||||||||||||||||||||
.toString(), | ||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||
}) satisfies ChangesetHandler<"InsertManyDonations">, | ||||||||||||||||||||||||||||||||||||||||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import Node’s high‑resolution timer explicitly
performance.now()
is used, but noimport { performance } from "node:perf_hooks"
(or"perf_hooks"
) is present.Relying on the global may break type‑checking in strict projects and older Node versions.
+import { performance } from "node:perf_hooks";
📝 Committable suggestion