1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-26 01:35:28 +03:00

More coderabbit

This commit is contained in:
Mark Haslinghuis 2025-06-12 22:57:37 +02:00
parent 97f35c9724
commit 6a06ca1ed1
2 changed files with 23 additions and 6 deletions

View file

@ -46,6 +46,12 @@ export class MSPQueueMonitor {
* Hook into MSP methods to collect real-time metrics * Hook into MSP methods to collect real-time metrics
*/ */
_hookMSPMethods() { _hookMSPMethods() {
// Check if MSP instance is already instrumented to prevent double-patching
if (this.msp._mspQueueMonitorInstrumented) {
console.warn("MSP instance is already instrumented by MSPQueueMonitor");
return;
}
// Store original methods // Store original methods
this.originalSendMessage = this.msp.send_message.bind(this.msp); this.originalSendMessage = this.msp.send_message.bind(this.msp);
this.originalDispatchMessage = this.msp._dispatch_message.bind(this.msp); this.originalDispatchMessage = this.msp._dispatch_message.bind(this.msp);
@ -70,6 +76,9 @@ export class MSPQueueMonitor {
return this.originalRemoveRequest(requestObj); return this.originalRemoveRequest(requestObj);
}; };
} }
// Mark MSP instance as instrumented
this.msp._mspQueueMonitorInstrumented = true;
} }
/** /**
@ -453,7 +462,8 @@ export class MSPQueueMonitor {
} }
// Deduct for queue size issues // Deduct for queue size issues
const queueRatio = this.currentQueueSize / (this.msp.MAX_QUEUE_SIZE || 50); const currentQueueSize = this.currentQueueSize || (this.msp.callbacks?.length ?? 0);
const queueRatio = currentQueueSize / (this.msp.MAX_QUEUE_SIZE || 50);
if (queueRatio > 0.8) { if (queueRatio > 0.8) {
score -= 20; score -= 20;
} else if (queueRatio > 0.6) { } else if (queueRatio > 0.6) {
@ -596,7 +606,13 @@ export class MSPQueueMonitor {
this.msp._removeRequestFromCallbacks = this.originalRemoveRequest; this.msp._removeRequestFromCallbacks = this.originalRemoveRequest;
} }
// Clear instrumentation flag
delete this.msp._mspQueueMonitorInstrumented;
this.listeners = []; this.listeners = [];
// Clear the singleton instance to allow creating a fresh monitor later
_mspQueueMonitorInstance = null;
} }
} }

View file

@ -3,12 +3,12 @@
* Comprehensive testing tool for MSP queue management, timeout handling, and performance * Comprehensive testing tool for MSP queue management, timeout handling, and performance
*/ */
import { MSPQueueMonitor } from "./msp_queue_monitor.js"; import { mspQueueMonitor } from "./msp_queue_monitor.js";
export class MSPStressTest { export class MSPStressTest {
constructor(mspInstance) { constructor(mspInstance) {
this.msp = mspInstance; this.msp = mspInstance;
this.monitor = new MSPQueueMonitor(mspInstance); this.monitor = mspQueueMonitor; // Reuse singleton to avoid duplicate method patching
this.isRunning = false; this.isRunning = false;
this.testResults = []; this.testResults = [];
this.currentTest = null; this.currentTest = null;
@ -92,6 +92,7 @@ export class MSPStressTest {
} }
this.monitor.stopMonitoring(); this.monitor.stopMonitoring();
this.monitor.destroy(); // Clean up MSP method patches and restore original behavior
this.testResults = results; this.testResults = results;
const report = this.generateTestReport(results); const report = this.generateTestReport(results);
@ -117,7 +118,7 @@ export class MSPStressTest {
} }
const results = await Promise.allSettled(promises); const results = await Promise.allSettled(promises);
const successful = results.filter((r) => r.status === "fulfilled" && !r.value.error).length; const successful = results.filter((r) => r.status === "fulfilled" && !(r.value && r.value.error)).length;
const failed = results.length - successful; const failed = results.length - successful;
return { return {
@ -194,9 +195,9 @@ export class MSPStressTest {
} }
const results = await Promise.allSettled(promises); const results = await Promise.allSettled(promises);
const successful = results.filter((r) => r.status === "fulfilled" && !r.value.error).length; const successful = results.filter((r) => r.status === "fulfilled" && !(r.value && r.value.error)).length;
const duplicateErrors = results.filter( const duplicateErrors = results.filter(
(r) => r.status === "rejected" || r?.value?.error?.includes("duplicate"), (r) => r.status === "rejected" || (r.value && r.value.error && r.value.error.includes("duplicate")),
).length; ).length;
return { return {