Skip to content

Commit

Permalink
add checks for serialisation/deserialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
csegarragonz committed Oct 29, 2021
1 parent d6c3b1a commit 11ec3fc
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 7 deletions.
97 changes: 90 additions & 7 deletions src/util/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <cppcodec/base64_rfc4648.hpp>

#include <sstream>

using namespace rapidjson;

namespace faabric::util {
Expand Down Expand Up @@ -187,24 +189,35 @@ std::string messageToJson(const faabric::Message& msg)
d.AddMember("record_exec_graph", msg.recordexecgraph(), a);

if (msg.execgraphdetails_size() > 0) {
std::string out = "";
std::stringstream ss;
const auto& map = msg.execgraphdetails();
for (const auto& it : map) {
out = fmt::format("{},{}:{}", out, it.first, it.second);
auto it = map.begin();
while (it != map.end()) {
ss << fmt::format("{}:{}", it->first, it->second);
++it;
if (it != map.end()) {
ss << ",";
}
}

std::string out = ss.str();
d.AddMember(
"exec_graph_detail", Value(out.c_str(), out.size()).Move(), a);
}

if (msg.intexecgraphdetails_size() > 0) {
std::string out = "";
std::stringstream ss;
const auto& map = msg.intexecgraphdetails();
for (const auto& it : map) {
out = fmt::format(
"{},{}:{}", out, it.first, std::to_string(it.second));
auto it = map.begin();
while (it != map.end()) {
ss << fmt::format("{}:{}", it->first, it->second);
++it;
if (it != map.end()) {
ss << ",";
}
}

std::string out = ss.str();
d.AddMember("int_exec_graph_detail",
Value(out.c_str(), out.size()).Move(),
a);
Expand Down Expand Up @@ -294,6 +307,54 @@ std::string getStringFromJson(Document& doc,
return std::string(valuePtr, valuePtr + it->value.GetStringLength());
}

std::map<std::string, std::string> getStringStringMapFromJson(
Document& doc,
const std::string& key)
{
std::map<std::string, std::string> map;

Value::MemberIterator it = doc.FindMember(key.c_str());
if (it == doc.MemberEnd()) {
return map;
}

const char* valuePtr = it->value.GetString();
std::stringstream ss(
std::string(valuePtr, valuePtr + it->value.GetStringLength()));
std::string keyVal;
while (std::getline(ss, keyVal, ',')) {
auto pos = keyVal.find(":");
std::string key = keyVal.substr(0, pos);
map[key] = keyVal.erase(0, pos + sizeof(char));
}

return map;
}

std::map<std::string, int> getStringIntMapFromJson(Document& doc,
const std::string& key)
{
std::map<std::string, int> map;

Value::MemberIterator it = doc.FindMember(key.c_str());
if (it == doc.MemberEnd()) {
return map;
}

const char* valuePtr = it->value.GetString();
std::stringstream ss(
std::string(valuePtr, valuePtr + it->value.GetStringLength()));
std::string keyVal;
while (std::getline(ss, keyVal, ',')) {
auto pos = keyVal.find(":");
std::string key = keyVal.substr(0, pos);
int val = std::stoi(keyVal.erase(0, pos + sizeof(char)));
map[key] = val;
}

return map;
}

faabric::Message jsonToMessage(const std::string& jsonIn)
{
PROF_START(jsonDecode)
Expand Down Expand Up @@ -352,6 +413,28 @@ faabric::Message jsonToMessage(const std::string& jsonIn)
msg.set_sgxpolicy(getStringFromJson(d, "sgxpolicy", ""));
msg.set_sgxresult(getStringFromJson(d, "sgxresult", ""));

msg.set_recordexecgraph(getBoolFromJson(d, "record_exec_graph", false));

// By default, clear the map
msg.clear_execgraphdetails();
// Fill keypairs if found
auto& msgStrMap = *msg.mutable_execgraphdetails();
std::map<std::string, std::string> strMap =
getStringStringMapFromJson(d, "exec_graph_detail");
for (auto& it : strMap) {
msgStrMap[it.first] = it.second;
}

// By default, clear the map
msg.clear_intexecgraphdetails();
// Fill keypairs if found
auto& msgIntMap = *msg.mutable_intexecgraphdetails();
std::map<std::string, int> intMap =
getStringIntMapFromJson(d, "int_exec_graph_detail");
for (auto& it : intMap) {
msgIntMap[it.first] = it.second;
}

PROF_END(jsonDecode)

return msg;
Expand Down
1 change: 1 addition & 0 deletions tests/test/util/test_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ TEST_CASE("Test message to JSON round trip", "[util]")
REQUIRE(msg.timestamp() > 0);

std::string jsonString = faabric::util::messageToJson(msg);
SPDLOG_INFO("{}", jsonString);

faabric::Message actual = faabric::util::jsonToMessage(jsonString);

Expand Down
16 changes: 16 additions & 0 deletions tests/utils/faabric_utils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <catch2/catch.hpp>

#include "fixtures.h"

#include <faabric/scheduler/ExecGraph.h>
Expand Down Expand Up @@ -63,6 +65,20 @@ using namespace faabric;
namespace tests {
void cleanFaabric();

template<class T>
void checkMessageMapEquality(T mapA, T mapB)
{
REQUIRE(mapA.size() == mapB.size());
auto itA = mapA.begin();
auto itB = mapB.begin();
while (itA != mapA.end() && itB != mapB.end()) {
REQUIRE(itA->first == itB->first);
REQUIRE(itA->second == itB->second);
itA++;
itB++;
}
}

void checkMessageEquality(const faabric::Message& msgA,
const faabric::Message& msgB);

Expand Down
5 changes: 5 additions & 0 deletions tests/utils/message_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,10 @@ void checkMessageEquality(const faabric::Message& msgA,
REQUIRE(msgA.sgxtag() == msgB.sgxtag());
REQUIRE(msgA.sgxpolicy() == msgB.sgxpolicy());
REQUIRE(msgA.sgxresult() == msgB.sgxresult());

REQUIRE(msgA.recordexecgraph() == msgB.recordexecgraph());
checkMessageMapEquality(msgA.execgraphdetails(), msgB.execgraphdetails());
checkMessageMapEquality(msgA.intexecgraphdetails(),
msgB.intexecgraphdetails());
}
}

0 comments on commit 11ec3fc

Please sign in to comment.