Is this benchmark for parsing json realistic? C++

  benchmarking, c++, json

I am working on a project where the latency is extremally critical.
Suppose I getting market data order book as json and want to parse and store internally.
I am using RapidJson library and want to know whether this benchmark is realistic or I messed up somewhere.
I use GoogleBenchark for benchmarking.

For the sake of simplicity I will skip the connectivity part and hardcode the json string.

struct OrderBookLevel
{
    long double askPrice;
    long double bidPrice;
    long double askQty;
    long double bidQty;
};

struct OrderBook
{
    std::vector<OrderBookLevel> levels;
};

void BM_parse_OB(benchmark::State& state)
{
    using namespace rapidjson;
    using std::stod;
    std::unordered_map<std::string, double> h;
    std::string body = "{"e":"depthUpdate", "E" : 163788...";  hardcoded Json string, lenght is 430
    const int orderBookLevels = 20;
    for (auto _ : state)
    {
        OrderBook ob;
        ob.levels.reserve(20);
        Document doc;
        doc.Parse(body.c_str());
        for (int i = 0; i < orderBookLevels; ++i) {
            auto askPrice = stod(doc["a"][i][0].GetString());
            auto bidPrice = stod(doc["b"][i][0].GetString());
            auto askQty = stod(doc["a"][i][1].GetString());
            auto bidQty = stod(doc["b"][i][1].GetString());

            OrderBookLevel lvl{ askPrice,bidPrice,askQty,bidQty };
            ob.levels.push_back(lvl);
        }
    }
    state.SetItemsProcessed(state.iterations());
}

BENCHMARK(BM_parse_OB);

This is the example of the Json I am getting (hardcoded in string)

  {
    "e": "depthUpdate",
    "E": 1637427107268,
    "T": 1637427107263,
    "s": "BTCUSDT",
    "U": 953731753239,
    "u": 953731755882,
    "pu": 953731752807,
    "b": [
      [ "56909.95", "12.069" ],
      [ "57000.29", "0.000" ],
      [ "57250.96", "0.000" ],
      [ "57445.17", "4.069" ],
      [ "57513.88", "0.000" ],
      [ "57551.85", "0.000" ],
      [ "57558.56", "0.623" ],
      [ "57720.14", "0.198" ],
      [ "57723.65", "0.003" ],
      [ "57728.19", "0.139" ],
      [ "57750.02", "0.000" ],
      [ "57751.00", "0.206" ],
      [ "57752.54", "0.000" ],
      [ "57753.70", "0.000" ],
      [ "57753.71", "0.000" ],
      [ "57753.85", "0.000" ]
    ],
    "a": [
      [ "57752.19", "0.000" ],
      [ "57752.54", "0.090" ],
      [ "57757.12", "0.105" ],
      [ "57761.18", "0.267" ],
      [ "57767.50", "0.550" ],
      [ "57773.42", "0.042" ],
      [ "57776.69", "0.070" ],
      [ "57781.41", "0.003" ],
      [ "57783.32", "0.000" ],
      [ "57795.38", "0.000" ],
      [ "57796.33", "0.000" ],
      [ "57797.74", "0.433" ],
      [ "57842.46", "0.000" ],
      [ "57846.96", "0.785" ],
      [ "57941.63", "0.000" ],
      [ "57948.39", "0.623" ],
      [ "58250.97", "0.001" ],
      [ "59268.99", "0.000" ]
    ]
  }

And all this magic:

  1. Parsing Json string to some structure
  2. Storing Bid/Ask prices as well as their quantities in the vector

Takes around 7-8 microseconds.

7-8 microseconds.

So the question is rather subjective and probably targeted to the audience who have experience with similar thigs.

Is this benchmark realistic or I am doing something wrong?

Source: Windows Questions C++

LEAVE A COMMENT