|
1 | 1 | #include <benchmark/benchmark.h> |
2 | 2 | #include <cppfig/cppfig.h> |
| 3 | +#include <cppfig/json.h> |
3 | 4 |
|
4 | 5 | #include <cstdlib> |
5 | 6 | #include <filesystem> |
@@ -114,7 +115,7 @@ class BenchmarkFixture : public ::benchmark::Fixture { |
114 | 115 | { |
115 | 116 | std::filesystem::path temp_path = std::filesystem::temp_directory_path() / "cppfig_benchmark"; |
116 | 117 | temp_path += std::to_string(reinterpret_cast<uintptr_t>(this)); |
117 | | - temp_path += ".json"; |
| 118 | + temp_path += ".conf"; |
118 | 119 | return temp_path.string(); |
119 | 120 | } |
120 | 121 |
|
@@ -457,82 +458,100 @@ BENCHMARK_REGISTER_F(BenchmarkFixture, JsonSerializerParse); |
457 | 458 |
|
458 | 459 | BENCHMARK_DEFINE_F(BenchmarkFixture, JsonSerializerStringify)(benchmark::State& state) |
459 | 460 | { |
460 | | - nlohmann::json json = { { "benchmark", |
461 | | - { { "string", "test" }, { "int", 42 }, { "double", 3.14159 }, { "bool", true }, { "validated", 50 } } } }; |
| 461 | + auto value = Value::Object(); |
| 462 | + auto benchmark_obj = Value::Object(); |
| 463 | + benchmark_obj["string"] = Value("test"); |
| 464 | + benchmark_obj["int"] = Value(42); |
| 465 | + benchmark_obj["double"] = Value(3.14159); |
| 466 | + benchmark_obj["bool"] = Value(true); |
| 467 | + benchmark_obj["validated"] = Value(50); |
| 468 | + value["benchmark"] = benchmark_obj; |
462 | 469 |
|
463 | 470 | for (auto _ : state) { |
464 | | - auto result = JsonSerializer::Stringify(json); |
| 471 | + auto result = JsonSerializer::Stringify(value); |
465 | 472 | benchmark::DoNotOptimize(result); |
466 | 473 | } |
467 | 474 | } |
468 | 475 | BENCHMARK_REGISTER_F(BenchmarkFixture, JsonSerializerStringify); |
469 | 476 |
|
470 | | -BENCHMARK_DEFINE_F(BenchmarkFixture, JsonSerializerGetAtPath)(benchmark::State& state) |
| 477 | +BENCHMARK_DEFINE_F(BenchmarkFixture, ValueGetAtPath)(benchmark::State& state) |
471 | 478 | { |
472 | | - nlohmann::json json = { { "database", { { "connection", { { "host", "localhost" }, { "port", 5432 } } } } } }; |
| 479 | + auto value = Value::Object(); |
| 480 | + auto database = Value::Object(); |
| 481 | + auto connection = Value::Object(); |
| 482 | + connection["host"] = Value("localhost"); |
| 483 | + connection["port"] = Value(5432); |
| 484 | + database["connection"] = connection; |
| 485 | + value["database"] = database; |
473 | 486 | std::string path = "database.connection.host"; |
474 | 487 |
|
475 | 488 | for (auto _ : state) { |
476 | | - auto result = JsonSerializer::GetAtPath(json, path); |
| 489 | + auto result = value.GetAtPath(path); |
477 | 490 | benchmark::DoNotOptimize(result); |
478 | 491 | } |
479 | 492 | } |
480 | | -BENCHMARK_REGISTER_F(BenchmarkFixture, JsonSerializerGetAtPath); |
| 493 | +BENCHMARK_REGISTER_F(BenchmarkFixture, ValueGetAtPath); |
481 | 494 |
|
482 | | -BENCHMARK_DEFINE_F(BenchmarkFixture, JsonSerializerSetAtPath)(benchmark::State& state) |
| 495 | +BENCHMARK_DEFINE_F(BenchmarkFixture, ValueSetAtPath)(benchmark::State& state) |
483 | 496 | { |
484 | | - nlohmann::json json = { { "database", { { "connection", { { "host", "localhost" }, { "port", 5432 } } } } } }; |
| 497 | + auto value = Value::Object(); |
| 498 | + auto database = Value::Object(); |
| 499 | + auto connection = Value::Object(); |
| 500 | + connection["host"] = Value("localhost"); |
| 501 | + connection["port"] = Value(5432); |
| 502 | + database["connection"] = connection; |
| 503 | + value["database"] = database; |
485 | 504 | std::string path = "database.connection.host"; |
486 | 505 |
|
487 | 506 | for (auto _ : state) { |
488 | | - nlohmann::json json_copy = json; |
489 | | - JsonSerializer::SetAtPath(json_copy, path, "example.com"); |
490 | | - benchmark::DoNotOptimize(json_copy); |
| 507 | + Value value_copy = value; |
| 508 | + value_copy.SetAtPath(path, Value("example.com")); |
| 509 | + benchmark::DoNotOptimize(value_copy); |
491 | 510 | } |
492 | 511 | } |
493 | | -BENCHMARK_REGISTER_F(BenchmarkFixture, JsonSerializerSetAtPath); |
| 512 | +BENCHMARK_REGISTER_F(BenchmarkFixture, ValueSetAtPath); |
494 | 513 |
|
495 | | -BENCHMARK_DEFINE_F(BenchmarkFixture, TraitsToJsonInt)(benchmark::State& state) |
| 514 | +BENCHMARK_DEFINE_F(BenchmarkFixture, TraitsSerializeInt)(benchmark::State& state) |
496 | 515 | { |
497 | 516 | for (auto _ : state) { |
498 | | - auto json = ConfigTraits<int>::ToJson(42); |
499 | | - benchmark::DoNotOptimize(json); |
| 517 | + auto val = ConfigTraits<int>::Serialize(42); |
| 518 | + benchmark::DoNotOptimize(val); |
500 | 519 | } |
501 | 520 | } |
502 | | -BENCHMARK_REGISTER_F(BenchmarkFixture, TraitsToJsonInt); |
| 521 | +BENCHMARK_REGISTER_F(BenchmarkFixture, TraitsSerializeInt); |
503 | 522 |
|
504 | | -BENCHMARK_DEFINE_F(BenchmarkFixture, TraitsFromJsonInt)(benchmark::State& state) |
| 523 | +BENCHMARK_DEFINE_F(BenchmarkFixture, TraitsDeserializeInt)(benchmark::State& state) |
505 | 524 | { |
506 | | - nlohmann::json json = 42; |
| 525 | + Value val(42); |
507 | 526 |
|
508 | 527 | for (auto _ : state) { |
509 | | - auto value = ConfigTraits<int>::FromJson(json); |
510 | | - benchmark::DoNotOptimize(value); |
| 528 | + auto result = ConfigTraits<int>::Deserialize(val); |
| 529 | + benchmark::DoNotOptimize(result); |
511 | 530 | } |
512 | 531 | } |
513 | | -BENCHMARK_REGISTER_F(BenchmarkFixture, TraitsFromJsonInt); |
| 532 | +BENCHMARK_REGISTER_F(BenchmarkFixture, TraitsDeserializeInt); |
514 | 533 |
|
515 | | -BENCHMARK_DEFINE_F(BenchmarkFixture, TraitsToJsonString)(benchmark::State& state) |
| 534 | +BENCHMARK_DEFINE_F(BenchmarkFixture, TraitsSerializeString)(benchmark::State& state) |
516 | 535 | { |
517 | 536 | std::string value = "benchmark_test_string"; |
518 | 537 |
|
519 | 538 | for (auto _ : state) { |
520 | | - auto json = ConfigTraits<std::string>::ToJson(value); |
521 | | - benchmark::DoNotOptimize(json); |
| 539 | + auto val = ConfigTraits<std::string>::Serialize(value); |
| 540 | + benchmark::DoNotOptimize(val); |
522 | 541 | } |
523 | 542 | } |
524 | | -BENCHMARK_REGISTER_F(BenchmarkFixture, TraitsToJsonString); |
| 543 | +BENCHMARK_REGISTER_F(BenchmarkFixture, TraitsSerializeString); |
525 | 544 |
|
526 | | -BENCHMARK_DEFINE_F(BenchmarkFixture, TraitsFromJsonString)(benchmark::State& state) |
| 545 | +BENCHMARK_DEFINE_F(BenchmarkFixture, TraitsDeserializeString)(benchmark::State& state) |
527 | 546 | { |
528 | | - nlohmann::json json = "benchmark_test_string"; |
| 547 | + Value val("benchmark_test_string"); |
529 | 548 |
|
530 | 549 | for (auto _ : state) { |
531 | | - auto value = ConfigTraits<std::string>::FromJson(json); |
532 | | - benchmark::DoNotOptimize(value); |
| 550 | + auto result = ConfigTraits<std::string>::Deserialize(val); |
| 551 | + benchmark::DoNotOptimize(result); |
533 | 552 | } |
534 | 553 | } |
535 | | -BENCHMARK_REGISTER_F(BenchmarkFixture, TraitsFromJsonString); |
| 554 | +BENCHMARK_REGISTER_F(BenchmarkFixture, TraitsDeserializeString); |
536 | 555 |
|
537 | 556 | } // namespace cppfig::bench |
538 | 557 |
|
|
0 commit comments