Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
acelyc111 committed Oct 12, 2023
1 parent ed3d31e commit 0136121
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
52 changes: 40 additions & 12 deletions src/aio/test/aio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include <fcntl.h>
#include <fmt/core.h>
// IWYU pragma: no_include <gtest/gtest-param-test.h>
// IWYU pragma: no_include <gtest/gtest-message.h>
// IWYU pragma: no_include <gtest/gtest-test-part.h>
Expand All @@ -48,11 +49,25 @@
#include "utils/env.h"
#include "utils/error_code.h"
#include "utils/filesystem.h"
#include "utils/flags.h"
#include "utils/fmt_logging.h"
#include "utils/ports.h"
#include "utils/test_macros.h"
#include "utils/threadpool_code.h"

DSN_DEFINE_uint32(aio_test,
op_buffer_size,
12,
"The buffer size of each aio read or write operation for the aio_test.basic");
DSN_DEFINE_uint32(aio_test,
total_op_count,
100,
"The total count of read or write operations for the aio_test.basic");
DSN_DEFINE_uint32(
aio_test,
op_count_per_batch,
10,
"The operation count of per read or write batch operation for the aio_test.basic");

using namespace ::dsn;

DEFINE_THREAD_POOL_CODE(THREAD_POOL_TEST_SERVER)
Expand All @@ -62,6 +77,7 @@ class aio_test : public pegasus::encrypt_data_test_base
{
public:
void SetUp() override { utils::filesystem::remove_path(kTestFileName); }
void TearDown() override { utils::filesystem::remove_path(kTestFileName); }

const std::string kTestFileName = "aio_test.txt";
};
Expand All @@ -71,10 +87,10 @@ INSTANTIATE_TEST_CASE_P(, aio_test, ::testing::Values(false));

TEST_P(aio_test, basic)
{
const char *kUnitBuffer = "hello, world";
const size_t kUnitBufferLength = strlen(kUnitBuffer);
const int kTotalBufferCount = 100;
const int kBufferCountPerBatch = 10;
const size_t kUnitBufferLength = FLAGS_op_buffer_size;
const std::string kUnitBuffer(kUnitBufferLength, 'x');
const int kTotalBufferCount = FLAGS_total_op_count;
const int kBufferCountPerBatch = FLAGS_op_count_per_batch;
const int64_t kFileSize = kUnitBufferLength * kTotalBufferCount;
ASSERT_EQ(0, kTotalBufferCount % kBufferCountPerBatch);

Expand All @@ -86,7 +102,7 @@ TEST_P(aio_test, basic)
auto verify_data = [=]() {
int64_t file_size;
ASSERT_TRUE(utils::filesystem::file_size(
kTestFileName.c_str(), dsn::utils::FileDataType::kSensitive, file_size));
kTestFileName, dsn::utils::FileDataType::kSensitive, file_size));
ASSERT_EQ(kFileSize, file_size);

// Create a read file handler.
Expand All @@ -95,6 +111,7 @@ TEST_P(aio_test, basic)

// 1. Check sequential read.
{
pegasus::stop_watch sw;
uint64_t offset = 0;
std::list<aio_task_ptr> tasks;
for (int i = 0; i < kTotalBufferCount; i++) {
Expand All @@ -111,12 +128,14 @@ TEST_P(aio_test, basic)

t->wait();
ASSERT_EQ(kUnitBufferLength, t->get_transferred_size());
ASSERT_STREQ(kUnitBuffer, read_buffer);
ASSERT_STREQ(kUnitBuffer.c_str(), read_buffer);
}
sw.stop_and_output(fmt::format("sequential read"));
}

// 2. Check concurrent read.
{
pegasus::stop_watch sw;
uint64_t offset = 0;
std::list<aio_task_ptr> tasks;
char read_buffers[kTotalBufferCount][kUnitBufferLength + 1];
Expand All @@ -137,22 +156,24 @@ TEST_P(aio_test, basic)
ASSERT_EQ(kUnitBufferLength, t->get_transferred_size());
}
for (int i = 0; i < kTotalBufferCount; i++) {
ASSERT_STREQ(kUnitBuffer, read_buffers[i]);
ASSERT_STREQ(kUnitBuffer.c_str(), read_buffers[i]);
}
sw.stop_and_output(fmt::format("concurrent read"));
}
ASSERT_EQ(ERR_OK, file::close(rfile));
};

