常规更新

This commit is contained in:
2026-07-24 10:54:45 +08:00
parent 0f4623cbef
commit e8bcc7ecfd
117 changed files with 12097 additions and 14121 deletions
+108 -136
View File
@@ -1,163 +1,135 @@
#pragma once
#include <string_view>
#include "global.h"
template<typename Mutex_Type = std::mutex>
template <typename Mutex_Type = std::mutex>
class Frequency_Limit_T {
public:
explicit Frequency_Limit_T(double times_per_second = 1.0)
: interval(1.0 / times_per_second),
last(std::chrono::steady_clock::now()) {
}
bool test() {
using namespace std::chrono;
std::lock_guard<std::mutex> lock(mtx);
auto now = steady_clock::now();
double dt = duration_cast<microseconds>(now - last).count() * 1e-6;
if (dt >= interval) {
last = now;
return true;
explicit Frequency_Limit_T(double times_per_second = 1.0) : interval(1.0 / times_per_second),
last(std::chrono::steady_clock::now()) {
}
bool test() {
using namespace std::chrono;
std::lock_guard<std::mutex> lock(mtx);
auto now = steady_clock::now();
double dt = duration_cast<microseconds>(now - last).count() * 1e-6;
if (dt >= interval) {
last = now;
return true;
}
return false;
}
return false;
}
private:
double interval;
std::chrono::steady_clock::time_point last;
Mutex_Type mtx;
double interval;
std::chrono::steady_clock::time_point last;
Mutex_Type mtx;
};
using Frequency_Limit_ST = Frequency_Limit_T<Psc::Empty_Lock>;
using Frequency_Limit = Frequency_Limit_T<std::mutex>;
template<typename State_Type = std::monostate, typename Mutex_Type = std::mutex>
template <typename State_Type = std::monostate, typename Mutex_Type = std::mutex>
class Frequency_Limit_Multi_T {
public:
using Clock = std::chrono::steady_clock;
struct Type_Info {
double interval{};
Clock::time_point last{};
[[no_unique_address]] State_Type state{};
};
explicit Frequency_Limit_Multi_T(double default_times_per_second = 1.0)
: default_interval_(1.0 / default_times_per_second) {
}
void set_rate(std::string_view type, double times_per_second) {
std::lock_guard<Mutex_Type> lock(mtx_);
map_[std::string(type)].interval = 1.0 / times_per_second;
}
bool test(std::string_view type) {
return test_ex(type);
}
State_Type *test_ex(std::string_view type) {
const auto now = Clock::now();
std::lock_guard<Mutex_Type> lock(mtx_);
auto &info = map_[std::string(type)];
if (info.interval == 0.0) {
info.interval = default_interval_;
info.last = now;
return &info.state;
using Clock = std::chrono::steady_clock;
struct Type_Info {
double interval{};
Clock::time_point last{};
[[no_unique_address]] State_Type state{};
};
explicit Frequency_Limit_Multi_T(double default_times_per_second = 1.0) : default_interval_(1.0 / default_times_per_second) {
}
const double dt = std::chrono::duration<double>(now - info.last).count();
if (dt < info.interval) {
return nullptr;
void set_rate(std::string_view type, double times_per_second) {
std::lock_guard<Mutex_Type> lock(mtx_);
map_[std::string(type)].interval = 1.0 / times_per_second;
}
bool test(std::string_view type) {
return test_ex(type);
}
State_Type* test_ex(std::string_view type) {
const auto now = Clock::now();
std::lock_guard<Mutex_Type> lock(mtx_);
auto& info = map_[std::string(type)];
if (info.interval == 0.0) {
info.interval = default_interval_;
info.last = now;
return &info.state;
}
const double dt = std::chrono::duration<double>(now - info.last).count();
if (dt < info.interval) {
return nullptr;
}
info.last = now;
return &info.state;
}
State_Type* get_state(std::string_view type) {
std::lock_guard<Mutex_Type> lock(mtx_);
auto& info = map_[std::string(type)];
return &info.state;
}
info.last = now;
return &info.state;
}
State_Type *get_state(std::string_view type) {
std::lock_guard<Mutex_Type> lock(mtx_);
auto &info = map_[std::string(type)];
return &info.state;
}
private:
double default_interval_;
std::map<std::string, Type_Info> map_;
Mutex_Type mtx_;
double default_interval_;
std::map<std::string, Type_Info> map_;
Mutex_Type mtx_;
};
using Frequency_Limit_Multi_ST = Frequency_Limit_Multi_T<Psc::Empty_Lock>;
using Frequency_Limit_Multi = Frequency_Limit_Multi_T<std::mutex>;
template<typename Value_Type = std::size_t, typename State_Type = std::monostate, typename Mutex_Type = std::mutex>
template <typename Value_Type = std::size_t, typename State_Type = std::monostate, typename Mutex_Type = std::mutex>
class Value_Growth_Multi_T {
public:
struct Type_Info {
std::size_t required_increase_count{};
Value_Type last_value{};
std::size_t increase_count{};
bool initialized{};
[[no_unique_address]] State_Type state{};
};
explicit Value_Growth_Multi_T(std::size_t default_required_increase_count = 5)
: default_required_increase_count_(default_required_increase_count) {
}
void set_count(std::string_view type, std::size_t required_increase_count) {
std::lock_guard<Mutex_Type> lock(mtx_);
auto &info = map_[std::string(type)];
info.required_increase_count = required_increase_count;
info.initialized = false;
info.increase_count = 0;
}
bool test(std::string_view type, Value_Type value) {
return test_ex(type, value);
}
State_Type *test_ex(std::string_view type, Value_Type value) {
std::lock_guard<Mutex_Type> lock(mtx_);
auto &info = map_[std::string(type)];
if (info.required_increase_count == 0) {
info.required_increase_count = default_required_increase_count_;
struct Type_Info {
std::size_t required_increase_count{};
Value_Type last_value{};
std::size_t increase_count{};
bool initialized{};
[[no_unique_address]] State_Type state{};
};
explicit Value_Growth_Multi_T(std::size_t default_required_increase_count = 5) : default_required_increase_count_(default_required_increase_count) {
}
if (!info.initialized) {
info.last_value = value;
info.initialized = true;
return nullptr;
void set_count(std::string_view type, std::size_t required_increase_count) {
std::lock_guard<Mutex_Type> lock(mtx_);
auto& info = map_[std::string(type)];
info.required_increase_count = required_increase_count;
info.initialized = false;
info.increase_count = 0;
}
if (value <= info.last_value) {
info.last_value = value;
info.increase_count = 0;
return nullptr;
bool test(std::string_view type, Value_Type value) {
return test_ex(type, value);
}
info.last_value = value;
if (info.increase_count < info.required_increase_count) {
++info.increase_count;
State_Type* test_ex(std::string_view type, Value_Type value) {
std::lock_guard<Mutex_Type> lock(mtx_);
auto& info = map_[std::string(type)];
if (info.required_increase_count == 0) {
info.required_increase_count = default_required_increase_count_;
}
if (!info.initialized) {
info.last_value = value;
info.initialized = true;
return nullptr;
}
if (value <= info.last_value) {
info.last_value = value;
info.increase_count = 0;
return nullptr;
}
info.last_value = value;
if (info.increase_count < info.required_increase_count) {
++info.increase_count;
}
if (info.increase_count < info.required_increase_count) {
return nullptr;
}
return &info.state;
}
if (info.increase_count < info.required_increase_count) {
return nullptr;
State_Type* get_state(std::string_view type) {
std::lock_guard<Mutex_Type> lock(mtx_);
return &map_[std::string(type)].state;
}
void reset(std::string_view type) {
std::lock_guard<Mutex_Type> lock(mtx_);
auto& info = map_[std::string(type)];
info.initialized = false;
info.increase_count = 0;
}
return &info.state;
}
State_Type *get_state(std::string_view type) {
std::lock_guard<Mutex_Type> lock(mtx_);
return &map_[std::string(type)].state;
}
void reset(std::string_view type) {
std::lock_guard<Mutex_Type> lock(mtx_);
auto &info = map_[std::string(type)];
info.initialized = false;
info.increase_count = 0;
}
private:
std::size_t default_required_increase_count_;
std::map<std::string, Type_Info> map_;
Mutex_Type mtx_;
std::size_t default_required_increase_count_;
std::map<std::string, Type_Info> map_;
Mutex_Type mtx_;
};
+14 -13
View File
@@ -1,9 +1,9 @@
#pragma once
#include "../Base/global_include.h"
#include "global.h"
#include <algorithm>
#include <array>
#include <cmath>
#include "../Base/global_include.h"
#include "global.h"
namespace Psc {
class Base_Statistics {
protected:
@@ -13,7 +13,8 @@ protected:
return static_cast<uint64_t>(duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count());
}
public:
Base_Statistics() : start_time(get_current_ms()) {}
Base_Statistics() : start_time(get_current_ms()) {
}
[[nodiscard]] virtual JSON to_json() const = 0;
virtual ~Base_Statistics() = default;
[[nodiscard]] unsigned int get_start_time() const {
@@ -50,8 +51,7 @@ public:
JSON ret = JSON::object();
Ret_J(instant_speed)
Ret_J(average_speed)
Ret_J(times)
return ret;
Ret_J(times) return ret;
}
[[nodiscard]] std::string to_string() const {
return VAR_STR_4(instant_speed, average_speed, cur_total, times);
@@ -119,8 +119,7 @@ public:
Ret_J(variation)
Ret_J(instant)
Ret_J(p95)
Ret_J(times)
return ret;
Ret_J(times) return ret;
}
[[nodiscard]] std::string to_string() const {
return VAR_STR_8(min, max, average, smooth, variation, instant, p95, times);
@@ -137,7 +136,8 @@ public:
smooth = value;
variation = 0.0;
init = true;
} else {
}
else {
double deviation = std::abs(value - smooth);
max = std::max(max, value);
min = std::min(min, value);
@@ -152,11 +152,13 @@ private:
void update_recent(double value) {
recent_values[recent_index] = value;
recent_index = (recent_index + 1) % recent_value_count;
if(recent_count < recent_value_count) ++recent_count;
if (recent_count < recent_value_count)
++recent_count;
std::array<double, recent_value_count> sorted = recent_values;
std::sort(sorted.begin(), sorted.begin() + static_cast<std::ptrdiff_t>(recent_count));
auto index = static_cast<std::size_t>(std::ceil(static_cast<double>(recent_count) * 0.95));
if(index > 0) --index;
if (index > 0)
--index;
p95 = sorted[index];
}
};
@@ -183,8 +185,7 @@ public:
Ret_J(average)
Ret_J(count)
Ret_J(total)
Ret_J(total_max)
return ret;
Ret_J(total_max) return ret;
}
void update(bool value) {
value ? update(1) : update(-1);
@@ -215,4 +216,4 @@ public:
total = 0;
}
};
}
} // namespace Psc
+1 -2
View File
@@ -1,4 +1,3 @@
#pragma once
#include "Frequency_Limit.h"
#include "Statistics.h"
#include "Frequency_Limit.h"
+3 -4
View File
@@ -1,8 +1,4 @@
#pragma once
#include "../Base/JSON.h"
#include "../Base/global_include.h"
#include "Core/spdlog/export.h"
#include <algorithm>
#include <cmath>
#include <ctime>
@@ -10,3 +6,6 @@
#include <limits>
#include <map>
#include <string>
#include "../Base/JSON.h"
#include "../Base/global_include.h"
#include "Core/spdlog/export.h"