From b1a9a8a17fa028f6d2ee13582cce70671f0bf0d4 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Sat, 24 Feb 2024 10:31:28 +0800 Subject: [PATCH] feat fix mutux --- .gitignore | 4 ++ CMakeLists.txt | 7 ++++ benchmark/strings/base64_benchmark.cc | 59 +++++++++++++++++++++++++++ include/sled/byte_order.h | 25 ++++++++++++ include/sled/ref_count.h | 10 ++--- include/sled/synchronization/mutex.h | 13 +++--- include/sled/units/timestamp.h | 5 ++- src/include/sled/ubench.h | 29 +++++++++++++ 8 files changed, 141 insertions(+), 11 deletions(-) create mode 100644 .gitignore create mode 100644 benchmark/strings/base64_benchmark.cc create mode 100644 src/include/sled/ubench.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c7e98b2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.cache/ +out/ +build/ +compile_commands.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 1213198..e6d0776 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,3 +50,10 @@ target_include_directories( PRIVATE src) target_link_libraries(sled PUBLIC rpc_core fmt) + +find_package(benchmark REQUIRED) + +add_executable(sled_benchmark +benchmark/strings/base64_benchmark.cc +) +target_link_libraries(sled_benchmark PRIVATE sled benchmark::benchmark benchmark::benchmark_main) diff --git a/benchmark/strings/base64_benchmark.cc b/benchmark/strings/base64_benchmark.cc new file mode 100644 index 0000000..c9e2740 --- /dev/null +++ b/benchmark/strings/base64_benchmark.cc @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +struct strings {}; + +static std::string +AllocRandomString(sled::Random &random, int len) +{ + std::stringstream ss; + for (int i = len; i > 0; i--) { ss << random.Rand(); } + return ss.str(); +} + +void +BenchmarkBase64Encode(benchmark::State &state) +{ + state.PauseTiming(); + sled::Random random(2393); + std::vector test_data; + for (int i = 0; i < state.range(0); i++) { + test_data.emplace_back(AllocRandomString(random, state.range(1))); + } + + state.ResumeTiming(); + for (int i = 0; i < state.range(2); i++) { + for (const auto &str : test_data) { + auto base64 = sled::Base64::Encode(str); + } + } +} + +std::string +uint2str(unsigned int num) +{ + std::ostringstream oss; + oss << num; + return oss.str(); +} + +void +test(benchmark::State &state) +{ + for (int i = 0; i < 1000000; i++) (void) uint2str(i); + state.end(); +} + +BENCHMARK(test); +/* +BENCHMARK(BenchmarkBase64Encode) + ->ArgsProduct({ + // generate the num of strings + benchmark::CreateRange(10, 1000, 10), + // generate the length of each string + benchmark::CreateRange(10, 1000, 10), + benchmark::CreateRange(10, 1000, 10), + }); +*/ diff --git a/include/sled/byte_order.h b/include/sled/byte_order.h index cabf431..c8b4299 100644 --- a/include/sled/byte_order.h +++ b/include/sled/byte_order.h @@ -1,5 +1,30 @@ #include +#if defined(__APPLE__) + +#include + +#define htobe16(x) OSSwapHostToBigInt16(x) +#define htole16(x) OSSwapHostToLittleInt16(x) +#define be16toh(x) OSSwapBigToHostInt16(x) +#define le16toh(x) OSSwapLittleToHostInt16(x) + +#define htobe32(x) OSSwapHostToBigInt32(x) +#define htole32(x) OSSwapHostToLittleInt32(x) +#define be32toh(x) OSSwapBigToHostInt32(x) +#define le32toh(x) OSSwapLittleToHostInt32(x) + +#define htobe64(x) OSSwapHostToBigInt64(x) +#define htole64(x) OSSwapHostToLittleInt64(x) +#define be64toh(x) OSSwapBigToHostInt64(x) +#define le64toh(x) OSSwapLittleToHostInt64(x) + +#define __BYTE_ORDER BYTE_ORDER +#define __BIG_ENDIAN BIG_ENDIAN +#define __LITTLE_ENDIAN LITTLE_ENDIAN +#define __PDP_ENDIAN PDP_ENDIAN +#endif + inline uint16_t HostToNetwork16(uint16_t n) { diff --git a/include/sled/ref_count.h b/include/sled/ref_count.h index 7f97ad4..4a49f0a 100644 --- a/include/sled/ref_count.h +++ b/include/sled/ref_count.h @@ -13,13 +13,13 @@ enum class RefCountReleaseStatus { kDroppedLastRef, kOtherRefsRemained }; class RefCountInterface { public: - virtual void AddRef() const = 0; - virtual RefCountReleaseStatus Release() const = 0; + virtual void AddRef() const = 0; + virtual RefCountReleaseStatus Release() const = 0; protected: - virtual ~RefCountInterface() = default; + virtual ~RefCountInterface() = default; }; -} // namespace sled +}// namespace sled -#endif // REF_COUNT_H +#endif// REF_COUNT_H diff --git a/include/sled/synchronization/mutex.h b/include/sled/synchronization/mutex.h index eeeb1ee..23959da 100644 --- a/include/sled/synchronization/mutex.h +++ b/include/sled/synchronization/mutex.h @@ -17,13 +17,15 @@ namespace sled { namespace internal { template struct HasLockAndUnlock { - template().Lock()) * = nullptr, - decltype(std::declval().Unlock()) * = nullptr> - static int Test(T); + template().Lock()) * = nullptr, + decltype(std::declval().Unlock()) * = nullptr> + static int Test(int); + + template static char Test(...); - static constexpr bool value = + static constexpr bool value = std::is_same(0)), int>::value; }; }// namespace internal @@ -69,6 +71,7 @@ template::value, TLock>::type * = nullptr> class LockGuard final { +public: LockGuard(const LockGuard &) = delete; LockGuard &operator=(const LockGuard &) = delete; diff --git a/include/sled/units/timestamp.h b/include/sled/units/timestamp.h index 027be43..fba4906 100644 --- a/include/sled/units/timestamp.h +++ b/include/sled/units/timestamp.h @@ -77,7 +77,10 @@ public: return ToFractionOr<1000>(fallback_value); } - constexpr int64_t us_or(int64_t fallback_value) const { return ToValueOr(fallback_value); } + constexpr int64_t us_or(int64_t fallback_value) const + { + return ToValueOr(fallback_value); + } Timestamp operator+(const TimeDelta delta) const { diff --git a/src/include/sled/ubench.h b/src/include/sled/ubench.h new file mode 100644 index 0000000..17c211a --- /dev/null +++ b/src/include/sled/ubench.h @@ -0,0 +1,29 @@ +/** + * @file : {{FILE}} + * @created : {{TIMESTAMP}} + * @license : {{LICENSE}} + **/ + +#ifndef{{MACRO_GUARD } } +#define{{MACRO_GUARD } } + +namespace { +{ + CURSOR +} +}// namespace + +{ + + class { + { + CAMEL_CLASS + } + } { + public: + + }; + +}// namespace + +#endif// {{MACRO_GUARD}}