// 1. Sequential write.
{
pegasus::stop_watch sw;
auto wfile = file::open(kTestFileName.c_str(), O_RDWR | O_CREAT | O_BINARY, 0666);
ASSERT_NE(wfile, nullptr);

uint64_t offset = 0;
std::list<aio_task_ptr> tasks;
for (int i = 0; i < kTotalBufferCount; i++) {
auto t = ::dsn::file::write(wfile,
kUnitBuffer,
kUnitBuffer.c_str(),
kUnitBufferLength,
offset,
LPC_AIO_TEST,
Expand All @@ -167,11 +188,13 @@ TEST_P(aio_test, basic)
}
ASSERT_EQ(ERR_OK, file::flush(wfile));
ASSERT_EQ(ERR_OK, file::close(wfile));
sw.stop_and_output(fmt::format("sequential write"));
}
NO_FATALS(verify_data());

// 2. Un-sequential write.
{
pegasus::stop_watch sw;
auto wfile = file::open(kTestFileName.c_str(), O_RDWR | O_CREAT | O_BINARY, 0666);
ASSERT_NE(wfile, nullptr);

Expand All @@ -188,7 +211,7 @@ TEST_P(aio_test, basic)
std::list<aio_task_ptr> tasks;
for (const auto &offset : offsets) {
auto t = ::dsn::file::write(wfile,
kUnitBuffer,
kUnitBuffer.c_str(),
kUnitBufferLength,
offset,
LPC_AIO_TEST,
Expand All @@ -202,19 +225,21 @@ TEST_P(aio_test, basic)
}
ASSERT_EQ(ERR_OK, file::flush(wfile));
ASSERT_EQ(ERR_OK, file::close(wfile));
sw.stop_and_output(fmt::format("un-sequential write"));
}
NO_FATALS(verify_data());

// 3. Overwrite.
{
pegasus::stop_watch sw;
auto wfile = file::open(kTestFileName.c_str(), O_RDWR | O_CREAT | O_BINARY, 0666);
ASSERT_NE(wfile, nullptr);

uint64_t offset = 0;
std::list<aio_task_ptr> tasks;
for (int i = 0; i < kTotalBufferCount; i++) {
auto t = ::dsn::file::write(wfile,
kUnitBuffer,
kUnitBuffer.c_str(),
kUnitBufferLength,
offset,
LPC_AIO_TEST,
Expand All @@ -229,19 +254,21 @@ TEST_P(aio_test, basic)
}
ASSERT_EQ(ERR_OK, file::flush(wfile));
ASSERT_EQ(ERR_OK, file::close(wfile));
sw.stop_and_output(fmt::format("overwrite"));
}
NO_FATALS(verify_data());

// 4. Vector write.
{
pegasus::stop_watch sw;
auto wfile = file::open(kTestFileName.c_str(), O_RDWR | O_CREAT | O_BINARY, 0666);
ASSERT_NE(wfile, nullptr);

uint64_t offset = 0;
std::list<aio_task_ptr> tasks;
std::unique_ptr<dsn_file_buffer_t[]> buffers(new dsn_file_buffer_t[kBufferCountPerBatch]);
for (int i = 0; i < kBufferCountPerBatch; i++) {
buffers[i].buffer = static_cast<void *>(const_cast<char *>(kUnitBuffer));
buffers[i].buffer = static_cast<void *>(const_cast<char *>(kUnitBuffer.c_str()));
buffers[i].size = kUnitBufferLength;
}
for (int i = 0; i < kTotalBufferCount / kBufferCountPerBatch; i++) {
Expand All @@ -264,6 +291,7 @@ TEST_P(aio_test, basic)
}
ASSERT_EQ(ERR_OK, file::flush(wfile));
ASSERT_EQ(ERR_OK, file::close(wfile));
sw.stop_and_output(fmt::format("vector write"));
}
NO_FATALS(verify_data());
}
Expand Down
24 changes: 23 additions & 1 deletion src/test_util/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@

#pragma once

#include <gtest/gtest.h>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <gtest/gtest.h>
#include <string>

#include "fmt/core.h"
#include "runtime/api_layer1.h"
#include "utils/flags.h"
#include "utils/test_macros.h"

Expand All @@ -43,6 +48,23 @@ class encrypt_data_test_base : public testing::TestWithParam<bool>
encrypt_data_test_base() { FLAGS_encrypt_data_at_rest = GetParam(); }
};

class stop_watch
{
public:
stop_watch() { _start_ms = dsn_now_ms(); }
void stop_and_output(const std::string &msg)
{
auto duration_ms =
std::chrono::duration_cast<std::chrono::duration<double>>(
std::chrono::milliseconds(static_cast<int64_t>(dsn_now_ms() - _start_ms)))
.count();
fmt::print(stdout, "{}, cost {} ms\n", msg, duration_ms);
}

private:
uint64_t _start_ms = 0;
};

void create_local_test_file(const std::string &full_name, dsn::replication::file_meta *fm);

#define ASSERT_EVENTUALLY(expr) \
Expand Down

0 comments on commit 0136121

Please sign in to comment.