|
| 1 | +// Temporary C2J vs schema-serde benchmark for DynamoDB (not part of the repo). |
| 2 | +// Build twice: against the C2J install, and against the schema-serde install with -DSCHEMA_SERDE. |
| 3 | +#include <aws/core/Aws.h> |
| 4 | +#include <aws/core/AmazonWebServiceResult.h> |
| 5 | +#include <aws/core/http/HttpClient.h> |
| 6 | +#include <aws/core/http/HttpClientFactory.h> |
| 7 | +#include <aws/core/http/standard/StandardHttpRequest.h> |
| 8 | +#include <aws/core/http/standard/StandardHttpResponse.h> |
| 9 | +#include <aws/core/utils/json/JsonSerializer.h> |
| 10 | +#include <aws/dynamodb/DynamoDBClient.h> |
| 11 | +#include <aws/dynamodb/model/GetItemRequest.h> |
| 12 | +#include <aws/dynamodb/model/GetItemResult.h> |
| 13 | +#include <aws/dynamodb/model/PutItemRequest.h> |
| 14 | +#include <aws/dynamodb/model/QueryRequest.h> |
| 15 | +#include <aws/dynamodb/model/QueryResult.h> |
| 16 | +#ifdef SCHEMA_SERDE |
| 17 | +#include <smithy/client/schema/ClientProtocol.h> |
| 18 | +#endif |
| 19 | + |
| 20 | +#include <sys/resource.h> |
| 21 | +#include <algorithm> |
| 22 | +#include <chrono> |
| 23 | +#include <cstdio> |
| 24 | +#include <fstream> |
| 25 | +#include <functional> |
| 26 | +#include <iostream> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +using namespace Aws::DynamoDB::Model; |
| 30 | +using AttrMap = Aws::Map<Aws::String, AttributeValue>; |
| 31 | + |
| 32 | +#ifdef SCHEMA_SERDE |
| 33 | +static const char* MODE = "schema-serde"; |
| 34 | +#else |
| 35 | +static const char* MODE = "c2j"; |
| 36 | +#endif |
| 37 | + |
| 38 | +// ---------------- payloads (identical for both builds) ---------------- |
| 39 | +static std::shared_ptr<AttributeValue> P(const AttributeValue& v) { return Aws::MakeShared<AttributeValue>("b", v); } |
| 40 | + |
| 41 | +static AttrMap ItemSmall() { |
| 42 | + AttrMap m; |
| 43 | + m["id"] = AttributeValue("user#0001"); |
| 44 | + m["name"] = AttributeValue("Ada Lovelace"); |
| 45 | + m["age"] = AttributeValue().SetN("36"); |
| 46 | + m["active"] = AttributeValue().SetBool(true); |
| 47 | + m["tags"] = AttributeValue(Aws::Vector<Aws::String>{"admin", "beta", "eu"}); |
| 48 | + return m; |
| 49 | +} |
| 50 | + |
| 51 | +static AttrMap ItemMedium() { // 50 mixed attributes with nesting |
| 52 | + AttrMap m = ItemSmall(); |
| 53 | + for (int i = 0; i < 20; ++i) m["s" + std::to_string(i)] = AttributeValue("value-" + std::to_string(i * 7919)); |
| 54 | + for (int i = 0; i < 15; ++i) m["n" + std::to_string(i)] = AttributeValue().SetN(std::to_string(i * 3.25)); |
| 55 | + AttributeValue addr; |
| 56 | + for (int i = 0; i < 10; ++i) addr.AddMEntry("k" + std::to_string(i), P(AttributeValue("v" + std::to_string(i)))); |
| 57 | + m["address"] = addr; |
| 58 | + AttributeValue hist; |
| 59 | + for (int i = 0; i < 10; ++i) hist.AddLItem(P(AttributeValue().SetN(std::to_string(1000 + i)))); |
| 60 | + m["history"] = hist; |
| 61 | + for (int i = 0; i < 8; ++i) m["b" + std::to_string(i)] = AttributeValue().SetBool(i % 2 == 0); |
| 62 | + return m; |
| 63 | +} |
| 64 | + |
| 65 | +static AttrMap ItemShallowMapL() { // 1,000 string attributes (like the cross-SDK ShallowMap_L case) |
| 66 | + AttrMap m; |
| 67 | + m["id"] = AttributeValue("big#1"); |
| 68 | + for (int i = 0; i < 1000; ++i) m["attr" + std::to_string(i)] = AttributeValue("v" + std::to_string(i * 31)); |
| 69 | + return m; |
| 70 | +} |
| 71 | + |
| 72 | +static AttrMap ItemBinaryL() { // 10 x 10 KiB blobs |
| 73 | + AttrMap m; |
| 74 | + m["id"] = AttributeValue("bin#1"); |
| 75 | + Aws::Utils::ByteBuffer blob(10 * 1024); |
| 76 | + for (size_t i = 0; i < blob.GetLength(); ++i) blob[i] = static_cast<unsigned char>((i * 131) & 0xff); |
| 77 | + for (int i = 0; i < 10; ++i) m["blob" + std::to_string(i)] = AttributeValue().SetB(blob); |
| 78 | + return m; |
| 79 | +} |
| 80 | + |
| 81 | +// The wire JSON for an item, built with the legacy serializer once at startup (same bytes for both builds' |
| 82 | +// deserialize benchmarks once dumped and reloaded; see main()). |
| 83 | +static Aws::String ItemJson(const AttrMap& item) { |
| 84 | + Aws::Utils::Json::JsonValue obj; |
| 85 | + for (const auto& kv : item) obj.WithObject(kv.first, kv.second.Jsonize()); |
| 86 | + return obj.View().WriteCompact(); |
| 87 | +} |
| 88 | + |
| 89 | +// ---------------- timing ---------------- |
| 90 | +struct Stats { double meanNs, p50Ns, p99Ns; size_t n; }; |
| 91 | + |
| 92 | +static Stats Time(const std::function<void()>& fn, double seconds = 1.0, size_t maxIters = 200000) { |
| 93 | + for (int i = 0; i < 200; ++i) fn(); // warm-up |
| 94 | + std::vector<double> samples; |
| 95 | + samples.reserve(maxIters); |
| 96 | + const auto start = std::chrono::steady_clock::now(); |
| 97 | + while (samples.size() < maxIters && |
| 98 | + std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count() < seconds) { |
| 99 | + const auto t0 = std::chrono::steady_clock::now(); |
| 100 | + fn(); |
| 101 | + const auto t1 = std::chrono::steady_clock::now(); |
| 102 | + samples.push_back(std::chrono::duration<double, std::nano>(t1 - t0).count()); |
| 103 | + } |
| 104 | + std::sort(samples.begin(), samples.end()); |
| 105 | + double sum = 0; |
| 106 | + for (double s : samples) sum += s; |
| 107 | + return {sum / samples.size(), samples[samples.size() / 2], samples[std::min(samples.size() - 1, samples.size() * 99 / 100)], |
| 108 | + samples.size()}; |
| 109 | +} |
| 110 | + |
| 111 | +static void Report(const char* group, const Aws::String& name, const Stats& s) { |
| 112 | + std::printf("%s\t%s\t%s\t%.0f\t%.0f\t%.0f\t%zu\n", MODE, group, name.c_str(), s.meanNs, s.p50Ns, s.p99Ns, s.n); |
| 113 | + std::fflush(stdout); |
| 114 | +} |
| 115 | + |
| 116 | +static volatile size_t g_sink = 0; |
| 117 | + |
| 118 | +// ---------------- fake HTTP layer for end-to-end client calls ---------------- |
| 119 | +static Aws::String g_responseBody = "{}"; |
| 120 | + |
| 121 | +class CannedHttpClient : public Aws::Http::HttpClient { |
| 122 | + public: |
| 123 | + std::shared_ptr<Aws::Http::HttpResponse> MakeRequest(const std::shared_ptr<Aws::Http::HttpRequest>& request, |
| 124 | + Aws::Utils::RateLimits::RateLimiterInterface*, |
| 125 | + Aws::Utils::RateLimits::RateLimiterInterface*) const override { |
| 126 | + auto response = Aws::MakeShared<Aws::Http::Standard::StandardHttpResponse>("b", request); |
| 127 | + response->SetResponseCode(Aws::Http::HttpResponseCode::OK); |
| 128 | + response->AddHeader("x-amzn-requestid", "BENCHMARKREQUESTID"); |
| 129 | + response->AddHeader("content-type", "application/x-amz-json-1.0"); |
| 130 | + response->GetResponseBody() << g_responseBody; |
| 131 | + return response; |
| 132 | + } |
| 133 | +}; |
| 134 | + |
| 135 | +class CannedHttpClientFactory : public Aws::Http::HttpClientFactory { |
| 136 | + public: |
| 137 | + std::shared_ptr<Aws::Http::HttpClient> CreateHttpClient(const Aws::Client::ClientConfiguration&) const override { |
| 138 | + return Aws::MakeShared<CannedHttpClient>("b"); |
| 139 | + } |
| 140 | + std::shared_ptr<Aws::Http::HttpRequest> CreateHttpRequest(const Aws::String& uri, Aws::Http::HttpMethod method, |
| 141 | + const Aws::IOStreamFactory& streamFactory) const override { |
| 142 | + return CreateHttpRequest(Aws::Http::URI(uri), method, streamFactory); |
| 143 | + } |
| 144 | + std::shared_ptr<Aws::Http::HttpRequest> CreateHttpRequest(const Aws::Http::URI& uri, Aws::Http::HttpMethod method, |
| 145 | + const Aws::IOStreamFactory& streamFactory) const override { |
| 146 | + auto request = Aws::MakeShared<Aws::Http::Standard::StandardHttpRequest>("b", uri, method); |
| 147 | + request->SetResponseStreamFactory(streamFactory); |
| 148 | + return request; |
| 149 | + } |
| 150 | +}; |
| 151 | + |
| 152 | +static double CpuSeconds() { |
| 153 | + rusage u{}; |
| 154 | + getrusage(RUSAGE_SELF, &u); |
| 155 | + return u.ru_utime.tv_sec + u.ru_utime.tv_usec / 1e6 + u.ru_stime.tv_sec + u.ru_stime.tv_usec / 1e6; |
| 156 | +} |
| 157 | + |
| 158 | +int main(int argc, char** argv) { |
| 159 | + const Aws::String outDir = argc > 1 ? argv[1] : "/tmp/serde-bench-out"; |
| 160 | + Aws::SDKOptions options; |
| 161 | + options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Off; |
| 162 | + Aws::InitAPI(options); |
| 163 | + { |
| 164 | + struct Case { Aws::String name; AttrMap item; }; |
| 165 | + std::vector<Case> cases = {{"Item_S", ItemSmall()}, {"Item_M", ItemMedium()}, |
| 166 | + {"Item_ShallowMap_L", ItemShallowMapL()}, {"Item_Binary_L", ItemBinaryL()}}; |
| 167 | + const Aws::Http::HeaderValueCollection headers{{"x-amzn-requestid", "BENCHMARKREQUESTID"}, |
| 168 | + {"content-type", "application/x-amz-json-1.0"}}; |
| 169 | +#ifdef SCHEMA_SERDE |
| 170 | + smithy::schema::AwsJson10Protocol protocol; |
| 171 | +#endif |
| 172 | + const double cpu0 = CpuSeconds(); |
| 173 | + |
| 174 | + for (const auto& c : cases) { |
| 175 | + PutItemRequest put; |
| 176 | + put.SetTableName("bench-table"); |
| 177 | + put.SetItem(c.item); |
| 178 | + |
| 179 | + // Correctness sample: the request body each build sends, compared across builds afterwards. |
| 180 | +#ifdef SCHEMA_SERDE |
| 181 | + auto parts = protocol.SerializeHttpRequest(put.GetSchemaSerdeContext(), *put.GetSchemaSerdeInput()); |
| 182 | + const Aws::String sentBody = parts.IsSuccess() && parts.GetResult().body ? *parts.GetResult().body : "<error>"; |
| 183 | +#else |
| 184 | + const Aws::String sentBody = put.SerializePayload(); |
| 185 | +#endif |
| 186 | + std::ofstream(outDir + "/" + MODE + "-" + c.name + "-request.json") << sentBody; |
| 187 | + |
| 188 | + Report("serialize", "PutItem/" + c.name, Time([&] { |
| 189 | +#ifdef SCHEMA_SERDE |
| 190 | + auto input = put.GetSchemaSerdeInput(); |
| 191 | + auto out = protocol.SerializeHttpRequest(put.GetSchemaSerdeContext(), *input); |
| 192 | + g_sink += out.GetResult().body->size(); |
| 193 | +#else |
| 194 | + auto body = put.SerializePayload(); |
| 195 | + g_sink += body.size(); |
| 196 | +#endif |
| 197 | + auto h = put.GetHeaders(); |
| 198 | + g_sink += h.size(); |
| 199 | + })); |
| 200 | + |
| 201 | + // Deserialize: GetItem response {"Item": {...}} and Query response with 100 copies. |
| 202 | + const Aws::String getBody = "{\"Item\":" + ItemJson(c.item) + "}"; |
| 203 | + GetItemRequest getReq; |
| 204 | + getReq.SetTableName("bench-table"); |
| 205 | + Report("deserialize", "GetItem/" + c.name, Time([&] { |
| 206 | +#ifdef SCHEMA_SERDE |
| 207 | + Aws::AmazonWebServiceResult<Aws::String> wrap(getBody, headers, Aws::Http::HttpResponseCode::OK); |
| 208 | + GetItemResult r(wrap); |
| 209 | + protocol.DeserializeHttpResponse(getReq.GetSchemaSerdeContext(), 200, headers, |
| 210 | + Aws::Crt::ByteCursorFromArray(reinterpret_cast<const uint8_t*>(getBody.data()), getBody.size()), |
| 211 | + *r.GetSchemaSerdeOutput()); |
| 212 | +#else |
| 213 | + Aws::Utils::Json::JsonValue jv(getBody); |
| 214 | + Aws::AmazonWebServiceResult<Aws::Utils::Json::JsonValue> wrap(std::move(jv), headers, Aws::Http::HttpResponseCode::OK); |
| 215 | + GetItemResult r(wrap); |
| 216 | +#endif |
| 217 | + g_sink += r.GetItem().size(); |
| 218 | + })); |
| 219 | + { // correctness sample: attribute count after parsing |
| 220 | +#ifdef SCHEMA_SERDE |
| 221 | + Aws::AmazonWebServiceResult<Aws::String> wrap(getBody, headers, Aws::Http::HttpResponseCode::OK); |
| 222 | + GetItemResult r(wrap); |
| 223 | + protocol.DeserializeHttpResponse(getReq.GetSchemaSerdeContext(), 200, headers, |
| 224 | + Aws::Crt::ByteCursorFromArray(reinterpret_cast<const uint8_t*>(getBody.data()), getBody.size()), |
| 225 | + *r.GetSchemaSerdeOutput()); |
| 226 | +#else |
| 227 | + GetItemResult r(Aws::AmazonWebServiceResult<Aws::Utils::Json::JsonValue>(Aws::Utils::Json::JsonValue(getBody), headers, |
| 228 | + Aws::Http::HttpResponseCode::OK)); |
| 229 | +#endif |
| 230 | + std::ofstream(outDir + "/" + MODE + "-" + c.name + "-getitem-attrs.txt") << r.GetItem().size() << "\n"; |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + // Query with 100 medium items. |
| 235 | + { |
| 236 | + Aws::String body = "{\"Count\":100,\"ScannedCount\":100,\"Items\":["; |
| 237 | + const Aws::String one = ItemJson(ItemMedium()); |
| 238 | + for (int i = 0; i < 100; ++i) body += (i ? "," : "") + one; |
| 239 | + body += "]}"; |
| 240 | + QueryRequest q; |
| 241 | + q.SetTableName("bench-table"); |
| 242 | + Report("deserialize", "Query/100xItem_M", Time([&] { |
| 243 | +#ifdef SCHEMA_SERDE |
| 244 | + Aws::AmazonWebServiceResult<Aws::String> wrap(body, headers, Aws::Http::HttpResponseCode::OK); |
| 245 | + QueryResult r(wrap); |
| 246 | + protocol.DeserializeHttpResponse(q.GetSchemaSerdeContext(), 200, headers, |
| 247 | + Aws::Crt::ByteCursorFromArray(reinterpret_cast<const uint8_t*>(body.data()), body.size()), |
| 248 | + *r.GetSchemaSerdeOutput()); |
| 249 | +#else |
| 250 | + QueryResult r(Aws::AmazonWebServiceResult<Aws::Utils::Json::JsonValue>(Aws::Utils::Json::JsonValue(body), headers, |
| 251 | + Aws::Http::HttpResponseCode::OK)); |
| 252 | +#endif |
| 253 | + g_sink += r.GetItems().size(); |
| 254 | + }, 2.0)); |
| 255 | + } |
| 256 | + const double serdeCpu = CpuSeconds() - cpu0; |
| 257 | + |
| 258 | + // End-to-end client calls (endpoint resolution, signing, serialize, canned "HTTP", deserialize). |
| 259 | + Aws::Http::SetHttpClientFactory(Aws::MakeShared<CannedHttpClientFactory>("b")); |
| 260 | + { |
| 261 | + Aws::DynamoDB::DynamoDBClientConfiguration cfg; |
| 262 | + cfg.region = "us-east-1"; |
| 263 | + Aws::DynamoDB::DynamoDBClient client(Aws::Auth::AWSCredentials("AKIDEXAMPLE", "SECRET"), cfg); |
| 264 | + for (const auto& c : cases) { |
| 265 | + PutItemRequest put; |
| 266 | + put.SetTableName("bench-table"); |
| 267 | + put.SetItem(c.item); |
| 268 | + g_responseBody = "{}"; |
| 269 | + Report("e2e", "PutItem/" + c.name, Time([&] { g_sink += client.PutItem(put).IsSuccess(); })); |
| 270 | + g_responseBody = "{\"Item\":" + ItemJson(c.item) + "}"; |
| 271 | + GetItemRequest get; |
| 272 | + get.SetTableName("bench-table"); |
| 273 | + get.AddKey("id", AttributeValue("x")); |
| 274 | + Report("e2e", "GetItem/" + c.name, Time([&] { |
| 275 | + auto o = client.GetItem(get); |
| 276 | + g_sink += o.IsSuccess() ? o.GetResult().GetItem().size() : 0; |
| 277 | + })); |
| 278 | + } |
| 279 | + } |
| 280 | + std::printf("%s\tcpu\tserde-section-cpu-seconds\t%.2f\t\t\t\n", MODE, serdeCpu); |
| 281 | + } |
| 282 | + Aws::ShutdownAPI(options); |
| 283 | + return g_sink == 42 ? 1 : 0; |
| 284 | +} |
0 commit comments