JsonParserByLeo/Test/Ben/BenJsonLib.mq5
179 lines
6.1 KiB
MQL5
Nique_372
2026-06-28 18:47:50 -05:00
//+------------------------------------------------------------------+
//| BenJsonLib.mq5 |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com/"
#property version "1.00"
#property strict
Nique_372
2026-10-03 17:00:34 -05:00
//#define JSONBENCH_RUN_PARSE
//#define JSONBENCH_RUN_MIX
Nique_372
2026-08-28 09:42:51 -05:00
Nique_372
2026-06-28 18:47:50 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include <JsonLib\\JsonLib.mqh>
Nique_372
2026-08-28 08:28:32 -05:00
#include "Def.mqh"
Nique_372
2026-06-28 18:47:50 -05:00
Nique_372
2026-08-28 09:42:51 -05:00
#define LIBNAME "MQL5-JsonLib"
Nique_372
2026-06-28 18:47:50 -05:00
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
Nique_372
2026-08-28 09:42:51 -05:00
#ifdef JSONBENCH_RUN_PARSE
Nique_372
2026-06-28 18:47:50 -05:00
MQL5_Json::JsonError error;
MQL5_Json::JsonParseOptions opts;
opts.engine = MQL5_Json::ENGINE_HIGH_SPEED;
//---
MQL5_Json::Internal::CJsonRapidParser rapid_parser;
rapid_parser.m_text = g_json_data; // one copy
rapid_parser.m_text_len = StringLen(g_json_data);
//---
Sleep(1000);
//---
Nique_372
2026-08-28 09:42:51 -05:00
Nique_372
2026-06-28 18:47:50 -05:00
ulong a = GetMicrosecondCount();
for(int i = 0; i < 1000; i++)
{
rapid_parser.ParseToTapeFast(opts, error);
Nique_372
2026-08-28 09:42:51 -05:00
MQL5_Json::Internal::CJsonDocument doc_impl; // o como corresponda instanciar
MQL5_Json::Internal::CDomBuilder dom_builder;
dom_builder.BuildFromTape(GetPointer(doc_impl), rapid_parser.m_tape, g_json_data);
Nique_372
2026-06-28 18:47:50 -05:00
}
ulong b = GetMicrosecondCount() - a;
Nique_372
2026-08-28 09:42:51 -05:00
Print("@", b);
#else // JSONBENCH_RUN_PARSE
MQL5_Json::JsonError error;
MQL5_Json::JsonParseOptions opts;
MQL5_Json::JsonDocument doc = MQL5_Json::JsonParse(g_json_test_access, error, opts);
MQL5_Json::JsonNode root = doc.GetRoot();
Nique_372
2026-10-03 17:00:34 -05:00
#ifdef JSONBENCH_RUN_MIX
Nique_372
2026-08-28 09:42:51 -05:00
string keys[];
root.Get("wide_object").GetKeys(keys);
const int n = ArraySize(keys);
Nique_372
2026-09-17 21:24:16 -05:00
TSN::CRandom<Xoshiro256> xo;
Nique_372
2026-08-28 09:42:51 -05:00
xo.Seed(JSONBENCH_ACCESO_XHOR_SEED);
double sump = 0.0, profit = 0.0;
long tin = 0;
ulong a = GetMicrosecondCount();
for(int i = 0; i < JSONBENCH_TOTAL_ITER_ACCESO; i++)
{
const string key = keys[xo.RandomInteger(0, n)];
root.Get("wide_object")[key];
}
ulong b = GetMicrosecondCount();
Sleep(100);
JSONBENCH_ACCES_CALL(JSONBENCH_TAGOP_ACCESO_WIDE)
a = GetMicrosecondCount();
for(int i = 0; i < JSONBENCH_TOTAL_ITER_ACCESO; i++)
{
root.Get("deep_object").Get("l1").Get("l2").Get("l3").Get("l4").Get("l5")
.Get("l6").Get("l7").Get("l8").Get("l9").Get("l10").Get("target").AsString();
}
b = GetMicrosecondCount();
Sleep(100);
JSONBENCH_ACCES_CALL(JSONBENCH_TAGOP_ACCESO_DEEP)
a = GetMicrosecondCount();
for(int i = 0; i < JSONBENCH_TOTAL_ITER_ACCESO; i++)
{
const int index = xo.RandomInteger(0, 10000);
tin += root.Get("large_array").At(index).AsInt(0);
}
b = GetMicrosecondCount();
Sleep(100);
JSONBENCH_ACCES_CALL(JSONBENCH_TAGOP_ACCESO_LARGE_ARR)
a = GetMicrosecondCount();
for(int k = 0; k < 500; k++)
{
sump += root.Get("array_of_objects").At(k).Get("price").AsDouble(0.0);
profit += root.Get("array_of_objects").At(k).Get("profit").AsDouble(0.0);
Nique_372
2026-10-03 17:00:34 -05:00
const int s = root.Get("array_of_objects").At(k).Get("tags").Size();
tin += StringLen(root.Get("array_of_objects").At(k).Get("tags").At(k % s).AsString());
Nique_372
2026-08-28 09:42:51 -05:00
}
b = GetMicrosecondCount();
Sleep(100);
JSONBENCH_ACCES_CALL(JSONBENCH_TAGOP_ACCESO_MIX)
PrintFormat("sump=%.2f profit=%.2f tin=%d ", sump, profit, tin);
Nique_372
2026-10-03 17:00:34 -05:00
Nique_372
2026-08-28 09:42:51 -05:00
Nique_372
2026-10-03 17:00:34 -05:00
#else // JSONBENCH_RUN_MIX
MQL5_Json::Internal::CJsonSerializer s;
MQL5_Json::Internal::CJsonValue* _root = doc._GetImpl().m_root;
ulong a = GetMicrosecondCount();
for(int i = 0; i < 5; i++)
{
s.Serialize__Mod(_root,false,false);
}
ulong b = GetMicrosecondCount();
Sleep(100);
b*=200; // para que llegue a 100 iteraciones
// lo puse asi dado qeu es bien lento
JSONBENCH_SER
#endif // JSONBENCH_RUN_MIX
JSONBENCH_FINISH
Nique_372
2026-08-28 09:42:51 -05:00
#endif // JSONBENCH_RUN_PARSE
Nique_372
2026-06-28 18:47:50 -05:00
}
//+------------------------------------------------------------------+
Nique_372
2026-10-03 17:00:34 -05:00
/* hice algunas modicaciones para uqe no retorne el string
void CJsonSerializer::Serialize__Mod(const CJsonValue *root, bool p, bool e)
{
// 每次调用都重置内部状态,确保线程安全和结果纯净
m_result_str = "";
m_pretty = p;
m_escape_non_ascii = e;
SerializeValue(root, 0); // 从根节点开始递归
// return m_result_str;
}
*/
Nique_372
2026-06-28 18:47:50 -05:00
/*
Nique_372
2026-10-03 17:00:34 -05:00
2026.10.03 15:16:12.200 BenJsonLib (EURUSD,H1) [acceso-wide] Time: 236
2026.10.03 15:16:12.312 BenJsonLib (EURUSD,H1) [acceso-deep] Time: 1016
2026.10.03 15:16:12.422 BenJsonLib (EURUSD,H1) [acceso-array-large] Time: 415
2026.10.03 15:16:12.534 BenJsonLib (EURUSD,H1) [acceso-mixed-trading] Time: 1125
2026.10.03 15:16:12.534 BenJsonLib (EURUSD,H1) sump=537.48 profit=-337.50 tin=4927909
2026.10.03 15:16:13.505 BenJsonLib (EURUSD,H1) [acceso-wide] Time: 240
2026.10.03 15:16:13.617 BenJsonLib (EURUSD,H1) [acceso-deep] Time: 1023
2026.10.03 15:16:13.727 BenJsonLib (EURUSD,H1) [acceso-array-large] Time: 548
2026.10.03 15:16:13.837 BenJsonLib (EURUSD,H1) [acceso-mixed-trading] Time: 1085
2026.10.03 15:16:13.837 BenJsonLib (EURUSD,H1) sump=537.48 profit=-337.50 tin=4927909
2026.08.28 09:34:19.945 BenJsonLib (EURUSD,H1) @2160719
2026.08.28 09:34:29.912 BenJsonLib (EURUSD,H1) @2118729
2026.08.28 09:36:20.789 BenJsonLib (EURUSD,H1) @12879
2026.08.28 09:37:24.614 BenJsonLib (EURUSD,H1) @17595195
2026.08.28 09:39:02.251 UtilHeader (EURUSD,H1) [MultiBen& - $[parse|twitter.json|MQL5-JsonLib|1000]
2026.08.28 09:39:28.038 BenJsonLib (EURUSD,H1) @17547384
2026.08.28 09:39:51.584 BenJsonLib (EURUSD,H1) @17760505
2026.08.28 09:40:04.740 UtilHeader (EURUSD,H1) [MultiBen& - %[MQL5-JsonLib]
2026.10.03 16:51:13.942 BenJsonLib (EURUSD,H1) [Serilization] Time: 4534365593
2026.10.03 16:51:47.443 BenJsonLib (EURUSD,H1) [Serilization] Time: 4503261352
*/
//+------------------------------------------------------------------+