feat fix mutux

This commit is contained in:
tqcq 2024-02-24 10:31:28 +08:00
parent 46976d53f9
commit b1a9a8a17f
8 changed files with 141 additions and 11 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.cache/
out/
build/
compile_commands.json

View File

@ -50,3 +50,10 @@ target_include_directories(
PRIVATE src) PRIVATE src)
target_link_libraries(sled PUBLIC rpc_core fmt) 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)

View File

@ -0,0 +1,59 @@
#include <benchmark/benchmark.h>
#include <sled/random.h>
#include <sled/strings/base64.h>
#include <sstream>
struct strings {};
static std::string
AllocRandomString(sled::Random &random, int len)
{
std::stringstream ss;
for (int i = len; i > 0; i--) { ss << random.Rand<char>(); }
return ss.str();
}
void
BenchmarkBase64Encode(benchmark::State &state)
{
state.PauseTiming();
sled::Random random(2393);
std::vector<std::string> 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),
});
*/

View File

@ -1,5 +1,30 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#if defined(__APPLE__)
#include <libkern/OSByteOrder.h>
#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 inline uint16_t
HostToNetwork16(uint16_t n) HostToNetwork16(uint16_t n)
{ {

View File

@ -13,13 +13,13 @@ enum class RefCountReleaseStatus { kDroppedLastRef, kOtherRefsRemained };
class RefCountInterface { class RefCountInterface {
public: public:
virtual void AddRef() const = 0; virtual void AddRef() const = 0;
virtual RefCountReleaseStatus Release() const = 0; virtual RefCountReleaseStatus Release() const = 0;
protected: protected:
virtual ~RefCountInterface() = default; virtual ~RefCountInterface() = default;
}; };
} // namespace sled }// namespace sled
#endif // REF_COUNT_H #endif// REF_COUNT_H

View File

@ -17,13 +17,15 @@ namespace sled {
namespace internal { namespace internal {
template<typename T> template<typename T>
struct HasLockAndUnlock { struct HasLockAndUnlock {
template<typename TClass, template<typename U,
decltype(std::declval<TClass>().Lock()) * = nullptr, decltype(std::declval<U>().Lock()) * = nullptr,
decltype(std::declval<TClass>().Unlock()) * = nullptr> decltype(std::declval<U>().Unlock()) * = nullptr>
static int Test(T); static int Test(int);
template<typename>
static char Test(...); static char Test(...);
static constexpr bool value = static constexpr bool value =
std::is_same<decltype(Test<T>(0)), int>::value; std::is_same<decltype(Test<T>(0)), int>::value;
}; };
}// namespace internal }// namespace internal
@ -69,6 +71,7 @@ template<typename TLock,
typename std::enable_if<internal::HasLockAndUnlock<TLock>::value, typename std::enable_if<internal::HasLockAndUnlock<TLock>::value,
TLock>::type * = nullptr> TLock>::type * = nullptr>
class LockGuard final { class LockGuard final {
public:
LockGuard(const LockGuard &) = delete; LockGuard(const LockGuard &) = delete;
LockGuard &operator=(const LockGuard &) = delete; LockGuard &operator=(const LockGuard &) = delete;

View File

@ -77,7 +77,10 @@ public:
return ToFractionOr<1000>(fallback_value); 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 Timestamp operator+(const TimeDelta delta) const
{ {

29
src/include/sled/ubench.h Normal file
View File

@ -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}}