Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Dueplay committed Apr 2, 2024
1 parent 464515b commit 4ce19b2
Show file tree
Hide file tree
Showing 29 changed files with 100 additions and 16 deletions.
6 changes: 4 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
"numbers": "cpp",
"semaphore": "cpp",
"stop_token": "cpp",
"strstream": "cpp"
}
"strstream": "cpp",
"cassert": "cpp"
},
"cmake.configureOnOpen": false
}
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ ADD_EXECUTABLE(buffer_test ${NEXT_SERVER_TEST_DIR}/core/buffer_test.cpp)
TARGET_LINK_LIBRARIES(buffer_test PRIVATE Catch2::Catch2WithMain next_core)

ADD_EXECUTABLE(cache_test ${NEXT_SERVER_TEST_DIR}/core/cache_test.cpp)
TARGET_LINK_LIBRARIES(cache_test PRIVATE Catch2::Catch2WithMain next_core)
TARGET_LINK_LIBRARIES(cache_test PRIVATE Catch2::Catch2WithMain next_core next_http)

ADD_EXECUTABLE(timer_test ${NEXT_SERVER_TEST_DIR}/core/timer_test.cpp)
TARGET_LINK_LIBRARIES(timer_test PRIVATE Catch2::Catch2WithMain next_core)
Expand Down
Binary file modified bin/echo_client
Binary file not shown.
Binary file modified bin/echo_server
Binary file not shown.
3 changes: 3 additions & 0 deletions build/CMakeCache.txt
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@ GITCOMMAND:FILEPATH=/usr/bin/git
//Git command line client
GIT_EXECUTABLE:FILEPATH=/usr/bin/git

//No help, variable specified on the command line.
LOG_LEVEL:UNINITIALIZED=NOLOG

//Command to build the project
MAKECOMMAND:STRING=/usr/bin/cmake --build . --config "${CTEST_CONFIGURATION_TYPE}"

Expand Down
3 changes: 2 additions & 1 deletion build/CMakeFiles/Makefile2
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ CMakeFiles/buffer_test.dir/clean:

# All Build rule for target.
CMakeFiles/cache_test.dir/all: CMakeFiles/next_core.dir/all
CMakeFiles/cache_test.dir/all: CMakeFiles/next_http.dir/all
CMakeFiles/cache_test.dir/all: CMakeFiles/next_log.dir/all
CMakeFiles/cache_test.dir/all: _deps/catch2-build/src/CMakeFiles/Catch2.dir/all
CMakeFiles/cache_test.dir/all: _deps/catch2-build/src/CMakeFiles/Catch2WithMain.dir/all
Expand All @@ -502,7 +503,7 @@ CMakeFiles/cache_test.dir/all: _deps/catch2-build/src/CMakeFiles/Catch2WithMain.

# Build rule for subdir invocation for target.
CMakeFiles/cache_test.dir/rule: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/gxj/Next/build/CMakeFiles 70
$(CMAKE_COMMAND) -E cmake_progress_start /home/gxj/Next/build/CMakeFiles 74
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 CMakeFiles/cache_test.dir/all
$(CMAKE_COMMAND) -E cmake_progress_start /home/gxj/Next/build/CMakeFiles 0
.PHONY : CMakeFiles/cache_test.dir/rule
Expand Down
2 changes: 1 addition & 1 deletion build/CMakeFiles/echo_client.dir/flags.make
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Generated by "Unix Makefiles" Generator, CMake Version 3.22

# compile CXX with /usr/bin/c++
CXX_DEFINES = -DTIMER_EXPIRATION=0
CXX_DEFINES = -DNOLOG -DTIMER_EXPIRATION=0

CXX_INCLUDES = -I/home/gxj/Next/src/include

Expand Down
2 changes: 1 addition & 1 deletion build/CMakeFiles/echo_server.dir/flags.make
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Generated by "Unix Makefiles" Generator, CMake Version 3.22

# compile CXX with /usr/bin/c++
CXX_DEFINES = -DTIMER_EXPIRATION=0
CXX_DEFINES = -DNOLOG -DTIMER_EXPIRATION=0

CXX_INCLUDES = -I/home/gxj/Next/src/include

Expand Down
2 changes: 1 addition & 1 deletion build/CMakeFiles/next_core.dir/flags.make
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Generated by "Unix Makefiles" Generator, CMake Version 3.22

# compile CXX with /usr/bin/c++
CXX_DEFINES = -DTIMER_EXPIRATION=0
CXX_DEFINES = -DNOLOG -DTIMER_EXPIRATION=0

CXX_INCLUDES = -I/home/gxj/Next/src/include

