Add Yyjson benchmarks

This commit is contained in:
John Keiser 2021-01-01 22:41:15 -08:00
parent 1dc4e9a84c
commit 3af54a9978
7 changed files with 183 additions and 8 deletions

View File

@ -47,10 +47,12 @@ if (TARGET benchmark::benchmark)
add_subdirectory(largerandom)
add_executable(bench_parse_call bench_parse_call.cpp)
add_executable(bench_dom_api bench_dom_api.cpp)
add_executable(bench_ondemand bench_ondemand.cpp)
if (TARGET yyjson)
target_link_libraries(bench_ondemand PRIVATE yyjson)
endif ()
if (SIMDJSON_EXCEPTIONS)
add_executable(bench_ondemand bench_ondemand.cpp)
if (TARGET yyjson)
target_link_libraries(bench_ondemand PRIVATE yyjson)
endif (TARGET yyjson)
endif (SIMDJSON_EXCEPTIONS)
endif()
include(checkperf.cmake)

View File

@ -30,6 +30,10 @@ SIMDJSON_POP_DISABLE_WARNINGS
// // yyjson
#ifdef SIMDJSON_COMPETITION_YYJSON
#include "partial_tweets/yyjson.h"
#include "largerandom/yyjson.h"
#include "kostya/yyjson.h"
#include "distinctuserid/yyjson.h"
#include "find_tweet/yyjson.h"
#endif

View File

@ -0,0 +1,49 @@
#pragma once
#include "distinctuserid.h"
namespace distinct_user_id {
class Yyjson {
public:
simdjson_really_inline const std::vector<int64_t> &Result() { return ids; }
simdjson_really_inline size_t ItemCount() { return ids.size(); }
private:
std::vector<int64_t> ids{};
public:
simdjson_really_inline bool Run(const padded_string &json) {
ids.clear();
// Walk the document, parsing the tweets as we go
yyjson_doc *doc = yyjson_read(json.data(), json.size(), 0);
if (!doc) { return false; }
yyjson_val *root = yyjson_doc_get_root(doc);
yyjson_val *statuses = yyjson_obj_get(root, "statuses");
size_t tweet_idx, tweets_max;
yyjson_val *tweet;
yyjson_arr_foreach(statuses, tweet_idx, tweets_max, tweet) {
auto user = yyjson_obj_get(tweet, "user");
auto id = yyjson_obj_get(user, "id");
ids.push_back(yyjson_get_sint(id));
// Not all tweets have a "retweeted_status", but when they do
// we want to go and find the user within.
auto retweet = yyjson_obj_get(tweet, "retweeted_status");
if (retweet) {
user = yyjson_obj_get(retweet, "user");
id = yyjson_obj_get(user, "id");
ids.push_back(yyjson_get_sint(id));
}
}
remove_duplicates(ids);
return true;
}
};
BENCHMARK_TEMPLATE(DistinctUserID, Yyjson);
} // namespace partial_tweets

View File

@ -0,0 +1,41 @@
#pragma once
#include "find_tweet.h"
namespace find_tweet {
class Yyjson {
public:
simdjson_really_inline std::string_view Result() { return text; }
simdjson_really_inline size_t ItemCount() { return 1; }
private:
std::string_view text{};
public:
simdjson_really_inline bool Run(const padded_string &json) {
text = "";
// Walk the document, parsing the tweets as we go
yyjson_doc *doc = yyjson_read(json.data(), json.size(), 0);
if (!doc) { return false; }
yyjson_val *root = yyjson_doc_get_root(doc);
yyjson_val *statuses = yyjson_obj_get(root, "statuses");
size_t tweet_idx, tweets_max;
yyjson_val *tweet;
yyjson_arr_foreach(statuses, tweet_idx, tweets_max, tweet) {
auto id = yyjson_obj_get(tweet, "id");
if (yyjson_get_uint(id) == TWEET_ID) {
auto _text = yyjson_obj_get(tweet, "text");
text = yyjson_get_str(_text);
return true;
}
}
return false;
}
};
BENCHMARK_TEMPLATE(FindTweet, Yyjson);
} // namespace find_tweet

42
benchmark/kostya/yyjson.h Normal file
View File

@ -0,0 +1,42 @@
#pragma once
#include "kostya.h"
namespace kostya {
class Yyjson {
public:
simdjson_really_inline const std::vector<my_point> &Result() { return container; }
simdjson_really_inline size_t ItemCount() { return container.size(); }
private:
ondemand::parser parser{};
std::vector<my_point> container{};
simdjson_really_inline double get_double(yyjson_val *obj, std::string_view key) {
yyjson_val *val = yyjson_obj_getn(obj, key.data(), key.length());
return yyjson_get_real(val);
}
public:
simdjson_really_inline bool Run(const padded_string &json) {
container.clear();
// Walk the document, parsing the tweets as we go
yyjson_doc *doc = yyjson_read(json.data(), json.size(), 0);
if (!doc) { return false; }
yyjson_val *root = yyjson_doc_get_root(doc);
yyjson_val *coords = yyjson_obj_get(root, "coordinates");
size_t idx, max;
yyjson_val *coord;
yyjson_arr_foreach(coords, idx, max, coord) {
container.emplace_back(my_point{get_double(coord, "x"), get_double(coord, "y"), get_double(coord, "z")});
}
return true;
}
};
BENCHMARK_TEMPLATE(Kostya, Yyjson);
} // namespace kostya

View File

@ -1,7 +1,5 @@
#pragma once
#if SIMDJSON_EXCEPTIONS
//
// Interface
//
@ -72,5 +70,3 @@ template<typename T> static void LargeRandom(benchmark::State &state) {
}
} // namespace largerandom
#endif // SIMDJSON_EXCEPTIONS

View File

@ -0,0 +1,41 @@
#pragma once
#include "largerandom.h"
namespace largerandom {
class Yyjson {
public:
simdjson_really_inline const std::vector<my_point> &Result() { return container; }
simdjson_really_inline size_t ItemCount() { return container.size(); }
private:
ondemand::parser parser{};
std::vector<my_point> container{};
simdjson_really_inline double get_double(yyjson_val *obj, std::string_view key) {
yyjson_val *val = yyjson_obj_getn(obj, key.data(), key.length());
return yyjson_get_real(val);
}
public:
simdjson_really_inline bool Run(const padded_string &json) {
container.clear();
// Walk the document, parsing the tweets as we go
yyjson_doc *doc = yyjson_read(json.data(), json.size(), 0);
if (!doc) { return false; }
yyjson_val *coords = yyjson_doc_get_root(doc);
size_t idx, max;
yyjson_val *coord;
yyjson_arr_foreach(coords, idx, max, coord) {
container.emplace_back(my_point{get_double(coord, "x"), get_double(coord, "y"), get_double(coord, "z")});
}
return true;
}
};
BENCHMARK_TEMPLATE(LargeRandom, Yyjson);
} // namespace kostya