-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
ci: Fix performance step in CI #9931
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
Merged
mtrezza
merged 7 commits into
parse-community:alpha
from
mtrezza:fix/schema-race-condition
Nov 17, 2025
+388
−121
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ae57fb7
fix
mtrezza 2401b84
Revert "fix"
mtrezza fbeb80a
Create bug.md
mtrezza 9da12ef
Revert "Create bug.md"
mtrezza 7315b3b
update benchmarks
mtrezza 3ac7987
explicit iterations
mtrezza 10fcd9f
faster
mtrezza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| /** | ||
| * MongoDB Latency Wrapper | ||
| * | ||
| * Utility to inject artificial latency into MongoDB operations for performance testing. | ||
| * This wrapper temporarily wraps MongoDB Collection methods to add delays before | ||
| * database operations execute. | ||
| * | ||
| * Usage: | ||
| * const { wrapMongoDBWithLatency } = require('./MongoLatencyWrapper'); | ||
| * | ||
| * // Before initializing Parse Server | ||
| * const unwrap = wrapMongoDBWithLatency(10); // 10ms delay | ||
| * | ||
| * // ... run benchmarks ... | ||
| * | ||
| * // Cleanup when done | ||
| * unwrap(); | ||
| */ | ||
|
|
||
| const { Collection } = require('mongodb'); | ||
|
|
||
| // Store original methods for restoration | ||
| const originalMethods = new Map(); | ||
|
|
||
| /** | ||
| * Wrap a Collection method to add artificial latency | ||
| * @param {string} methodName - Name of the method to wrap | ||
| * @param {number} latencyMs - Delay in milliseconds | ||
| */ | ||
| function wrapMethod(methodName, latencyMs) { | ||
| if (!originalMethods.has(methodName)) { | ||
| originalMethods.set(methodName, Collection.prototype[methodName]); | ||
| } | ||
|
|
||
| const originalMethod = originalMethods.get(methodName); | ||
|
|
||
| Collection.prototype[methodName] = function (...args) { | ||
| // For methods that return cursors (like find, aggregate), we need to delay the execution | ||
| // but still return a cursor-like object | ||
| const result = originalMethod.apply(this, args); | ||
|
|
||
| // Check if result has cursor methods (toArray, forEach, etc.) | ||
| if (result && typeof result.toArray === 'function') { | ||
| // Wrap cursor methods that actually execute the query | ||
| const originalToArray = result.toArray.bind(result); | ||
| result.toArray = function() { | ||
| // Wait for the original promise to settle, then delay the result | ||
| return originalToArray().then( | ||
| value => new Promise(resolve => setTimeout(() => resolve(value), latencyMs)), | ||
| error => new Promise((_, reject) => setTimeout(() => reject(error), latencyMs)) | ||
| ); | ||
| }; | ||
| return result; | ||
| } | ||
|
|
||
| // For promise-returning methods, wrap the promise with delay | ||
| if (result && typeof result.then === 'function') { | ||
| // Wait for the original promise to settle, then delay the result | ||
| return result.then( | ||
| value => new Promise(resolve => setTimeout(() => resolve(value), latencyMs)), | ||
| error => new Promise((_, reject) => setTimeout(() => reject(error), latencyMs)) | ||
| ); | ||
| } | ||
|
|
||
| // For synchronous methods, just add delay | ||
| return new Promise((resolve) => { | ||
| setTimeout(() => { | ||
| resolve(result); | ||
| }, latencyMs); | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Wrap MongoDB Collection methods with artificial latency | ||
| * @param {number} latencyMs - Delay in milliseconds to inject before each operation | ||
| * @returns {Function} unwrap - Function to restore original methods | ||
| */ | ||
| function wrapMongoDBWithLatency(latencyMs) { | ||
| if (typeof latencyMs !== 'number' || latencyMs < 0) { | ||
| throw new Error('latencyMs must be a non-negative number'); | ||
| } | ||
|
|
||
| if (latencyMs === 0) { | ||
| // eslint-disable-next-line no-console | ||
| console.log('Latency is 0ms, skipping MongoDB wrapping'); | ||
| return () => {}; // No-op unwrap function | ||
| } | ||
|
|
||
| // eslint-disable-next-line no-console | ||
| console.log(`Wrapping MongoDB operations with ${latencyMs}ms artificial latency`); | ||
|
|
||
| // List of MongoDB Collection methods to wrap | ||
| const methodsToWrap = [ | ||
| 'find', | ||
| 'findOne', | ||
| 'countDocuments', | ||
| 'estimatedDocumentCount', | ||
| 'distinct', | ||
| 'aggregate', | ||
| 'insertOne', | ||
| 'insertMany', | ||
| 'updateOne', | ||
| 'updateMany', | ||
| 'replaceOne', | ||
| 'deleteOne', | ||
| 'deleteMany', | ||
| 'findOneAndUpdate', | ||
| 'findOneAndReplace', | ||
| 'findOneAndDelete', | ||
| 'createIndex', | ||
| 'createIndexes', | ||
| 'dropIndex', | ||
| 'dropIndexes', | ||
| 'drop', | ||
| ]; | ||
|
|
||
| methodsToWrap.forEach(methodName => { | ||
| wrapMethod(methodName, latencyMs); | ||
| }); | ||
|
|
||
| // Return unwrap function to restore original methods | ||
| return function unwrap() { | ||
| // eslint-disable-next-line no-console | ||
| console.log('Removing MongoDB latency wrapper, restoring original methods'); | ||
|
|
||
| originalMethods.forEach((originalMethod, methodName) => { | ||
| Collection.prototype[methodName] = originalMethod; | ||
| }); | ||
|
|
||
| originalMethods.clear(); | ||
| }; | ||
| } | ||
|
|
||
| module.exports = { | ||
| wrapMongoDBWithLatency, | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Critical: Multiple invocations will stack latency wrappers.
The
wrapMethodfunction stores the original method only on the first invocation (line 31-33), but it unconditionally replacesCollection.prototype[methodName]on line 37. IfwrapMongoDBWithLatencyis called multiple times without callingunwrap()in between, the second invocation will wrap an already-wrapped method, causing latency to be applied multiple times (e.g., 2× latency on second call, 3× on third call).Additionally, the cursor-wrapping logic only instruments
toArray()(lines 45-52), but MongoDB cursors expose many other execution methods such asforEach,next,hasNext,map, andclose. Operations using these methods will bypass the artificial latency, leading to inconsistent benchmark behavior.Consider these fixes:
Prevent multiple wrapping: Check if the method is already wrapped before applying a new wrapper, or throw an error if
originalMethodsalready contains the method.Expand cursor coverage: Wrap all cursor execution methods, or document that only
toArray()is supported and advise benchmark code to use it exclusively.Apply this diff to prevent stacking:
function wrapMethod(methodName, latencyMs) { + // Prevent wrapping an already-wrapped method + if (Collection.prototype[methodName].__isLatencyWrapped) { + throw new Error(`Method ${methodName} is already wrapped. Call unwrap() first.`); + } + if (!originalMethods.has(methodName)) { originalMethods.set(methodName, Collection.prototype[methodName]); } const originalMethod = originalMethods.get(methodName); Collection.prototype[methodName] = function (...args) { // ... existing wrapper code ... }; + + // Mark the method as wrapped + Collection.prototype[methodName].__isLatencyWrapped = true; }And update the unwrap logic:
originalMethods.forEach((originalMethod, methodName) => { Collection.prototype[methodName] = originalMethod; + delete Collection.prototype[methodName].__isLatencyWrapped; });🤖 Prompt for AI Agents