fast_json/Tests/TestJsonVsRival.mq5

115 行
3.9 KiB
MQL5

//+------------------------------------------------------------------+
//| TestJsonVsRival.mq5 |
//| AI-Toolkit |
//+------------------------------------------------------------------+
#property script_show_inputs
#include <Json\JsonLib.mqh> // The Challenger
#include <fast_json.mqh> // Our Library
input int Loops = 20000; // Adjusted loops for fairness (JsonLib is slow)
void OnStart() {
Print("=== JSON DEATHMATCH: AI-Toolkit vs 'Professional' JsonLib ===");
Print("Loops: ", Loops);
// Complex Payload (Same as previous benchmark)
string json =
"{"
"\"id\": 12345,"
"\"config\": {\"active\": true, \"mode\": \"swar\", \"threshold\": "
"0.0005},"
"\"rates\": [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0],"
"\"users\": [{\"id\":1, \"name\":\"A\"}, {\"id\":2, \"name\":\"B\"}],"
"\"desc\": \"Short string\","
"\"long\": \"This is a longer string to test the memory copy speed of "
"the parser when handling larger payloads.\""
"}";
ulong t0, t_fast = 0, t_rival = 0;
//---------------------------------------------------------
// 1. AI-TOOLKIT (FAST_JSON)
//---------------------------------------------------------
Print("\n--- 1. (fast_json) ---");
CJson toolkit;
long dummy_int = 0;
// A. PARSE
t0 = GetMicrosecondCount();
for (int i = 0; i < Loops; i++) {
toolkit.Parse(json);
}
ulong t_fast_parse = GetMicrosecondCount() - t0;
Print("Parse Time: ", t_fast_parse, " us");
// B. ACCESS (Read 'config.mode' and 'id')
t0 = GetMicrosecondCount();
for (int i = 0; i < Loops; i++) {
dummy_int += toolkit["config"]["mode"].Equals("swar");
dummy_int += toolkit["id"].ToInt();
}
ulong t_fast_access = GetMicrosecondCount() - t0;
Print("Access Time: ", t_fast_access, " us");
t_fast = t_fast_parse + t_fast_access;
Print(">> TOTAL (Fast): ", t_fast, " us");
//---------------------------------------------------------
// 2. RIVAL (JsonLib)
//---------------------------------------------------------
Print("\n--- 2. (JsonLib) ---");
JsonError error;
JsonParseOptions options; // Required by their API
JsonDocument doc;
long dummy_rival = 0;
// A. PARSE
// Their API: JsonParse(text, error, options) returns a JsonDocument by value
t0 = GetMicrosecondCount();
for (int i = 0; i < Loops; i++) {
doc = JsonParse(json, error, options);
}
ulong t_rival_parse = GetMicrosecondCount() - t0;
Print("Parse Time: ", t_rival_parse, " us");
// B. ACCESS
// We need a valid doc for this test
doc = JsonParse(json, error, options);
JsonNode root = doc.GetRoot();
t0 = GetMicrosecondCount();
for (int i = 0; i < Loops; i++) {
// Their API: root["key"]["key"].AsString()
JsonNode config = root["config"];
JsonNode mode = config["mode"];
string s = mode.AsString();
dummy_rival += (s == "swar");
// Direct access attempt
dummy_rival += root["id"].AsInt();
}
ulong t_rival_access = GetMicrosecondCount() - t0;
Print("Access Time: ", t_rival_access, " us");
t_rival = t_rival_parse + t_rival_access;
Print(">> TOTAL (Rival): ", t_rival, " us");
//---------------------------------------------------------
// RESULTS
//---------------------------------------------------------
Print("\n==================================================");
Print("FINAL SCOREBOARD");
Print("==================================================");
double speedup_parse = (double)t_rival_parse / (double)t_fast_parse;
double speedup_total = (double)t_rival / (double)t_fast;
Print(StringFormat("Parse Speedup: %.2fx FASTER", speedup_parse));
Print(StringFormat("Total Speedup: %.2fx FASTER", speedup_total));
if (t_fast < t_rival)
Print("\nWINNER: AI-Toolkit (Flawless Victory)");
else
Print("\nWINNER: Rival (Check your math, Arnaldo)");
Print("==================================================");
}