bat-markets 0.1.2

Futures-first headless Rust exchange engine with honest Binance/Bybit linear futures support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use std::{
    sync::atomic::{AtomicU64, Ordering},
    time::Duration,
};

use crate::client::BatMarkets;

/// Snapshot of observed lock wait/hold costs for shared engine state.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct LockDiagnosticsSnapshot {
    /// Number of lock observations included in this snapshot.
    pub operations: u64,
    /// Total observed time waiting to acquire the lock.
    pub wait_total_ns: u64,
    /// Maximum observed time waiting to acquire the lock.
    pub wait_max_ns: u64,
    /// Total observed time holding the lock.
    pub hold_total_ns: u64,
    /// Maximum observed time holding the lock.
    pub hold_max_ns: u64,
}

impl LockDiagnosticsSnapshot {
    /// Return the average lock wait time in nanoseconds.
    #[must_use]
    pub const fn average_wait_ns(&self) -> u64 {
        if self.operations == 0 {
            0
        } else {
            self.wait_total_ns / self.operations
        }
    }

    /// Return the average lock hold time in nanoseconds.
    #[must_use]
    pub const fn average_hold_ns(&self) -> u64 {
        if self.operations == 0 {
            0
        } else {
            self.hold_total_ns / self.operations
        }
    }
}

/// Snapshot of accumulated runtime latency for a named operation.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct LatencyDiagnosticsSnapshot {
    /// Number of operation observations included in this snapshot.
    pub operations: u64,
    /// Total observed operation latency.
    pub total_ns: u64,
    /// Maximum observed operation latency.
    pub max_ns: u64,
}

impl LatencyDiagnosticsSnapshot {
    /// Return the average observed latency in nanoseconds.
    #[must_use]
    pub const fn average_ns(&self) -> u64 {
        if self.operations == 0 {
            0
        } else {
            self.total_ns / self.operations
        }
    }
}

/// Cheap runtime and lock diagnostics for live operator inspection.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct RuntimeDiagnosticsSnapshot {
    /// Shared-state read lock wait/hold costs.
    pub state_reads: LockDiagnosticsSnapshot,
    /// Shared-state write lock wait/hold costs.
    pub state_writes: LockDiagnosticsSnapshot,
    /// Live metadata refresh latency.
    pub refresh_metadata: LatencyDiagnosticsSnapshot,
    /// Public REST ticker fetch latency.
    pub fetch_ticker: LatencyDiagnosticsSnapshot,
    /// Public REST mark-price fetch latency.
    pub fetch_mark_price: LatencyDiagnosticsSnapshot,
    /// Public REST funding-rate fetch latency.
    pub fetch_funding_rate: LatencyDiagnosticsSnapshot,
    /// Public REST trade fetch latency.
    pub fetch_trades: LatencyDiagnosticsSnapshot,
    /// Public REST top-of-book fetch latency.
    pub fetch_book_top: LatencyDiagnosticsSnapshot,
    /// Public REST order-book fetch latency.
    pub fetch_order_book: LatencyDiagnosticsSnapshot,
    /// Public liquidation cache/fetch latency.
    pub fetch_liquidations: LatencyDiagnosticsSnapshot,
    /// Public REST OHLCV fetch latency.
    pub fetch_ohlcv: LatencyDiagnosticsSnapshot,
    /// Account refresh latency.
    pub refresh_account: LatencyDiagnosticsSnapshot,
    /// Position refresh latency.
    pub refresh_positions: LatencyDiagnosticsSnapshot,
    /// Open-order refresh latency.
    pub refresh_open_orders: LatencyDiagnosticsSnapshot,
    /// Execution-history refresh latency.
    pub refresh_executions: LatencyDiagnosticsSnapshot,
    /// Single-order REST lookup latency.
    pub get_order: LatencyDiagnosticsSnapshot,
    /// Create-order command latency.
    pub create_order: LatencyDiagnosticsSnapshot,
    /// Batch create-order command latency.
    pub create_orders: LatencyDiagnosticsSnapshot,
    /// Amend-order command latency.
    pub amend_order: LatencyDiagnosticsSnapshot,
    /// Batch amend-order command latency.
    pub amend_orders: LatencyDiagnosticsSnapshot,
    /// Cancel-order command latency.
    pub cancel_order: LatencyDiagnosticsSnapshot,
    /// Batch cancel-order command latency.
    pub cancel_orders: LatencyDiagnosticsSnapshot,
    /// Cancel-all-orders command latency.
    pub cancel_all_orders: LatencyDiagnosticsSnapshot,
    /// Close-position command latency.
    pub close_position: LatencyDiagnosticsSnapshot,
    /// Validate-order command latency.
    pub validate_order: LatencyDiagnosticsSnapshot,
    /// Set-leverage command latency.
    pub set_leverage: LatencyDiagnosticsSnapshot,
    /// Set-margin-mode command latency.
    pub set_margin_mode: LatencyDiagnosticsSnapshot,
    /// Set-position-mode command latency.
    pub set_position_mode: LatencyDiagnosticsSnapshot,
    /// Private reconcile cycle latency.
    pub reconcile_private: LatencyDiagnosticsSnapshot,
    /// Open-interest refresh latency.
    pub refresh_open_interest: LatencyDiagnosticsSnapshot,
}

