fast_json/Tests/TestBinanceBenchmark.mq5

261 lines
12 KiB
MQL5
Raw Permalink Normal View History

//+------------------------------------------------------------------+
//| TestBinanceBenchmark.mq5 |
//| AI-Toolkit |
//| |
//| Benchmark: fast_json vs JAson on REAL Binance API Responses |
//| Downloads live JSON via WebRequest, then benchmarks parse/serial |
//| |
//| IMPORTANT: Add https://api.binance.com to |
//| Tools > Options > Expert Advisors > Allow WebRequest for URLs |
//+------------------------------------------------------------------+
#property script_show_inputs
2026-02-19 22:19:54 -03:00
#include "../fast_json.mqh"
#include <JAson.mqh>
input int InpLoops = 10000; // Benchmark Loops per payload
input string InpSymbol = "BTCUSDT"; // Symbol
//+------------------------------------------------------------------+
//| Download JSON from URL via WebRequest |
//+------------------------------------------------------------------+
string HttpGet(string url) {
char post[];
char result[];
string headers;
string cookie = "";
string referer = "";
int timeout = 5000;
ResetLastError();
int res = WebRequest("GET", url, cookie, referer, timeout, post, 0, result,
headers);
if (res == -1) {
int err = GetLastError();
PrintFormat("WebRequest FAILED: %s (Error %d)", url, err);
if (err == 4014)
Print(">>> Add https://api.binance.com to Tools > Options > Expert "
"Advisors > Allow WebRequest");
return "";
}
if (res != 200) {
PrintFormat("HTTP %d: %s", res, url);
return "";
}
return CharArrayToString(result, 0, WHOLE_ARRAY, CP_UTF8);
}
//+------------------------------------------------------------------+
//| Benchmark a single payload: fast_json vs JAson |
//+------------------------------------------------------------------+
void BenchPayload(string label, string json, int loops) {
if (json == "") {
PrintFormat(" [%s] SKIPPED — empty payload", label);
return;
}
int bytes = StringLen(json);
PrintFormat(" [%s] %d bytes", label, bytes);
// --- fast_json ---
CJson fj;
ulong t0, fj_parse, fj_ser;
string dummy = "";
// Warmup
fj.Parse(json);
// Parse
t0 = GetMicrosecondCount();
for (int i = 0; i < loops; i++)
fj.Parse(json);
fj_parse = GetMicrosecondCount() - t0;
// Serialize
t0 = GetMicrosecondCount();
for (int i = 0; i < loops; i++)
dummy = fj.Serialize();
fj_ser = GetMicrosecondCount() - t0;
// --- JAson ---
2026-07-20 01:00:36 +03:30
CJAson ja;
ulong ja_parse, ja_ser;
// Warmup
ja.Deserialize(json);
// Parse
t0 = GetMicrosecondCount();
for (int i = 0; i < loops; i++)
ja.Deserialize(json);
ja_parse = GetMicrosecondCount() - t0;
// Serialize
t0 = GetMicrosecondCount();
for (int i = 0; i < loops; i++)
dummy = ja.Serialize();
ja_ser = GetMicrosecondCount() - t0;
// --- Results ---
double parse_speedup = (ja_parse > 0) ? (double)ja_parse / fj_parse : 0;
double ser_speedup = (ja_ser > 0) ? (double)ja_ser / fj_ser : 0;
double total_speedup = (double)(ja_parse + ja_ser) / (fj_parse + fj_ser);
PrintFormat(" fast_json | Parse: %6d us (%5.1f us/op) | Serialize: %6d "
"us (%5.1f us/op)",
fj_parse, (double)fj_parse / loops, fj_ser,
(double)fj_ser / loops);
PrintFormat(" JAson | Parse: %6d us (%5.1f us/op) | Serialize: %6d "
"us (%5.1f us/op)",
ja_parse, (double)ja_parse / loops, ja_ser,
(double)ja_ser / loops);
PrintFormat(" Speedup | Parse: %.1fx | Serialize: %.1fx | Total: %.1fx",
parse_speedup, ser_speedup, total_speedup);
Print(" ────────────────────────────────────────────────");
}
//+------------------------------------------------------------------+
//| Script entry point |
//+------------------------------------------------------------------+
void OnStart() {
Print("══════════════════════════════════════════════════");
2026-07-20 04:20:01 +03:30
Print(" BINANCE LIVE BENCHMARK — fast_json v3.7.0 vs JAson");
Print(" Symbol: ", InpSymbol, " | Loops: ", InpLoops);
Print("══════════════════════════════════════════════════");
Print("");
string base = "https://api.binance.com/api/v3/";
//--- 1. Download all payloads first
Print("Downloading live data from Binance API...");
string klines =
HttpGet(base + "klines?symbol=" + InpSymbol + "&interval=1h&limit=100");
string depth = HttpGet(base + "depth?symbol=" + InpSymbol + "&limit=100");
string trades = HttpGet(base + "trades?symbol=" + InpSymbol + "&limit=100");
string ticker = HttpGet(base + "ticker/24hr?symbol=" + InpSymbol);
if (klines == "" && depth == "" && trades == "" && ticker == "") {
Print("ALL downloads failed. Check WebRequest permissions.");
Print(">>> Tools > Options > Expert Advisors > Allow WebRequest");
Print(">>> Add: https://api.binance.com");
return;
}
Print("Download complete. Starting benchmark...");
Print("");
//--- 2. Benchmark each payload type
// Klines: Array of arrays, heavy numeric (timestamps + OHLCV as strings)
// ~100 candles × 12 fields = 1200 values
Print("━━━ 1. KLINES (Array of Arrays — OHLCV) ━━━");
BenchPayload("Klines 100x1H", klines, InpLoops);
// Depth: Object with 2 arrays of arrays (bids/asks), pure numeric strings
// 100 bid levels + 100 ask levels × 2 fields = 400 values
Print("━━━ 2. ORDER BOOK DEPTH (Bids/Asks) ━━━");
BenchPayload("Depth L100", depth, InpLoops);
// Trades: Array of objects with mixed types (id, price, qty, bool, timestamp)
// 100 trades × 7 fields = 700 values
Print("━━━ 3. RECENT TRADES (Array of Objects) ━━━");
BenchPayload("Trades 100", trades, InpLoops);
// Ticker24hr: Single flat object with many numeric string fields
// ~20 fields, small but diverse
Print("━━━ 4. TICKER 24HR (Flat Object) ━━━");
BenchPayload("Ticker24hr", ticker, InpLoops);
//--- 3. Summary
Print("");
Print("══════════════════════════════════════════════════");
Print(" Benchmark complete. All data sourced LIVE from");
Print(" Binance REST API. No synthetic/hardcoded payloads.");
Print("══════════════════════════════════════════════════");
}
2026-07-20 04:20:01 +03:30
/*
BINANCE LIVE BENCHMARK fast_json v3.4.1 vs JAson
Symbol: BTCUSDT | Loops: 10000
Downloading live data from Binance API...
Download complete. Starting benchmark...
1. KLINES (Array of Arrays OHLCV)
[Klines 100x1H] 17788 bytes
fast_json | Parse: 647507 us ( 64.8 us/op) | Serialize: 607825 us ( 60.8 us/op)
JAson | Parse: 23768546 us (2376.9 us/op) | Serialize: 28581753 us (2858.2 us/op)
Speedup | Parse: 36.7x | Serialize: 47.0x | Total: 41.7x
2. ORDER BOOK DEPTH (Bids/Asks)
[Depth L100] 6446 bytes
fast_json | Parse: 298848 us ( 29.9 us/op) | Serialize: 234277 us ( 23.4 us/op)
JAson | Parse: 12638947 us (1263.9 us/op) | Serialize: 21846688 us (2184.7 us/op)
Speedup | Parse: 42.3x | Serialize: 93.3x | Total: 64.7x
3. RECENT TRADES (Array of Objects)
[Trades 100] 14734 bytes
fast_json | Parse: 1383672 us (138.4 us/op) | Serialize: 1648019 us (164.8 us/op)
JAson | Parse: 50189472 us (5018.9 us/op) | Serialize: 19787449 us (1978.7 us/op)
Speedup | Parse: 36.3x | Serialize: 12.0x | Total: 23.1x
4. TICKER 24HR (Flat Object)
[Ticker24hr] 556 bytes
fast_json | Parse: 21403 us ( 2.1 us/op) | Serialize: 22978 us ( 2.3 us/op)
JAson | Parse: 444254 us ( 44.4 us/op) | Serialize: 221482 us ( 22.1 us/op)
Speedup | Parse: 20.8x | Serialize: 9.6x | Total: 15.0x
Benchmark complete. All data sourced LIVE from
Binance REST API. No synthetic/hardcoded payloads.
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
BINANCE LIVE BENCHMARK fast_json v3.4.1 vs JAson
Symbol: BTCUSDT | Loops: 10000
Downloading live data from Binance API...
Download complete. Starting benchmark...
1. KLINES (Array of Arrays OHLCV)
[Klines 100x1H] 17790 bytes
fast_json | Parse: 661143 us ( 66.1 us/op) | Serialize: 572607 us ( 57.3 us/op)
JAson | Parse: 22307001 us (2230.7 us/op) | Serialize: 26518872 us (2651.9 us/op)
Speedup | Parse: 33.7x | Serialize: 46.3x | Total: 39.6x
2. ORDER BOOK DEPTH (Bids/Asks)
[Depth L100] 6446 bytes
fast_json | Parse: 260455 us ( 26.0 us/op) | Serialize: 192279 us ( 19.2 us/op)
JAson | Parse: 5424845 us (542.5 us/op) | Serialize: 6373021 us (637.3 us/op)
Speedup | Parse: 20.8x | Serialize: 33.1x | Total: 26.1x
3. RECENT TRADES (Array of Objects)
[Trades 100] 14629 bytes
fast_json | Parse: 604014 us ( 60.4 us/op) | Serialize: 543447 us ( 54.3 us/op)
JAson | Parse: 16181770 us (1618.2 us/op) | Serialize: 18988997 us (1898.9 us/op)
Speedup | Parse: 26.8x | Serialize: 34.9x | Total: 30.7x
4. TICKER 24HR (Flat Object)
[Ticker24hr] 556 bytes
fast_json | Parse: 18924 us ( 1.9 us/op) | Serialize: 21739 us ( 2.2 us/op)
JAson | Parse: 565130 us ( 56.5 us/op) | Serialize: 212420 us ( 21.2 us/op)
Speedup | Parse: 29.9x | Serialize: 9.8x | Total: 19.1x
Benchmark complete. All data sourced LIVE from
Binance REST API. No synthetic/hardcoded payloads.
*/