Expand Down
Binary file modified build/CMakeFiles/next_core.dir/src/core/acceptor.cpp.o
Binary file not shown.
Binary file modified build/CMakeFiles/next_core.dir/src/core/cache.cpp.o
Binary file not shown.
Binary file modified build/CMakeFiles/next_core.dir/src/core/connection.cpp.o
Binary file not shown.
Binary file modified build/CMakeFiles/next_core.dir/src/core/looper.cpp.o
Binary file not shown.
Binary file modified build/CMakeFiles/next_core.dir/src/core/poller.cpp.o
Binary file not shown.
Binary file modified build/CMakeFiles/next_core.dir/src/core/socket.cpp.o
Binary file not shown.
Binary file modified build/CMakeFiles/next_core.dir/src/core/timer.cpp.o
Binary file not shown.
2 changes: 1 addition & 1 deletion build/CMakeFiles/next_log.dir/flags.make
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Generated by "Unix Makefiles" Generator, CMake Version 3.22

# compile CXX with /usr/bin/c++
CXX_DEFINES = -DTIMER_EXPIRATION=0
CXX_DEFINES = -DNOLOG -DTIMER_EXPIRATION=0

CXX_INCLUDES = -I/home/gxj/Next/src/include

Expand Down
Binary file modified lib/libnext_core.a
Binary file not shown.
12 changes: 12 additions & 0 deletions src/core/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ auto GetTimeUtc() noexcept -> uint64_t {
.count();
return mill_since_epoch;
}

Cache::CacheNode::CacheNode() noexcept { UpdataTimeStamp(); }
Cache::CacheNode::CacheNode(std::string identifier,
const std::vector<unsigned char> &data)
Expand Down Expand Up @@ -91,6 +92,7 @@ void Cache::Clear() {
}

