Skip to content

Commit

Permalink
[PP-3211] Add cql wire data generation.
Browse files Browse the repository at this point in the history
Summary:
For the purposes of the benchmark, we want to be able to generate a bunch of wire data for each protocol. This diff adds the utilities to make data generation easier. These utilities could also be used in the tests instead of the ad-hoc char[] byte buffers that the tests currently used. This diff adds a couple examples in the tests to demonstrate this usage.

Note, this diff also restructures the build file so that test utilities don't get built into the main binary.

Test Plan: No functional changes, so relying on existing tests. This diff also increases test coverage.

Reviewers: #stirling, oazizi

Reviewed By: #stirling, oazizi

Subscribers: oazizi, yzhao

JIRA Issues: PP-3211

Signed-off-by: James Bartlett <jamesbartlett@pixielabs.ai>

Differential Revision: https://phab.corp.pixielabs.ai/D10784

GitOrigin-RevId: 9317f15
  • Loading branch information
JamesMBartlett authored and copybaranaut committed Feb 17, 2022
1 parent 4fe6d48 commit 2dc928c
Show file tree
Hide file tree
Showing 6 changed files with 443 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/stirling/source_connectors/socket_tracer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pl_cc_test(
srcs = ["socket_trace_connector_test.cc"],
deps = [
":cc_library",
"//src/stirling/source_connectors/socket_tracer/protocols/cql:testing",
"//src/stirling/source_connectors/socket_tracer/testing:cc_library",
"//src/stirling/testing:cc_library",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ pl_cc_library(
name = "cc_library",
srcs = glob(
["*.cc"],
exclude = ["**/*_test.cc"],
exclude = [
"**/*_test.cc",
"test_utils.cc",
],
),
hdrs = glob(
["*.h"],
exclude = ["test_utils.h"],
),
hdrs = glob(["*.h"]),
deps = [
"//src/common/json:cc_library",
"//src/stirling/source_connectors/socket_tracer/protocols/common:cc_library",
Expand All @@ -33,20 +39,36 @@ pl_cc_library(
],
)

pl_cc_library(
name = "testing",
srcs = [
"test_utils.cc",
"test_utils.h",
],
hdrs = ["test_utils.h"],
deps = [":cc_library"],
)

pl_cc_test(
name = "parse_test",
srcs = ["parse_test.cc"],
deps = [":cc_library"],
deps = [
":testing",
],
)

pl_cc_test(
name = "stitcher_test",
srcs = ["stitcher_test.cc"],
deps = [":cc_library"],
deps = [
":testing",
],
)

pl_cc_test(
name = "frame_body_decoder_test",
srcs = ["frame_body_decoder_test.cc"],
deps = [":cc_library"],
deps = [
":testing",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <gtest/gtest.h>

#include "src/stirling/source_connectors/socket_tracer/protocols/cql/frame_body_decoder.h"
#include "src/stirling/source_connectors/socket_tracer/protocols/cql/test_utils.h"

#include "src/common/testing/testing.h"

Expand All @@ -33,6 +34,8 @@ using ::testing::IsEmpty;
using ::testing::Pair;
using ::testing::UnorderedElementsAre;

using ::px::stirling::protocols::cass::testutils::CreateFrame;

//-----------------------------------------------------------------------------
// Test Data
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -766,6 +769,70 @@ TEST(ExtractSchemaChange, Basic) {
EXPECT_THAT(sc.arg_types, IsEmpty());
}

TEST(ParseResultResp, RowsResult) {
ResultResp expected_resp;
expected_resp.kind = ResultRespKind::kRows;

ResultRowsResp expected_rows_resp;
expected_rows_resp.metadata.flags = 0;
expected_rows_resp.metadata.columns_count = 2;
ColSpec col_spec;
col_spec.ks_name = "keyspace";
col_spec.table_name = "table";
col_spec.name = "col1";
col_spec.type = {
.type = protocols::cass::DataType::kVarchar,
.value = "",
};
expected_rows_resp.metadata.col_specs.push_back(col_spec);
col_spec.name = "col2";
expected_rows_resp.metadata.col_specs.push_back(col_spec);

// Estimate body size (without rows) by settings rows_count to 0.
expected_rows_resp.rows_count = 0;
expected_resp.resp = expected_rows_resp;
auto resp_body = testutils::ResultRespToByteString(expected_resp);

uint16_t stream = 1;
uint64_t ts = 1;
auto frame = CreateFrame(stream, Opcode::kResult, resp_body, ts);

ASSERT_OK_AND_ASSIGN(ResultResp resp, ParseResultResp(&frame));

EXPECT_EQ(resp, expected_resp);
}

TEST(ParseResultResp, VoidResult) {
ResultResp expected_resp;
expected_resp.kind = ResultRespKind::kVoid;

auto resp_body = testutils::ResultRespToByteString(expected_resp);

uint16_t stream = 1;
uint64_t ts = 1;
auto frame = CreateFrame(stream, Opcode::kResult, resp_body, ts);

ASSERT_OK_AND_ASSIGN(ResultResp resp, ParseResultResp(&frame));

EXPECT_EQ(resp, expected_resp);
}

TEST(ParseQueryReq, Basic) {
QueryReq expected_req;
expected_req.query = "SELECT * FROM table;";
expected_req.qp.flags = 0;
expected_req.qp.consistency = 1;

auto body = testutils::QueryReqToByteString(expected_req);

uint16_t stream = 1;
uint64_t ts = 1;
auto frame = CreateFrame(stream, Opcode::kResult, body, ts);

ASSERT_OK_AND_ASSIGN(QueryReq req, ParseQueryReq(&frame));
EXPECT_EQ(req, expected_req);
}

} // namespace cass
} // namespace protocols
} // namespace stirling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "src/stirling/source_connectors/socket_tracer/protocols/cql/parse.h"
#include "src/stirling/source_connectors/socket_tracer/protocols/cql/stitcher.h"
#include "src/stirling/source_connectors/socket_tracer/protocols/cql/test_utils.h"

using ::testing::HasSubstr;
using ::testing::IsEmpty;
Expand All @@ -30,6 +31,7 @@ namespace stirling {
namespace protocols {
namespace cass {

using testutils::CreateFrame;
//-----------------------------------------------------------------------------
// Test Data
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -167,39 +169,6 @@ constexpr uint8_t kEventResp[] = {0x00, 0x0d, 0x53, 0x43, 0x48, 0x45, 0x4d, 0x41
0x74, 0x75, 0x74, 0x6f, 0x72, 0x69, 0x61, 0x6c, 0x73, 0x70, 0x6f,
0x69, 0x6e, 0x74, 0x00, 0x03, 0x65, 0x6d, 0x70};

//-----------------------------------------------------------------------------
// Test Utils
//-----------------------------------------------------------------------------

// Required because zero length C-arrays are not allowed in C++, so can't use the version above.
Frame CreateFrame(uint16_t stream, Opcode opcode, uint64_t timestamp_ns) {
// Should be either a request or response opcode.
CHECK(IsReqOpcode(opcode) != IsRespOpcode(opcode));

Frame f;
f.hdr.opcode = opcode;
f.hdr.stream = stream;
f.hdr.flags = 0;
f.hdr.version = 0x04;
f.hdr.flags = 0;
f.hdr.length = 0;
f.msg = "";
f.timestamp_ns = timestamp_ns;
return f;
}

// This version populates a body.
template <size_t N>
Frame CreateFrame(uint16_t stream, Opcode opcode, const uint8_t (&msg)[N], uint64_t timestamp_ns) {
Frame f = CreateFrame(stream, opcode, timestamp_ns);

std::string_view msg_view = CreateCharArrayView<char>(msg);
f.hdr.length = msg_view.length();
f.msg = msg_view;

return f;
}

//-----------------------------------------------------------------------------
// Test Cases
//-----------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 2dc928c

Please sign in to comment.