geopro/tools/gpr_poc/Probe.hpp

61 lines
1.6 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef GEOPRO_TOOLS_GPR_POC_PROBE_HPP
#define GEOPRO_TOOLS_GPR_POC_PROBE_HPP
// 轻量 headless 度量探针:墙钟计时 + 进程峰值工作集内存。
// 仅供 gpr_poc CLI 使用header-only零外部依赖Windows 用 Psapi
#include <chrono>
#include <string>
#if defined(_WIN32)
// 防止 <windows.h> 的 min/max 宏污染 std::numeric_limits<>::min()/max()
// ScalarVolumeI16.hpp 等会因此编译失败)。
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <psapi.h>
#endif
namespace geopro::tools {
// 墙钟计时器构造即起表elapsedMs() 返回毫秒double
class Stopwatch {
public:
Stopwatch() : start_(std::chrono::steady_clock::now()) {}
void reset() { start_ = std::chrono::steady_clock::now(); }
double elapsedMs() const {
const auto now = std::chrono::steady_clock::now();
return std::chrono::duration<double, std::milli>(now - start_).count();
}
private:
std::chrono::steady_clock::time_point start_;
};
// 进程级度量探针。
struct Probe {
// 进程峰值工作集MB。Windows 取 GetProcessMemoryInfo::PeakWorkingSetSize
// 其它平台返回 0本任务仅 Windows 实测)。
static double peakMemMB() {
#if defined(_WIN32)
PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
return static_cast<double>(pmc.PeakWorkingSetSize) / (1024.0 * 1024.0);
}
return 0.0;
#else
return 0.0;
#endif
}
};
} // namespace geopro::tools
#endif // GEOPRO_TOOLS_GPR_POC_PROBE_HPP