sol-trade-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +390 -0
- package/dist/chunk-MMQAMIKR.mjs +3735 -0
- package/dist/chunk-NEZDFAYA.mjs +7744 -0
- package/dist/clients-VITWK7B6.mjs +1370 -0
- package/dist/index-1BK_FXsW.d.mts +2327 -0
- package/dist/index-1BK_FXsW.d.ts +2327 -0
- package/dist/index.d.mts +2659 -0
- package/dist/index.d.ts +2659 -0
- package/dist/index.js +13265 -0
- package/dist/index.mjs +562 -0
- package/dist/perf/index.d.mts +2 -0
- package/dist/perf/index.d.ts +2 -0
- package/dist/perf/index.js +3742 -0
- package/dist/perf/index.mjs +214 -0
- package/package.json +101 -0
- package/src/__tests__/complete_sdk.test.ts +354 -0
- package/src/__tests__/hotpath.test.ts +486 -0
- package/src/__tests__/nonce.test.ts +45 -0
- package/src/__tests__/sdk.test.ts +425 -0
- package/src/address-lookup/index.ts +197 -0
- package/src/cache/cache.ts +308 -0
- package/src/calc/index.ts +1058 -0
- package/src/calc/pumpfun.ts +124 -0
- package/src/common/bonding_curve.ts +272 -0
- package/src/common/compute-budget.ts +148 -0
- package/src/common/confirm-any-signature.ts +184 -0
- package/src/common/fast-timing.ts +481 -0
- package/src/common/fast_fn.ts +150 -0
- package/src/common/gas-fee-strategy.ts +253 -0
- package/src/common/map-pool.ts +23 -0
- package/src/common/nonce.ts +40 -0
- package/src/common/sdk-log.ts +460 -0
- package/src/common/seed.ts +381 -0
- package/src/common/spl-token.ts +578 -0
- package/src/common/subscription-handle.ts +644 -0
- package/src/common/trading-utils.ts +239 -0
- package/src/common/wsol-manager.ts +325 -0
- package/src/compute/compute_budget_manager.ts +187 -0
- package/src/compute/index.ts +21 -0
- package/src/constants/index.ts +96 -0
- package/src/execution/execution.ts +532 -0
- package/src/execution/index.ts +42 -0
- package/src/hotpath/executor.ts +464 -0
- package/src/hotpath/index.ts +64 -0
- package/src/hotpath/state.ts +435 -0
- package/src/index.ts +2117 -0
- package/src/instruction/bonk_builder.ts +730 -0
- package/src/instruction/index.ts +24 -0
- package/src/instruction/meteora_damm_v2_builder.ts +509 -0
- package/src/instruction/pumpfun_builder.ts +1183 -0
- package/src/instruction/pumpswap.ts +1123 -0
- package/src/instruction/raydium_amm_v4_builder.ts +692 -0
- package/src/instruction/raydium_cpmm_builder.ts +795 -0
- package/src/middleware/traits.ts +407 -0
- package/src/params/index.ts +483 -0
- package/src/perf/compiler-optimization.ts +529 -0
- package/src/perf/hardware.ts +631 -0
- package/src/perf/index.ts +9 -0
- package/src/perf/kernel-bypass.ts +656 -0
- package/src/perf/protocol.ts +682 -0
- package/src/perf/realtime.ts +592 -0
- package/src/perf/simd.ts +668 -0
- package/src/perf/syscall-bypass.ts +331 -0
- package/src/perf/ultra-low-latency.ts +505 -0
- package/src/perf/zero-copy.ts +589 -0
- package/src/pool/pool.ts +294 -0
- package/src/rpc/client.ts +345 -0
- package/src/sdk-errors.ts +13 -0
- package/src/security/index.ts +26 -0
- package/src/security/secure-key.ts +303 -0
- package/src/security/validators.ts +281 -0
- package/src/seed/pda.ts +262 -0
- package/src/serialization/index.ts +28 -0
- package/src/serialization/serialization.ts +288 -0
- package/src/swqos/clients.ts +1754 -0
- package/src/swqos/index.ts +50 -0
- package/src/swqos/providers.ts +1707 -0
- package/src/trading/core/async-executor.ts +702 -0
- package/src/trading/core/confirmation-monitor.ts +711 -0
- package/src/trading/core/index.ts +82 -0
- package/src/trading/core/retry-handler.ts +683 -0
- package/src/trading/core/transaction-pool.ts +780 -0
- package/src/trading/executor.ts +385 -0
- package/src/trading/factory.ts +282 -0
- package/src/trading/index.ts +30 -0
- package/src/types.ts +8 -0
- package/src/utils/index.ts +155 -0
|
@@ -0,0 +1,592 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Realtime Tuning Module for Sol Trade SDK
|
|
3
|
+
* Provides realtime configuration, thread priority, and performance tuning.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// ===== Types =====
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Realtime configuration
|
|
10
|
+
*/
|
|
11
|
+
export interface RealtimeConfig {
|
|
12
|
+
/** Enable realtime mode */
|
|
13
|
+
enableRealtime: boolean;
|
|
14
|
+
/** Target latency in microseconds */
|
|
15
|
+
targetLatencyUs: number;
|
|
16
|
+
/** CPU core for pinning */
|
|
17
|
+
cpuCore: number;
|
|
18
|
+
/** Thread priority level */
|
|
19
|
+
priority: ThreadPriorityLevel;
|
|
20
|
+
/** Enable busy waiting */
|
|
21
|
+
busyWait: boolean;
|
|
22
|
+
/** Scheduler policy */
|
|
23
|
+
scheduler: SchedulerPolicy;
|
|
24
|
+
/** Memory lock */
|
|
25
|
+
lockMemory: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Thread priority levels
|
|
30
|
+
*/
|
|
31
|
+
export enum ThreadPriorityLevel {
|
|
32
|
+
Idle = 0,
|
|
33
|
+
Lowest = 1,
|
|
34
|
+
Low = 2,
|
|
35
|
+
Normal = 3,
|
|
36
|
+
High = 4,
|
|
37
|
+
Highest = 5,
|
|
38
|
+
Realtime = 6,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Scheduler policies
|
|
43
|
+
*/
|
|
44
|
+
export enum SchedulerPolicy {
|
|
45
|
+
/** Normal scheduler */
|
|
46
|
+
Normal = 'normal',
|
|
47
|
+
/** FIFO scheduler */
|
|
48
|
+
FIFO = 'fifo',
|
|
49
|
+
/** Round-robin scheduler */
|
|
50
|
+
RoundRobin = 'rr',
|
|
51
|
+
/** Batch scheduler */
|
|
52
|
+
Batch = 'batch',
|
|
53
|
+
/** Idle scheduler */
|
|
54
|
+
Idle = 'idle',
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Default realtime configuration
|
|
59
|
+
*/
|
|
60
|
+
export function defaultRealtimeConfig(): RealtimeConfig {
|
|
61
|
+
return {
|
|
62
|
+
enableRealtime: false,
|
|
63
|
+
targetLatencyUs: 100, // 100 microseconds
|
|
64
|
+
cpuCore: -1, // No pinning
|
|
65
|
+
priority: ThreadPriorityLevel.High,
|
|
66
|
+
busyWait: false,
|
|
67
|
+
scheduler: SchedulerPolicy.Normal,
|
|
68
|
+
lockMemory: false,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Tuning statistics
|
|
74
|
+
*/
|
|
75
|
+
export interface TuningStats {
|
|
76
|
+
adjustments: number;
|
|
77
|
+
averageLatencyUs: number;
|
|
78
|
+
minLatencyUs: number;
|
|
79
|
+
maxLatencyUs: number;
|
|
80
|
+
violations: number;
|
|
81
|
+
lastAdjustmentTime: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ===== Thread Priority =====
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Thread priority manager for realtime performance.
|
|
88
|
+
* Controls execution priority of trading threads.
|
|
89
|
+
*/
|
|
90
|
+
export class ThreadPriority {
|
|
91
|
+
private currentPriority: ThreadPriorityLevel = ThreadPriorityLevel.Normal;
|
|
92
|
+
private config: RealtimeConfig;
|
|
93
|
+
|
|
94
|
+
constructor(config: RealtimeConfig = defaultRealtimeConfig()) {
|
|
95
|
+
this.config = config;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Set thread priority
|
|
100
|
+
*/
|
|
101
|
+
setPriority(priority: ThreadPriorityLevel): boolean {
|
|
102
|
+
// In Node.js, we can't directly set thread priority
|
|
103
|
+
// This is a placeholder for potential native addon integration
|
|
104
|
+
this.currentPriority = priority;
|
|
105
|
+
|
|
106
|
+
// Log the priority change
|
|
107
|
+
if (this.config.enableRealtime) {
|
|
108
|
+
console.log(`Thread priority set to: ${ThreadPriorityLevel[priority]}`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Get current priority
|
|
116
|
+
*/
|
|
117
|
+
getPriority(): ThreadPriorityLevel {
|
|
118
|
+
return this.currentPriority;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Increase priority by one level
|
|
123
|
+
*/
|
|
124
|
+
increasePriority(): boolean {
|
|
125
|
+
if (this.currentPriority < ThreadPriorityLevel.Realtime) {
|
|
126
|
+
return this.setPriority(this.currentPriority + 1);
|
|
127
|
+
}
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Decrease priority by one level
|
|
133
|
+
*/
|
|
134
|
+
decreasePriority(): boolean {
|
|
135
|
+
if (this.currentPriority > ThreadPriorityLevel.Idle) {
|
|
136
|
+
return this.setPriority(this.currentPriority - 1);
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Set realtime priority
|
|
143
|
+
*/
|
|
144
|
+
setRealtime(): boolean {
|
|
145
|
+
return this.setPriority(ThreadPriorityLevel.Realtime);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Set normal priority
|
|
150
|
+
*/
|
|
151
|
+
setNormal(): boolean {
|
|
152
|
+
return this.setPriority(ThreadPriorityLevel.Normal);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Check if running at realtime priority
|
|
157
|
+
*/
|
|
158
|
+
isRealtime(): boolean {
|
|
159
|
+
return this.currentPriority === ThreadPriorityLevel.Realtime;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// ===== Realtime Tuner =====
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Realtime performance tuner for trading systems.
|
|
167
|
+
* Automatically adjusts system parameters for optimal latency.
|
|
168
|
+
*/
|
|
169
|
+
export class RealtimeTuner {
|
|
170
|
+
private config: RealtimeConfig;
|
|
171
|
+
private running: boolean = false;
|
|
172
|
+
private tunerInterval: NodeJS.Timeout | null = null;
|
|
173
|
+
private latencies: number[] = [];
|
|
174
|
+
private stats: TuningStats;
|
|
175
|
+
private callbacks: Array<(stats: TuningStats) => void> = [];
|
|
176
|
+
private lastAdjustment: number = 0;
|
|
177
|
+
private adjustmentCooldownMs: number = 1000;
|
|
178
|
+
|
|
179
|
+
constructor(config: RealtimeConfig = defaultRealtimeConfig()) {
|
|
180
|
+
this.config = config;
|
|
181
|
+
this.stats = {
|
|
182
|
+
adjustments: 0,
|
|
183
|
+
averageLatencyUs: 0,
|
|
184
|
+
minLatencyUs: Infinity,
|
|
185
|
+
maxLatencyUs: 0,
|
|
186
|
+
violations: 0,
|
|
187
|
+
lastAdjustmentTime: 0,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Start the realtime tuner
|
|
193
|
+
*/
|
|
194
|
+
start(): void {
|
|
195
|
+
if (this.running) return;
|
|
196
|
+
this.running = true;
|
|
197
|
+
|
|
198
|
+
// Apply initial configuration
|
|
199
|
+
this.applyConfiguration();
|
|
200
|
+
|
|
201
|
+
// Start monitoring loop
|
|
202
|
+
this.tunerInterval = setInterval(() => {
|
|
203
|
+
this.tune();
|
|
204
|
+
}, 100); // 100ms tuning interval
|
|
205
|
+
|
|
206
|
+
console.log('Realtime tuner started');
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Stop the realtime tuner
|
|
211
|
+
*/
|
|
212
|
+
stop(): void {
|
|
213
|
+
this.running = false;
|
|
214
|
+
|
|
215
|
+
if (this.tunerInterval) {
|
|
216
|
+
clearInterval(this.tunerInterval);
|
|
217
|
+
this.tunerInterval = null;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
console.log('Realtime tuner stopped');
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Record a latency measurement
|
|
225
|
+
*/
|
|
226
|
+
recordLatency(latencyUs: number): void {
|
|
227
|
+
this.latencies.push(latencyUs);
|
|
228
|
+
|
|
229
|
+
// Keep last 1000 measurements
|
|
230
|
+
if (this.latencies.length > 1000) {
|
|
231
|
+
this.latencies.shift();
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Check for violation
|
|
235
|
+
if (latencyUs > this.config.targetLatencyUs) {
|
|
236
|
+
this.stats.violations++;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Update stats
|
|
240
|
+
this.stats.minLatencyUs = Math.min(this.stats.minLatencyUs, latencyUs);
|
|
241
|
+
this.stats.maxLatencyUs = Math.max(this.stats.maxLatencyUs, latencyUs);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Get current tuning statistics
|
|
246
|
+
*/
|
|
247
|
+
getStats(): TuningStats {
|
|
248
|
+
if (this.latencies.length > 0) {
|
|
249
|
+
this.stats.averageLatencyUs =
|
|
250
|
+
this.latencies.reduce((a, b) => a + b, 0) / this.latencies.length;
|
|
251
|
+
}
|
|
252
|
+
return { ...this.stats };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Register a callback for tuning events
|
|
257
|
+
*/
|
|
258
|
+
onTune(callback: (stats: TuningStats) => void): void {
|
|
259
|
+
this.callbacks.push(callback);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Reset statistics
|
|
264
|
+
*/
|
|
265
|
+
resetStats(): void {
|
|
266
|
+
this.latencies = [];
|
|
267
|
+
this.stats = {
|
|
268
|
+
adjustments: 0,
|
|
269
|
+
averageLatencyUs: 0,
|
|
270
|
+
minLatencyUs: Infinity,
|
|
271
|
+
maxLatencyUs: 0,
|
|
272
|
+
violations: 0,
|
|
273
|
+
lastAdjustmentTime: 0,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private applyConfiguration(): void {
|
|
278
|
+
// Set thread priority
|
|
279
|
+
const priority = new ThreadPriority(this.config);
|
|
280
|
+
priority.setPriority(this.config.priority);
|
|
281
|
+
|
|
282
|
+
// Log configuration
|
|
283
|
+
console.log('Realtime configuration applied:', {
|
|
284
|
+
targetLatencyUs: this.config.targetLatencyUs,
|
|
285
|
+
cpuCore: this.config.cpuCore,
|
|
286
|
+
priority: ThreadPriorityLevel[this.config.priority],
|
|
287
|
+
busyWait: this.config.busyWait,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
private tune(): void {
|
|
292
|
+
if (!this.running) return;
|
|
293
|
+
|
|
294
|
+
const now = Date.now();
|
|
295
|
+
if (now - this.lastAdjustment < this.adjustmentCooldownMs) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Calculate current average latency
|
|
300
|
+
if (this.latencies.length === 0) return;
|
|
301
|
+
|
|
302
|
+
const avgLatency = this.latencies.reduce((a, b) => a + b, 0) / this.latencies.length;
|
|
303
|
+
this.stats.averageLatencyUs = avgLatency;
|
|
304
|
+
|
|
305
|
+
// Check if tuning is needed
|
|
306
|
+
const violationRate = this.stats.violations / this.latencies.length;
|
|
307
|
+
|
|
308
|
+
if (violationRate > 0.1) {
|
|
309
|
+
// More than 10% violations, need adjustment
|
|
310
|
+
this.adjustForLowerLatency();
|
|
311
|
+
this.stats.adjustments++;
|
|
312
|
+
this.stats.lastAdjustmentTime = now;
|
|
313
|
+
this.lastAdjustment = now;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Notify callbacks
|
|
317
|
+
for (const callback of this.callbacks) {
|
|
318
|
+
try {
|
|
319
|
+
callback(this.getStats());
|
|
320
|
+
} catch (error) {
|
|
321
|
+
console.error('Tuning callback error:', error);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
private adjustForLowerLatency(): void {
|
|
327
|
+
// In a real implementation, this would adjust:
|
|
328
|
+
// - CPU frequency governor
|
|
329
|
+
// - Process priority
|
|
330
|
+
// - Memory allocation strategy
|
|
331
|
+
// - Network interrupt coalescing
|
|
332
|
+
// - etc.
|
|
333
|
+
|
|
334
|
+
console.log('Adjusting for lower latency...');
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// ===== Busy Wait Utilities =====
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Busy wait for a precise amount of time.
|
|
342
|
+
* More accurate than setTimeout for short durations.
|
|
343
|
+
*/
|
|
344
|
+
export function busyWait(microseconds: number): void {
|
|
345
|
+
const start = process.hrtime.bigint();
|
|
346
|
+
const target = start + BigInt(microseconds) * BigInt(1000);
|
|
347
|
+
|
|
348
|
+
while (process.hrtime.bigint() < target) {
|
|
349
|
+
// Busy wait
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Spin lock with timeout
|
|
355
|
+
*/
|
|
356
|
+
export function spinLock(condition: () => boolean, timeoutUs: number): boolean {
|
|
357
|
+
const start = process.hrtime.bigint();
|
|
358
|
+
const timeoutNs = BigInt(timeoutUs) * BigInt(1000);
|
|
359
|
+
|
|
360
|
+
while (!condition()) {
|
|
361
|
+
if (process.hrtime.bigint() - start > timeoutNs) {
|
|
362
|
+
return false; // Timeout
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
return true;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// ===== Latency Monitor =====
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Latency monitoring for realtime systems
|
|
373
|
+
*/
|
|
374
|
+
export class LatencyMonitor {
|
|
375
|
+
private measurements: number[] = [];
|
|
376
|
+
private maxMeasurements: number;
|
|
377
|
+
private histogram: Map<number, number> = new Map();
|
|
378
|
+
private bucketSize: number;
|
|
379
|
+
|
|
380
|
+
constructor(maxMeasurements: number = 10000, bucketSize: number = 10) {
|
|
381
|
+
this.maxMeasurements = maxMeasurements;
|
|
382
|
+
this.bucketSize = bucketSize;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Record a latency measurement
|
|
387
|
+
*/
|
|
388
|
+
record(latencyUs: number): void {
|
|
389
|
+
this.measurements.push(latencyUs);
|
|
390
|
+
|
|
391
|
+
if (this.measurements.length > this.maxMeasurements) {
|
|
392
|
+
this.measurements.shift();
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Update histogram
|
|
396
|
+
const bucket = Math.floor(latencyUs / this.bucketSize) * this.bucketSize;
|
|
397
|
+
const count = this.histogram.get(bucket) || 0;
|
|
398
|
+
this.histogram.set(bucket, count + 1);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Get percentile latency
|
|
403
|
+
*/
|
|
404
|
+
getPercentile(percentile: number): number {
|
|
405
|
+
if (this.measurements.length === 0) return 0;
|
|
406
|
+
|
|
407
|
+
const sorted = [...this.measurements].sort((a, b) => a - b);
|
|
408
|
+
const index = Math.floor((percentile / 100) * sorted.length);
|
|
409
|
+
return sorted[Math.min(index, sorted.length - 1)] ?? 0;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Get average latency
|
|
414
|
+
*/
|
|
415
|
+
getAverage(): number {
|
|
416
|
+
if (this.measurements.length === 0) return 0;
|
|
417
|
+
return this.measurements.reduce((a, b) => a + b, 0) / this.measurements.length;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Get minimum latency
|
|
422
|
+
*/
|
|
423
|
+
getMin(): number {
|
|
424
|
+
if (this.measurements.length === 0) return 0;
|
|
425
|
+
return Math.min(...this.measurements);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Get maximum latency
|
|
430
|
+
*/
|
|
431
|
+
getMax(): number {
|
|
432
|
+
if (this.measurements.length === 0) return 0;
|
|
433
|
+
return Math.max(...this.measurements);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Get histogram
|
|
438
|
+
*/
|
|
439
|
+
getHistogram(): Map<number, number> {
|
|
440
|
+
return new Map(this.histogram);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Reset all measurements
|
|
445
|
+
*/
|
|
446
|
+
reset(): void {
|
|
447
|
+
this.measurements = [];
|
|
448
|
+
this.histogram.clear();
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Get summary statistics
|
|
453
|
+
*/
|
|
454
|
+
getSummary(): {
|
|
455
|
+
count: number;
|
|
456
|
+
min: number;
|
|
457
|
+
max: number;
|
|
458
|
+
avg: number;
|
|
459
|
+
p50: number;
|
|
460
|
+
p95: number;
|
|
461
|
+
p99: number;
|
|
462
|
+
p999: number;
|
|
463
|
+
} {
|
|
464
|
+
return {
|
|
465
|
+
count: this.measurements.length,
|
|
466
|
+
min: this.getMin(),
|
|
467
|
+
max: this.getMax(),
|
|
468
|
+
avg: this.getAverage(),
|
|
469
|
+
p50: this.getPercentile(50),
|
|
470
|
+
p95: this.getPercentile(95),
|
|
471
|
+
p99: this.getPercentile(99),
|
|
472
|
+
p999: this.getPercentile(99.9),
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// ===== Performance Governor =====
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Performance governor for trading systems
|
|
481
|
+
*/
|
|
482
|
+
export class PerformanceGovernor {
|
|
483
|
+
private targetThroughput: number = 0;
|
|
484
|
+
private currentThroughput: number = 0;
|
|
485
|
+
private adjustments: number = 0;
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Set target throughput (operations per second)
|
|
489
|
+
*/
|
|
490
|
+
setTargetThroughput(opsPerSecond: number): void {
|
|
491
|
+
this.targetThroughput = opsPerSecond;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Report actual throughput
|
|
496
|
+
*/
|
|
497
|
+
reportThroughput(opsPerSecond: number): void {
|
|
498
|
+
this.currentThroughput = opsPerSecond;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Get recommended batch size
|
|
503
|
+
*/
|
|
504
|
+
getRecommendedBatchSize(): number {
|
|
505
|
+
if (this.targetThroughput === 0) return 1;
|
|
506
|
+
|
|
507
|
+
const ratio = this.currentThroughput / this.targetThroughput;
|
|
508
|
+
|
|
509
|
+
if (ratio < 0.5) {
|
|
510
|
+
return 1; // Underperforming, reduce batch size
|
|
511
|
+
} else if (ratio > 1.5) {
|
|
512
|
+
return 10; // Overperforming, increase batch size
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
return 5; // Optimal range
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Get adjustment count
|
|
520
|
+
*/
|
|
521
|
+
getAdjustments(): number {
|
|
522
|
+
return this.adjustments;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// ===== Convenience Functions =====
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Create a realtime tuner
|
|
530
|
+
*/
|
|
531
|
+
export function createRealtimeTuner(config?: Partial<RealtimeConfig>): RealtimeTuner {
|
|
532
|
+
const fullConfig = { ...defaultRealtimeConfig(), ...config };
|
|
533
|
+
return new RealtimeTuner(fullConfig);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Create a thread priority manager
|
|
538
|
+
*/
|
|
539
|
+
export function createThreadPriority(config?: Partial<RealtimeConfig>): ThreadPriority {
|
|
540
|
+
const fullConfig = { ...defaultRealtimeConfig(), ...config };
|
|
541
|
+
return new ThreadPriority(fullConfig);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Create a latency monitor
|
|
546
|
+
*/
|
|
547
|
+
export function createLatencyMonitor(
|
|
548
|
+
maxMeasurements?: number,
|
|
549
|
+
bucketSize?: number
|
|
550
|
+
): LatencyMonitor {
|
|
551
|
+
return new LatencyMonitor(maxMeasurements, bucketSize);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Create a performance governor
|
|
556
|
+
*/
|
|
557
|
+
export function createPerformanceGovernor(): PerformanceGovernor {
|
|
558
|
+
return new PerformanceGovernor();
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Measure function execution time
|
|
563
|
+
*/
|
|
564
|
+
export function measureTime<T>(fn: () => T): { result: T; elapsedUs: number } {
|
|
565
|
+
const start = process.hrtime.bigint();
|
|
566
|
+
const result = fn();
|
|
567
|
+
const elapsed = Number(process.hrtime.bigint() - start) / 1000; // Convert to microseconds
|
|
568
|
+
return { result, elapsedUs: elapsed };
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* Create a high-resolution timer
|
|
573
|
+
*/
|
|
574
|
+
export function createTimer(): {
|
|
575
|
+
start: () => void;
|
|
576
|
+
elapsed: () => number;
|
|
577
|
+
reset: () => void;
|
|
578
|
+
} {
|
|
579
|
+
let startTime: bigint = BigInt(0);
|
|
580
|
+
|
|
581
|
+
return {
|
|
582
|
+
start: () => {
|
|
583
|
+
startTime = process.hrtime.bigint();
|
|
584
|
+
},
|
|
585
|
+
elapsed: () => {
|
|
586
|
+
return Number(process.hrtime.bigint() - startTime) / 1000; // microseconds
|
|
587
|
+
},
|
|
588
|
+
reset: () => {
|
|
589
|
+
startTime = process.hrtime.bigint();
|
|
590
|
+
},
|
|
591
|
+
};
|
|
592
|
+
}
|