/// Read-only access to runtime and contention diagnostics.
pub struct DiagnosticsClient<'a> {
    inner: &'a BatMarkets,
}

impl<'a> DiagnosticsClient<'a> {
    pub(crate) const fn new(inner: &'a BatMarkets) -> Self {
        Self { inner }
    }

    /// Return a point-in-time diagnostics snapshot.
    ///
    /// The snapshot combines shared-state lock diagnostics with live runtime
    /// operation latency counters.
    #[must_use]
    pub fn snapshot(&self) -> RuntimeDiagnosticsSnapshot {
        let mut snapshot = self.inner.runtime_state.diagnostics.snapshot();
        snapshot.state_reads = self.inner.shared.read_diagnostics();
        snapshot.state_writes = self.inner.shared.write_diagnostics();
        snapshot
    }
}

#[derive(Debug, Default)]
pub(crate) struct SharedStateDiagnostics {
    reads: AtomicLockDiagnostics,
    writes: AtomicLockDiagnostics,
}

impl SharedStateDiagnostics {
    pub(crate) fn observe_read(&self, wait: Duration, hold: Duration) {
        self.reads.observe(wait, hold);
    }

    pub(crate) fn observe_write(&self, wait: Duration, hold: Duration) {
        self.writes.observe(wait, hold);
    }

    pub(crate) fn read_snapshot(&self) -> LockDiagnosticsSnapshot {
        self.reads.snapshot()
    }

    pub(crate) fn write_snapshot(&self) -> LockDiagnosticsSnapshot {
        self.writes.snapshot()
    }
}

#[derive(Debug, Default)]
pub(crate) struct RuntimeDiagnosticsState {
    refresh_metadata: AtomicLatencyDiagnostics,
    fetch_ticker: AtomicLatencyDiagnostics,
    fetch_mark_price: AtomicLatencyDiagnostics,
    fetch_funding_rate: AtomicLatencyDiagnostics,
    fetch_trades: AtomicLatencyDiagnostics,
    fetch_book_top: AtomicLatencyDiagnostics,
    fetch_order_book: AtomicLatencyDiagnostics,
    fetch_liquidations: AtomicLatencyDiagnostics,
    fetch_ohlcv: AtomicLatencyDiagnostics,
    refresh_account: AtomicLatencyDiagnostics,
    refresh_positions: AtomicLatencyDiagnostics,
    refresh_open_orders: AtomicLatencyDiagnostics,
    refresh_executions: AtomicLatencyDiagnostics,
    get_order: AtomicLatencyDiagnostics,
    create_order: AtomicLatencyDiagnostics,
    create_orders: AtomicLatencyDiagnostics,
    amend_order: AtomicLatencyDiagnostics,
    amend_orders: AtomicLatencyDiagnostics,
    cancel_order: AtomicLatencyDiagnostics,
    cancel_orders: AtomicLatencyDiagnostics,
    cancel_all_orders: AtomicLatencyDiagnostics,
    close_position: AtomicLatencyDiagnostics,
    validate_order: AtomicLatencyDiagnostics,
    set_leverage: AtomicLatencyDiagnostics,
    set_margin_mode: AtomicLatencyDiagnostics,
    set_position_mode: AtomicLatencyDiagnostics,
    reconcile_private: AtomicLatencyDiagnostics,
    refresh_open_interest: AtomicLatencyDiagnostics,
}

