Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ if(PROTOCYTE_SMOKE_BUILD_BENCHMARKS)
FetchContent_MakeAvailable(protobuf)
set(PROTOCYTE_PROTOC_EXECUTABLE "$<TARGET_FILE:protobuf::protoc>" CACHE INTERNAL "protoc executable for protocyte")
endif()
set(PROTOCYTE_PROTOCYTE_BENCHMARK_GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated/protocyte")
set(PROTOCYTE_PROTOCYTE_BENCHMARK_HEADER "${PROTOCYTE_PROTOCYTE_BENCHMARK_GENERATED_DIR}/benchmark.protocyte.hpp")
set(PROTOCYTE_PROTOCYTE_BENCHMARK_SOURCE "${PROTOCYTE_PROTOCYTE_BENCHMARK_GENERATED_DIR}/benchmark.protocyte.cpp")
protocyte_generate(
TARGET protocyte_smoke_benchmark_codegen
PROTO_ROOT "${PROTOCYTE_SMOKE_PROTO_DIR}"
OUT_DIR "${PROTOCYTE_PROTOCYTE_BENCHMARK_GENERATED_DIR}"
PROTOS "${PROTOCYTE_BENCHMARK_PROTO}"
OPTIONS "namespace_prefix=protocyte_smoke"
)

set(PROTOCYTE_PROTOBUF_BENCHMARK_GENERATED_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated/protobuf")
set(PROTOCYTE_PROTOBUF_BENCHMARK_HEADER "${PROTOCYTE_PROTOBUF_BENCHMARK_GENERATED_DIR}/benchmark.pb.h")
set(PROTOCYTE_PROTOBUF_BENCHMARK_SOURCE "${PROTOCYTE_PROTOBUF_BENCHMARK_GENERATED_DIR}/benchmark.pb.cc")
Expand Down Expand Up @@ -253,11 +264,14 @@ endif()
if(PROTOCYTE_SMOKE_BUILD_BENCHMARKS)
add_executable(protocyte_host_benchmark
src/host_benchmark.cpp
"${PROTOCYTE_GENERATED_DIR}/example.protocyte.cpp"
"${PROTOCYTE_GENERATED_DIR}/compat.protocyte.cpp"
"${PROTOCYTE_GENERATED_DIR}/cross_package.protocyte.cpp"
"${PROTOCYTE_PROTOCYTE_BENCHMARK_SOURCE}"
)
target_include_directories(
protocyte_host_benchmark
PRIVATE
"${PROTOCYTE_GENERATED_DIR}"
"${PROTOCYTE_PROTOCYTE_BENCHMARK_GENERATED_DIR}"
)
target_include_directories(protocyte_host_benchmark PRIVATE "${PROTOCYTE_GENERATED_DIR}")
target_compile_definitions(
protocyte_host_benchmark
PRIVATE
Expand All @@ -271,6 +285,7 @@ if(PROTOCYTE_SMOKE_BUILD_BENCHMARKS)
if(PROTOCYTE_SMOKE_REGENERATE)
add_dependencies(protocyte_host_benchmark protocyte_smoke_regenerate)
endif()
add_dependencies(protocyte_host_benchmark protocyte_smoke_benchmark_codegen)

if(MSVC)
target_compile_options(protocyte_host_benchmark PRIVATE /W4 /permissive-)
Expand Down Expand Up @@ -313,11 +328,56 @@ if(PROTOCYTE_SMOKE_BUILD_BENCHMARKS)
else()
target_compile_options(protocyte_host_protobuf_benchmark PRIVATE -Wall -Wextra -Wpedantic)
endif()

add_executable(protocyte_host_benchmark_smoke
src/benchmark_compat_smoke.cpp
"${PROTOCYTE_PROTOCYTE_BENCHMARK_SOURCE}"
"${PROTOCYTE_PROTOBUF_BENCHMARK_SOURCE}"
)
target_include_directories(
protocyte_host_benchmark_smoke
PRIVATE
"${PROTOCYTE_GENERATED_DIR}"
"${PROTOCYTE_PROTOCYTE_BENCHMARK_GENERATED_DIR}"
"${PROTOCYTE_PROTOBUF_BENCHMARK_GENERATED_DIR}"
)
target_compile_definitions(
protocyte_host_benchmark_smoke
PRIVATE
PROTOCYTE_ENABLE_HOSTED_ALLOCATOR=1
PROTOCYTE_ENABLE_STD_STRING_VIEW=1
)
protocyte_smoke_enable_std_format_if_available(protocyte_host_benchmark_smoke)
target_compile_features(protocyte_host_benchmark_smoke PRIVATE cxx_std_20)
target_link_libraries(
protocyte_host_benchmark_smoke
PRIVATE
Catch2::Catch2WithMain
"${protocyte_smoke_libprotobuf_target}"
)
add_dependencies(
protocyte_host_benchmark_smoke
protocyte_smoke_benchmark_codegen
protocyte_smoke_protobuf_benchmark_codegen
)

if(PROTOCYTE_SMOKE_REGENERATE)
add_dependencies(protocyte_host_benchmark_smoke protocyte_smoke_regenerate)
endif()

if(MSVC)
target_compile_options(protocyte_host_benchmark_smoke PRIVATE /W4 /permissive-)
else()
target_compile_options(protocyte_host_benchmark_smoke PRIVATE -Wall -Wextra -Wpedantic)
endif()
endif()

enable_testing()
include(Catch)
catch_discover_tests(protocyte_host_smoke)
if(PROTOCYTE_SMOKE_BUILD_BENCHMARKS)
catch_discover_tests(protocyte_host_benchmark_smoke)
endif()

if(PROTOCYTE_SMOKE_BUILD_DRIVER)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
Expand Down
8 changes: 4 additions & 4 deletions smoke/generated/compat.protocyte.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1680,7 +1680,7 @@ namespace protocyte_smoke::test::compat {
return st;
}
}
for (const auto entry : map_str_int32_) {
for (const auto &entry : map_str_int32_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -1727,7 +1727,7 @@ namespace protocyte_smoke::test::compat {
return st;
}
}
for (const auto entry : map_int32_str_) {
for (const auto &entry : map_int32_str_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -2054,7 +2054,7 @@ namespace protocyte_smoke::test::compat {
}
total = *st_size;
}
for (const auto entry : map_str_int32_) {
for (const auto &entry : map_str_int32_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -2092,7 +2092,7 @@ namespace protocyte_smoke::test::compat {
}
total = *st_size;
}
for (const auto entry : map_int32_str_) {
for (const auto &entry : map_int32_str_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down
24 changes: 12 additions & 12 deletions smoke/generated/example.protocyte.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ namespace test::ultimate {
return st;
}
}
for (const auto entry : weird_map_) {
for (const auto &entry : weird_map_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -1425,7 +1425,7 @@ namespace test::ultimate {
}
total = *st_size;
}
for (const auto entry : weird_map_) {
for (const auto &entry : weird_map_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -4480,7 +4480,7 @@ namespace test::ultimate {
return st;
}
}
for (const auto entry : map_str_int32_) {
for (const auto &entry : map_str_int32_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -4527,7 +4527,7 @@ namespace test::ultimate {
return st;
}
}
for (const auto entry : map_int32_str_) {
for (const auto &entry : map_int32_str_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -4574,7 +4574,7 @@ namespace test::ultimate {
return st;
}
}
for (const auto entry : map_bool_bytes_) {
for (const auto &entry : map_bool_bytes_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -4621,7 +4621,7 @@ namespace test::ultimate {
return st;
}
}
for (const auto entry : map_uint64_msg_) {
for (const auto &entry : map_uint64_msg_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -4668,7 +4668,7 @@ namespace test::ultimate {
return st;
}
}
for (const auto entry : very_nested_map_) {
for (const auto &entry : very_nested_map_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -5196,7 +5196,7 @@ namespace test::ultimate {
}
total = *st_size;
}
for (const auto entry : map_str_int32_) {
for (const auto &entry : map_str_int32_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -5234,7 +5234,7 @@ namespace test::ultimate {
}
total = *st_size;
}
for (const auto entry : map_int32_str_) {
for (const auto &entry : map_int32_str_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -5272,7 +5272,7 @@ namespace test::ultimate {
}
total = *st_size;
}
for (const auto entry : map_bool_bytes_) {
for (const auto &entry : map_bool_bytes_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -5310,7 +5310,7 @@ namespace test::ultimate {
}
total = *st_size;
}
for (const auto entry : map_uint64_msg_) {
for (const auto &entry : map_uint64_msg_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down Expand Up @@ -5348,7 +5348,7 @@ namespace test::ultimate {
}
total = *st_size;
}
for (const auto entry : very_nested_map_) {
for (const auto &entry : very_nested_map_) {
enum struct EntryFieldNumber : ::protocyte::u32 {
key = 1u,
value = 2u,
Expand Down
32 changes: 15 additions & 17 deletions smoke/generated/protocyte/runtime/runtime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2924,6 +2924,13 @@ namespace protocyte {
return {};
}

Status validate() const noexcept {
if (!validate_utf8(byte_view())) {
return protocyte::unexpected(ErrorCode::invalid_utf8, {});
}
return {};
}

protected:
Status check_size_limit(const usize size) const noexcept {
if (const auto ctx = bytes_.context(); ctx != nullptr && size > ctx->limits.max_string_bytes) {
Expand Down Expand Up @@ -4202,12 +4209,8 @@ namespace protocyte {
return st;
}
auto buffer = temp.mutable_view();
for (usize i {}; i < size; ++i) {
auto byte = reader.read_byte();
if (!byte) {
return byte.status();
}
buffer.data()[i] = *byte;
if (const auto st = reader.read(buffer.data(), buffer.size()); !st) {
return st;
}
out = protocyte::move(temp);
return {};
Expand All @@ -4233,20 +4236,15 @@ namespace protocyte {
if (size > ctx.limits.max_string_bytes) {
return protocyte::unexpected(ErrorCode::size_limit, reader.position());
}
typename Config::Bytes buffer {&ctx};
if (const auto st = buffer.resize_for_overwrite(size); !st) {
typename Config::String temp {&ctx};
if (const auto st = temp.resize_for_overwrite(size); !st) {
return st;
}
auto bytes = buffer.mutable_view();
for (usize i {}; i < size; ++i) {
auto byte = reader.read_byte();
if (!byte) {
return byte.status();
}
bytes.data()[i] = *byte;
auto bytes = temp.mutable_view_for_overwrite();
if (const auto st = reader.read(bytes.data(), bytes.size()); !st) {
return st;
}
typename Config::String temp {&ctx};
if (const auto st = temp.assign_owned(protocyte::move(buffer)); !st) {
if (const auto st = temp.validate(); !st) {
return st;
}
out = protocyte::move(temp);
Expand Down
71 changes: 71 additions & 0 deletions smoke/src/benchmark_compat_smoke.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <string>
#include <vector>

#include <catch2/catch_test_macros.hpp>

#include "protobuf_benchmark_fixture.hpp"
#include "protocyte_benchmark_fixture.hpp"

namespace {

template<class TStatusLike> void require_success(const TStatusLike &status_like) {
if (!status_like) {
const auto error = status_like.error();
CAPTURE(static_cast<uint32_t>(error.code), error.offset, error.field_number);
}
REQUIRE(status_like);
}

std::vector<protocyte::u8>
serialize_protocyte(const protocyte_smoke::benchmark_fixture::ProtocyteMessage &message) {
const auto size = message.encoded_size();
require_success(size);
std::vector<protocyte::u8> encoded(*size);
protocyte::SliceWriter writer(encoded.data(), encoded.size());
require_success(message.serialize(writer));
REQUIRE(writer.position() == encoded.size());
return encoded;
}

template<class T, protocyte::usize Extent>
bool view_equal(const protocyte::Span<T, Extent> lhs, const std::string &rhs) {
return lhs.size() == rhs.size() && protocyte::bytes_equal(protocyte::Span<const T> {lhs.data(), lhs.size()},
protocyte::Span<const char> {rhs.data(), rhs.size()});
}

} // namespace

TEST_CASE("benchmark schema round trips between Protocyte and protobuf", "[smoke][benchmark][protobuf]") {
namespace fixture = protocyte_smoke::benchmark_fixture;

auto ctx = fixture::make_context();
fixture::ProtocyteMessage protocyte_message(ctx);
require_success(fixture::populate_protocyte_message(protocyte_message, ctx));

const auto protocyte_encoded = serialize_protocyte(protocyte_message);
fixture::ProtobufMessage protobuf_from_protocyte;
REQUIRE(
protobuf_from_protocyte.ParseFromArray(protocyte_encoded.data(), static_cast<int>(protocyte_encoded.size())));
CHECK(protobuf_from_protocyte.ByteSizeLong() == protocyte_encoded.size());
CHECK(protobuf_from_protocyte.f_int32() == 42);
CHECK(protobuf_from_protocyte.oneof_bytes() == fixture::bytes_of(fixture::view_of(fixture::oneof_bytes)));
CHECK(protobuf_from_protocyte.map_str_int32().at(fixture::bytes_of(fixture::view_of(fixture::map_key))) == 301);

fixture::ProtobufMessage protobuf_message;
fixture::populate_protobuf_message(&protobuf_message);
const auto protobuf_encoded = protobuf_message.SerializeAsString();

auto parse_ctx = fixture::make_context();
fixture::ProtocyteMessage protocyte_from_protobuf(parse_ctx);
protocyte::SliceReader reader(reinterpret_cast<const protocyte::u8 *>(protobuf_encoded.data()),
protobuf_encoded.size());
require_success(protocyte_from_protobuf.merge_from(reader));
REQUIRE(reader.eof());

const auto protocyte_size = protocyte_from_protobuf.encoded_size();
require_success(protocyte_size);
CHECK(*protocyte_size == protobuf_message.ByteSizeLong());
CHECK(protocyte_from_protobuf.f_int32() == 42);
CHECK(view_equal(protocyte_from_protobuf.oneof_bytes(), protobuf_message.oneof_bytes()));
CHECK(protocyte_from_protobuf.map_str_int32().size() == 1u);
}
Loading
Loading