mirror of
https://github.com/luxfi/dex.git
synced 2026-09-24 00:26:31 +00:00
The matching engine (pkg/lx, 45.5K LOC incl. tests) and the deterministic core layered on it (pkg/dex) are one matcher spread across two packages (dex imported lx in 12 files; lx imported neither). Fold lx into dex and drop the "lx" name entirely — pkg/dex is now the single matcher package. Pure rename/fold, ZERO logic change. Behavior proven identical: the merged pkg/dex runs 457 top-level tests (431 former-lx + 26 former-dex) and 489 test funcs (468 + 21), all green — matching, batch (BatchMatchEqualsSequential, run-to-run determinism, tile boundary), GPU/CPU parity, truncation, and CL swap math all pass unchanged. What moved: - git mv all 101 pkg/lx/*.go + 1 .c into pkg/dex/ (package lx -> package dex). - lx's swap_test.go -> swap_math_test.go (only filename collision with dex's existing swap_test.go). - Rewrote imports repo-wide: 54 pkg/lx import lines removed; 42 external importers (api, client, consensus, dchain, dpdk, lxgpu, marketdata, test/, tests/) repointed to pkg/dex with lx.X -> dex.X; the 12 in-package dex files had lx.X -> bare X. 717 selector lines rewritten. Stale pkg/lx comment refs updated (pkg/lxgpu left intact). Collisions resolved (2, both package-level vars): - ErrNoLiquidity: lx's pool-level error (dexError, used only in swap.go, never referenced via qualifier) renamed ErrPoolNoLiquidity to match the existing ErrPool* convention; dex's router-level ErrNoLiquidity (public, has errors.Is tests) kept. - priceMultiplierBig: identical after fold (both big.NewInt(PriceMultiplier)); deduped by deleting dex's copy. Not folded — pkg/orderbook: it is NOT the GPU seam (that is orderbook_gpu.go, which lives in the engine and folded in here). pkg/orderbook is a zero-importer, placeholder-cgo island whose 13 core domain identifiers (Order, OrderBook, Trade, OrderType, OrderStatus, PriceLevel, Buy, Sell, Market, Limit, Filled, PartiallyFilled, NewOrderBook) collide incompatibly with the engine's. Folding it verbatim breaks the build; folding via rename reintroduces the exact parallel duplication this fold removes. Left for owner decision (recommend deletion as dead code) — see refactor notes. Follow-on (separate repos, not touched): 19 files under luxfi/dex import pkg/lx externally — lux/precompile/dex, lux/precompile-dex/dex, lux/consensus/examples/01-simple-bridge, lux/_consensus-v12537/examples, lux/cex/pkg/engine — repoint to pkg/dex after this lands. Verified: CGO_ENABLED=0 GOWORK=off go build ./... clean; go vet ./... clean; go test ./... -count=1 all green (22 ok / 0 fail). Co-authored-by: Hanzo Dev <[email protected]>
189 lines
4.7 KiB
Go
189 lines
4.7 KiB
Go
package tests
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/luxfi/dex/pkg/dex"
|
|
)
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 1. BenchmarkOrderInsert — Single order insertion latency
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func BenchmarkOrderInsert(b *testing.B) {
|
|
ob := dex.NewOrderBook("BENCH-INSERT")
|
|
|
|
b.ResetTimer()
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
order := &dex.Order{
|
|
Symbol: "BENCH-INSERT",
|
|
Type: dex.Limit,
|
|
Side: dex.Side(i % 2),
|
|
Price: 100.0 + float64(i%100),
|
|
Size: 1.0,
|
|
UserID: "bench",
|
|
}
|
|
ob.AddOrder(order)
|
|
}
|
|
|
|
b.ReportMetric(float64(b.N)/b.Elapsed().Seconds(), "orders/sec")
|
|
b.ReportMetric(float64(b.Elapsed().Nanoseconds())/float64(b.N), "ns/order")
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 2. BenchmarkOrderMatch — Match latency for crossing orders
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func BenchmarkOrderMatch(b *testing.B) {
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
ob := dex.NewOrderBook("BENCH-MATCH")
|
|
|
|
// Insert one buy and one sell that cross
|
|
ob.AddOrder(&dex.Order{
|
|
Symbol: "BENCH-MATCH",
|
|
Type: dex.Limit,
|
|
Side: dex.Buy,
|
|
Price: 100.0,
|
|
Size: 1.0,
|
|
UserID: "buyer",
|
|
})
|
|
ob.AddOrder(&dex.Order{
|
|
Symbol: "BENCH-MATCH",
|
|
Type: dex.Limit,
|
|
Side: dex.Sell,
|
|
Price: 100.0,
|
|
Size: 1.0,
|
|
UserID: "seller",
|
|
})
|
|
|
|
ob.MatchOrders()
|
|
}
|
|
|
|
b.ReportMetric(float64(b.N)/b.Elapsed().Seconds(), "matches/sec")
|
|
b.ReportMetric(float64(b.Elapsed().Nanoseconds())/float64(b.N), "ns/match")
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 3. BenchmarkCancelOrder — Cancel latency
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func BenchmarkCancelOrder(b *testing.B) {
|
|
// Pre-fill the book so cancels have work to do
|
|
ob := dex.NewOrderBook("BENCH-CANCEL")
|
|
ids := make([]uint64, b.N)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
order := &dex.Order{
|
|
Symbol: "BENCH-CANCEL",
|
|
Type: dex.Limit,
|
|
Side: dex.Side(i % 2),
|
|
Price: 100.0 + float64(i%200),
|
|
Size: 1.0,
|
|
UserID: "bench",
|
|
}
|
|
ids[i] = ob.AddOrder(order)
|
|
}
|
|
|
|
b.ResetTimer()
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
ob.CancelOrder(ids[i])
|
|
}
|
|
|
|
b.ReportMetric(float64(b.N)/b.Elapsed().Seconds(), "cancels/sec")
|
|
b.ReportMetric(float64(b.Elapsed().Nanoseconds())/float64(b.N), "ns/cancel")
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 4. BenchmarkBatchOrders — 1000 orders in batch
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func BenchmarkBatchOrders(b *testing.B) {
|
|
const batchSize = 1000
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
ob := dex.NewOrderBook("BENCH-BATCH")
|
|
|
|
// Submit a batch of 1000 orders (500 buy, 500 sell)
|
|
for j := 0; j < batchSize; j++ {
|
|
order := &dex.Order{
|
|
Symbol: "BENCH-BATCH",
|
|
Type: dex.Limit,
|
|
Side: dex.Side(j % 2),
|
|
Price: 90.0 + float64(j%20),
|
|
Size: 1.0,
|
|
UserID: fmt.Sprintf("user-%d", j%10),
|
|
}
|
|
ob.AddOrder(order)
|
|
}
|
|
|
|
// Match all crossing orders
|
|
ob.MatchOrders()
|
|
}
|
|
|
|
totalOrders := float64(b.N) * batchSize
|
|
b.ReportMetric(totalOrders/b.Elapsed().Seconds(), "orders/sec")
|
|
b.ReportMetric(float64(b.Elapsed().Nanoseconds())/totalOrders, "ns/order")
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 5. BenchmarkConcurrentMatching — Parallel matching across 100 markets
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func BenchmarkConcurrentMatching(b *testing.B) {
|
|
const numMarkets = 100
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
books := make([]*dex.OrderBook, numMarkets)
|
|
for m := 0; m < numMarkets; m++ {
|
|
books[m] = dex.NewOrderBook(fmt.Sprintf("MKT-%d", m))
|
|
|
|
// Seed each book with crossing orders
|
|
for j := 0; j < 10; j++ {
|
|
books[m].AddOrder(&dex.Order{
|
|
Symbol: fmt.Sprintf("MKT-%d", m),
|
|
Type: dex.Limit,
|
|
Side: dex.Buy,
|
|
Price: 100.0,
|
|
Size: 1.0,
|
|
UserID: fmt.Sprintf("buyer-%d", j),
|
|
})
|
|
books[m].AddOrder(&dex.Order{
|
|
Symbol: fmt.Sprintf("MKT-%d", m),
|
|
Type: dex.Limit,
|
|
Side: dex.Sell,
|
|
Price: 100.0,
|
|
Size: 1.0,
|
|
UserID: fmt.Sprintf("seller-%d", j),
|
|
})
|
|
}
|
|
}
|
|
|
|
// Match all markets concurrently
|
|
var wg sync.WaitGroup
|
|
wg.Add(numMarkets)
|
|
for m := 0; m < numMarkets; m++ {
|
|
go func(book *dex.OrderBook) {
|
|
defer wg.Done()
|
|
book.MatchOrders()
|
|
}(books[m])
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
totalMatches := float64(b.N) * numMarkets
|
|
b.ReportMetric(totalMatches/b.Elapsed().Seconds(), "markets/sec")
|
|
b.ReportMetric(float64(b.Elapsed().Nanoseconds())/totalMatches, "ns/market")
|
|
}
|