impl RuntimeDiagnosticsState {
    pub(crate) fn observe(&self, operation: RuntimeOperation, elapsed: Duration) {
        let diagnostics = match operation {
            RuntimeOperation::RefreshMetadata => &self.refresh_metadata,
            RuntimeOperation::FetchTicker => &self.fetch_ticker,
            RuntimeOperation::FetchMarkPrice => &self.fetch_mark_price,
            RuntimeOperation::FetchFundingRate => &self.fetch_funding_rate,
            RuntimeOperation::FetchTrades => &self.fetch_trades,
            RuntimeOperation::FetchBookTop => &self.fetch_book_top,
            RuntimeOperation::FetchOrderBook => &self.fetch_order_book,
            RuntimeOperation::FetchLiquidations => &self.fetch_liquidations,
            RuntimeOperation::FetchOhlcv => &self.fetch_ohlcv,
            RuntimeOperation::RefreshAccount => &self.refresh_account,
            RuntimeOperation::RefreshPositions => &self.refresh_positions,
            RuntimeOperation::RefreshOpenOrders => &self.refresh_open_orders,
            RuntimeOperation::RefreshExecutions => &self.refresh_executions,
            RuntimeOperation::GetOrder => &self.get_order,
            RuntimeOperation::CreateOrder => &self.create_order,
            RuntimeOperation::CreateOrders => &self.create_orders,
            RuntimeOperation::AmendOrder => &self.amend_order,
            RuntimeOperation::AmendOrders => &self.amend_orders,
            RuntimeOperation::CancelOrder => &self.cancel_order,
            RuntimeOperation::CancelOrders => &self.cancel_orders,
            RuntimeOperation::CancelAllOrders => &self.cancel_all_orders,
            RuntimeOperation::ClosePosition => &self.close_position,
            RuntimeOperation::ValidateOrder => &self.validate_order,
            RuntimeOperation::SetLeverage => &self.set_leverage,
            RuntimeOperation::SetMarginMode => &self.set_margin_mode,
            RuntimeOperation::SetPositionMode => &self.set_position_mode,
            RuntimeOperation::ReconcilePrivate => &self.reconcile_private,
            RuntimeOperation::RefreshOpenInterest => &self.refresh_open_interest,
        };
        diagnostics.observe(elapsed);
    }