void Cache::EvictOne() noexcept {
// lru策略,第一个是最久未使用的。
auto *first_node = header_->next_;
auto resource_size = first_node->Size();
auto iter = mapping_.find(first_node->identifier_); // friend
Expand All @@ -99,6 +101,16 @@ void Cache::EvictOne() noexcept {
mapping_.erase(iter);
occupancy_ -= resource_size;
}

void Cache::EvictOneByUrl(const std::string& url) noexcept {
auto iter = mapping_.find(url); // friend
assert(iter != mapping_.end());
auto resource_size = iter->second->Size();
RemoveFromList(iter->second);
mapping_.erase(iter);
occupancy_ -= resource_size;
}

void Cache::RemoveFromList(const std::shared_ptr<CacheNode> &node) noexcept {
auto *node_ptr = node.get();
auto *node_prev = node_ptr->prev_;
Expand Down
2 changes: 1 addition & 1 deletion src/core/looper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Looper::Looper(uint64_t timer_expiration)
// 通过poller_->Poll获取epoll中就绪的事件对应的connection,然后执行他们的回调conn->GetCallback()();
void Looper::Loop() {
while (!exit_) {
auto ready_connections = poller_->Poll();
auto ready_connections = poller_->Poll(TIMEOUT);
Connection *timer_conn = nullptr;

for (auto &conn : ready_connections) {
Expand Down
3 changes: 2 additions & 1 deletion src/core/poller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ void Poller::AddConnection(Connection *conn) {

auto Poller::Poll(int timeout) -> std::vector<Connection *> {
std::vector<Connection *> events_happen;
// timeout 参数传 -1 意味着无限期等待,直到至少一个监视的文件描述符上发生了一个事件
int ready = epoll_wait(epoll_fd_, epoll_events_, epoll_size_, timeout);
if (ready == -1) {
perror("Poller: Poll() error");
exit(EXIT_FAILURE);
}
for (int i = 0; i < ready; i++) {
Connection * ready_conn = reinterpret_cast<Connection *>(epoll_events_[i].data.ptr);
ready_conn->SetEvents(epoll_events_[i].events);
ready_conn->SetRevents(epoll_events_[i].events);
events_happen.emplace_back(ready_conn);
}
return events_happen;
Expand Down
8 changes: 8 additions & 0 deletions src/core/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ auto NowSinceEpoch() noexcept -> uint64_t {
// 从epoch开始到now有多少ms
return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count());
}
auto NowSinceEpoch2() noexcept -> uint64_t {
auto now = std::chrono::high_resolution_clock::now();
auto duration = now.time_since_epoch();
// 从epoch开始到now有多少ns
return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count());
}

auto FromNow(uint64_t timestamp) noexcept -> uint64_t {
auto now = NowSinceEpoch();
Expand Down Expand Up @@ -70,6 +76,8 @@ Timer::Timer() : timer_fd_(timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CL
}
timer_conn_ = std::make_unique<Connection>(std::make_unique<Socket>(timer_fd_));
timer_conn_->SetEvents(POLL_READ | POLL_ET);
// bind eg:print(int a, int b), auto boundFunc = std::bind(print, std::placeholders::_1, 42);
// boundFunc 是一个新的可调用对象,它接受一个参数作为 print 函数的第一个参数,而第二个参数则是在绑定时已经指定为 42
timer_conn_->SetCallback(std::bind(&Timer::HandleRead, this));
}

Expand Down
3 changes: 1 addition & 2 deletions src/http/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ void PrecessHttpRequest(const std::string &serving_dir,
no_more_parse = true;
} else {
std::string resource_full_path = serving_dir + request.GetResourceUrl();
std::cout << "recv a http request, request path: " << resource_full_path
<< std::endl;
//std::cout << "recv a http request, request path: " << resource_full_path << std::endl;
if (IsCgiRequest(resource_full_path)) {
// dynamic cgi request
Cgier cgier = Cgier::ParseCgier(resource_full_path);
Expand Down
2 changes: 1 addition & 1 deletion src/http/http_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <filesystem>
#include <fstream>
#include <sstream>

#include <iostream>
namespace Next::Http {

Method ToMethod(const std::string &str) {
Expand Down
1 change: 1 addition & 0 deletions src/http/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <algorithm>

namespace Next::Http {

Request::Request(Method method, std::string resource_url, Version version,
const std::vector<Header> &heads) noexcept
: method_(method), resource_url_(std::move(resource_url)),
Expand Down
2 changes: 1 addition & 1 deletion src/include/core/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Cache {
const std::vector<unsigned char> &source) -> bool;

void Clear();

void EvictOneByUrl(const std::string& url) noexcept;
private:
void EvictOne() noexcept;
void RemoveFromList(const std::shared_ptr<CacheNode> &node) noexcept;
Expand Down
1 change: 1 addition & 0 deletions src/include/core/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Next {
class Socket;
class Connection;
auto NowSinceEpoch() noexcept -> uint64_t;
auto NowSinceEpoch2() noexcept -> uint64_t;

auto FromNow(uint64_t timestamp) noexcept -> uint64_t;

Expand Down
Binary file modified src/include/head.tar.gz
Binary file not shown.
60 changes: 58 additions & 2 deletions test/core/cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
#include "core/cache.h"

#include <vector>

#include "http/http_utils.h"
#include "catch2/catch_test_macros.hpp"

#include <string>
#include <iostream>
#include "core/timer.h"
/* for convenience reason */
using Next::Cache;
using Next::Http::LoadFile;
using Next::Http::IsFileExists;
using Next::GetTimeUtc;
using Next::NowSinceEpoch;
using Next::NowSinceEpoch2;


TEST_CASE("[core/cache]") {
const int capacity = 20;
Expand Down Expand Up @@ -45,3 +53,51 @@ TEST_CASE("[core/cache]") {
CHECK(!load_success);
}
}

TEST_CASE("[cache_file_test]"){
const int capacity = 512;
Cache cache(capacity);
int total_file_num = 16;

uint64_t begin = NowSinceEpoch2();
for (int i = 0; i < total_file_num; i++) {
std::string file_path = "testfile" + std::to_string(i);
CHECK(IsFileExists(file_path));
std::vector<unsigned char> buf;
LoadFile(file_path, buf);
bool cache_success = cache.TryInsert(file_path, buf);
//std::cout << "file content: " << buf.data() << std::endl;
CHECK(cache_success);
}
uint64_t end = NowSinceEpoch2();
//std::cout << "begin: "<< begin << " end: " << end << std::endl;
uint64_t spendtime1 = end - begin;
std::cout << "get data from file, read " << total_file_num << " files," << " spend "<< spendtime1 << " ns." << std::endl;

int evict_num = 15;
for (int i = 0; i < evict_num; i++) {
std::string file_path = "testfile" + std::to_string(i);
cache.EvictOneByUrl(file_path);
}

begin = NowSinceEpoch2();
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < total_file_num; i++) {
std::string file_path = "testfile" + std::to_string(i);
std::vector<unsigned char> read_buf;
bool load_success = cache.TryLoad(file_path, read_buf);
if(!load_success) {
cnt2++;
std::vector<unsigned char> buf;
LoadFile(file_path, buf);
}else {
cnt1++;
}
//std::cout << "get file content from memory: " << read_buf.data() << std::endl;
}
end = NowSinceEpoch2();
uint64_t spendtime2 = end - begin;
//std::cout << "begin: "<< begin << " end: " << end << std::endl;
std::cout << "get data from memory, read buf " << cnt1 << ", read file " << cnt2 << ", spend "<< spendtime2 << " ns." << std::endl;
std::cout << "get data from memory, faster than get from file " << (double)spendtime1 / (double)spendtime2 << " times." << std::endl;
}

0 comments on commit 4ce19b2

Please sign in to comment.