    pub(crate) fn snapshot(&self) -> RuntimeDiagnosticsSnapshot {
        RuntimeDiagnosticsSnapshot {
            state_reads: LockDiagnosticsSnapshot::default(),
            state_writes: LockDiagnosticsSnapshot::default(),
            refresh_metadata: self.refresh_metadata.snapshot(),
            fetch_ticker: self.fetch_ticker.snapshot(),
            fetch_mark_price: self.fetch_mark_price.snapshot(),
            fetch_funding_rate: self.fetch_funding_rate.snapshot(),
            fetch_trades: self.fetch_trades.snapshot(),
            fetch_book_top: self.fetch_book_top.snapshot(),
            fetch_order_book: self.fetch_order_book.snapshot(),
            fetch_liquidations: self.fetch_liquidations.snapshot(),
            fetch_ohlcv: self.fetch_ohlcv.snapshot(),
            refresh_account: self.refresh_account.snapshot(),
            refresh_positions: self.refresh_positions.snapshot(),
            refresh_open_orders: self.refresh_open_orders.snapshot(),
            refresh_executions: self.refresh_executions.snapshot(),
            get_order: self.get_order.snapshot(),
            create_order: self.create_order.snapshot(),
            create_orders: self.create_orders.snapshot(),
            amend_order: self.amend_order.snapshot(),
            amend_orders: self.amend_orders.snapshot(),
            cancel_order: self.cancel_order.snapshot(),
            cancel_orders: self.cancel_orders.snapshot(),
            cancel_all_orders: self.cancel_all_orders.snapshot(),
            close_position: self.close_position.snapshot(),
            validate_order: self.validate_order.snapshot(),
            set_leverage: self.set_leverage.snapshot(),
            set_margin_mode: self.set_margin_mode.snapshot(),
            set_position_mode: self.set_position_mode.snapshot(),
            reconcile_private: self.reconcile_private.snapshot(),
            refresh_open_interest: self.refresh_open_interest.snapshot(),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum RuntimeOperation {
    RefreshMetadata,
    FetchTicker,
    FetchMarkPrice,
    FetchFundingRate,
    FetchTrades,
    FetchBookTop,
    FetchOrderBook,
    FetchLiquidations,
    FetchOhlcv,
    RefreshAccount,
    RefreshPositions,
    RefreshOpenOrders,
    RefreshExecutions,
    GetOrder,
    CreateOrder,
    CreateOrders,
    AmendOrder,
    AmendOrders,
    CancelOrder,
    CancelOrders,
    CancelAllOrders,
    ClosePosition,
    ValidateOrder,
    SetLeverage,
    SetMarginMode,
    SetPositionMode,
    ReconcilePrivate,
    RefreshOpenInterest,
}

#[derive(Debug, Default)]
struct AtomicLockDiagnostics {
    operations: AtomicU64,
    wait_total_ns: AtomicU64,
    wait_max_ns: AtomicU64,
    hold_total_ns: AtomicU64,
    hold_max_ns: AtomicU64,
}

impl AtomicLockDiagnostics {
    fn observe(&self, wait: Duration, hold: Duration) {
        let wait_ns = duration_ns(wait);
        let hold_ns = duration_ns(hold);
        self.operations.fetch_add(1, Ordering::Relaxed);
        self.wait_total_ns.fetch_add(wait_ns, Ordering::Relaxed);
        self.hold_total_ns.fetch_add(hold_ns, Ordering::Relaxed);
        update_max(&self.wait_max_ns, wait_ns);
        update_max(&self.hold_max_ns, hold_ns);
    }

    fn snapshot(&self) -> LockDiagnosticsSnapshot {
        LockDiagnosticsSnapshot {
            operations: self.operations.load(Ordering::Relaxed),
            wait_total_ns: self.wait_total_ns.load(Ordering::Relaxed),
            wait_max_ns: self.wait_max_ns.load(Ordering::Relaxed),
            hold_total_ns: self.hold_total_ns.load(Ordering::Relaxed),
            hold_max_ns: self.hold_max_ns.load(Ordering::Relaxed),
        }
    }
}

#[derive(Debug, Default)]
struct AtomicLatencyDiagnostics {
    operations: AtomicU64,
    total_ns: AtomicU64,
    max_ns: AtomicU64,
}

impl AtomicLatencyDiagnostics {
    fn observe(&self, elapsed: Duration) {
        let elapsed_ns = duration_ns(elapsed);
        self.operations.fetch_add(1, Ordering::Relaxed);
        self.total_ns.fetch_add(elapsed_ns, Ordering::Relaxed);
        update_max(&self.max_ns, elapsed_ns);
    }

    fn snapshot(&self) -> LatencyDiagnosticsSnapshot {
        LatencyDiagnosticsSnapshot {
            operations: self.operations.load(Ordering::Relaxed),
            total_ns: self.total_ns.load(Ordering::Relaxed),
            max_ns: self.max_ns.load(Ordering::Relaxed),
        }
    }
}

fn duration_ns(duration: Duration) -> u64 {
    duration.as_nanos().min(u128::from(u64::MAX)) as u64
}

fn update_max(cell: &AtomicU64, sample: u64) {
    let mut current = cell.load(Ordering::Relaxed);
    while sample > current {
        match cell.compare_exchange_weak(current, sample, Ordering::Relaxed, Ordering::Relaxed) {
            Ok(_) => return,
            Err(observed) => current = observed,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::{RuntimeDiagnosticsState, RuntimeOperation};
    use crate::BatMarketsBuilder;
    use bat_markets_core::{Product, Venue};

    #[test]
    fn diagnostics_snapshot_tracks_state_lock_activity() {
        let client = BatMarketsBuilder::default()
            .venue(Venue::Binance)
            .product(Product::LinearUsdt)
            .build()
            .expect("fixture client should build");

        let before = client.diagnostics().snapshot();
        assert_eq!(before.state_reads.operations, 0);
        assert_eq!(before.state_writes.operations, 0);

        let _ = client.market().instrument_specs();
        client.write_state(|state| state.mark_rest_success(None));

        let after = client.diagnostics().snapshot();
        assert!(after.state_reads.operations >= 1);
        assert!(after.state_writes.operations >= 1);
    }

    #[test]
    fn runtime_latency_snapshot_accumulates_average_and_max() {
        let diagnostics = RuntimeDiagnosticsState::default();
        diagnostics.observe(
            RuntimeOperation::ReconcilePrivate,
            Duration::from_micros(100),
        );
        diagnostics.observe(
            RuntimeOperation::ReconcilePrivate,
            Duration::from_micros(300),
        );

        let snapshot = diagnostics.snapshot();
        assert_eq!(snapshot.reconcile_private.operations, 2);
        assert_eq!(snapshot.reconcile_private.average_ns(), 200_000);
        assert_eq!(snapshot.reconcile_private.max_ns, 300_000);
    }
}