VOOZH about

URL: https://www.nuget.org/packages/BugFree.Core/

⇱ NuGet Gallery | BugFree.Core 1.2.2026.614-beta1516




👁 Image
BugFree.Core 1.2.2026.614-beta1516

This is a prerelease version of BugFree.Core.
dotnet add package BugFree.Core --version 1.2.2026.614-beta1516
 
 
NuGet\Install-Package BugFree.Core -Version 1.2.2026.614-beta1516
 
 
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BugFree.Core" Version="1.2.2026.614-beta1516" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BugFree.Core" Version="1.2.2026.614-beta1516" />
 
Directory.Packages.props
<PackageReference Include="BugFree.Core" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BugFree.Core --version 1.2.2026.614-beta1516
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: BugFree.Core, 1.2.2026.614-beta1516"
 
 
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BugFree.Core@1.2.2026.614-beta1516
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BugFree.Core&version=1.2.2026.614-beta1516&prerelease
 
Install as a Cake Addin
#tool nuget:?package=BugFree.Core&version=1.2.2026.614-beta1516&prerelease
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

BugFree.Core

👁 .NET
👁 NuGet

📖 项目介绍

BugFree.Core 是一个功能强大的 .NET 工具类库,旨在为开发人员提供高效、可靠的常用工具和扩展方法。该库包含了编解码、枚举处理、集合操作、IP地址转换、路径安全防护、单位格式化等实用功能,同时还提供了安全随机字符串生成和系统资源压力测试等高级工具。

核心特性

  • 全面覆盖:涵盖开发中常用的各种工具类和扩展方法
  • 跨平台支持:支持 Windows、Linux、macOS 等多种操作系统
  • 高性能:所有实现都经过优化,确保高性能和低内存占用
  • 易用性:提供简洁的API设计,易于集成和使用
  • 安全性:包含路径安全防护、安全随机字符串生成等安全相关功能

🎯 功能特性

扩展方法(Extensions)

类名 说明
AttributeExtensions 通过字段名字符串获取自定义特性(Attribute),支持 TryGet 模式
CodecExtensions 编解码工具集:Hex、Base32、Base58、Base62、Base64(含 Base64Url 模式)、Base91 编码转换,支持 Try 安全解码
EnumerableExtensions 集合扩展:判断空集合、ForEach 遍历、AddRange 批量添加(自动优化 List<T>)
DictionaryExtensions 线程安全的字典 TryGetOrAdd 方法,兼容 ConcurrentDictionary
EnumExtensions 枚举扩展:获取枚举值的 Description 描述信息,支持批量获取
IpConvertExtensions IP 地址工具:IPv4 ↔ UInt32 互转、CIDR 网段解析
PathSecurityHelper 路径安全防护:防止路径穿越攻击、规范化路径片段、路径合法性校验
TcpClientExtensions TCP 连接状态可靠检测,解决 Socket.Connected 误判问题
UnitFormatExtensions 数值单位格式化:存储容量(B/KB/MB/GB...)、传输速率、频率、操作速率、请求速率、中文万/亿/兆等

特性(Attributes)

类名 说明
DisplayAttribute UI 显示元数据特性,支持名称、短名称、分组、说明、提示语和排序

工具类(Tools)

类名 说明
ResourceStressController 系统资源压力控制器,模拟 CPU/内存/磁盘高负载,支持闭环反馈、安全限流、跨平台
SecureRandomStringGenerator 密码学安全随机字符串生成器,支持数字、字母、符号等多种字符集,可排除易混淆字符

🎮 资源压力测试(ResourceStressController)

概述

ResourceStressController 是一个系统资源压力测试工具,用于模拟服务器高负载场景。它可以动态调节CPU、内存、磁盘的使用率,支持闭环反馈控制,确保系统资源使用率稳定在目标值附近。

主要用途:

  • 向客户展示服务器硬件性能
  • 进行压力测试和性能评估
  • 模拟高负载场景下的系统行为
  • 验证系统在资源紧张时的稳定性

架构设计

Tools/ResourceStress/
├── ResourceStressController.cs # 统一入口,编排所有生成器
├── ResourceStressOptions.cs # 配置类型
├── ResourceStressSnapshot.cs # 状态快照
├── CpuStressGenerator.cs # CPU 压力(满转线程 + 小数 PWM)
├── MemoryStressGenerator.cs # 内存压力(字节数组块)
├── DiskStressGenerator.cs # 磁盘压力(文件创建/删除)
└── Platform/
 ├── ISystemMonitor.cs # 跨平台系统监控抽象
 └── SystemMonitorFactory.cs # 工厂 + Win/Linux/macOS 实现

跨平台支持

平台 CPU 监控 内存监控
Windows PerformanceCounter PerformanceCounter + kernel32
Linux /proc/stat 两次采样差分 /proc/meminfo
macOS host_statistics Mach API sysctl + vm_stat

控制算法

三种资源使用同一套控制策略

flowchart LR
 A[系统监控\n实时 CPU/内存/磁盘] --> B[EMA 指数平滑\n消除尖峰噪声]
 B --> C{安全检查\n超上限?}
 C -->|是| D[熔断: 释放全部]
 C -->|否| E[闭环缺口填充\ngap/perUnit × Damping]
 E --> F[调整负载单元\n线程/内存块/文件]
 F --> A
 D --> A

控制策略说明:

  1. EMA指数平滑:使用指数移动平均算法消除系统监控数据的尖峰噪声
  2. 安全检查:当资源使用率超过安全上限时,立即释放所有资源进行熔断保护
  3. 闭环缺口填充:根据目标使用率与当前使用率的差值,动态调整资源负载
  4. 自适应调节:支持滞后恢复,防止在安全边界频繁切换状态

使用示例

using BugFree.Core.Tools.ResourceStress;

// 创建控制器实例
var ctrl = new ResourceStressController(new ResourceStressOptions
{
 Cpu = new() { TargetUsagePercent = 80, SafetyUpperPercent = 95 },
 Memory = new() { TargetUsagePercent = 70, SafetyUpperPercent = 95 },
 Disk = new() { TargetUsagePercent = 60, SafetyUpperPercent = 90 },
});

// 订阅状态变化回调
ctrl.OnStatusChanged = s =>
{
 string direction = s.CurrentPercent < s.TargetPercent ? "加压" : "释压";
 Console.WriteLine($"[{s.ResourceType}] {s.CurrentPercent:F1}% / 目标 {s.TargetPercent}% | {direction}");
};

ctrl.Start(); // 开始施加压力
// ... 保持运行 ...
ctrl.Stop(); // 停止压力测试
ctrl.Dispose(); // 释放资源

配置说明

选项 默认 说明
TargetUsagePercent CPU 80 / Mem 70 / Disk 60 目标占用率,控制器会努力使实际使用率接近此值
SafetyUpperPercent null(不启用) 安全熔断上限,推荐设置为95,超过此值会立即释放资源
SafetyHysteresis 10 滞后恢复值,熔断后需要降到 (上限 - 回差) 才恢复正常调节
MinimumFreeMemoryMb 256 内存最小保留空间,确保系统基本运行
MinimumFreeSpaceMb 1024 磁盘最小保留空间,确保系统基本运行

Demo 控制台

项目附带交互式控制台 Demo,支持5种压力测试模式:

dotnet run --project BugFree.Core.Demo

支持模式:

  • CPU压力测试
  • 内存压力测试
  • 磁盘压力测试
  • 全资源压力测试
  • 自定义参数压力测试

Demo 提供可视化进度条和实时状态显示,方便监控压力测试过程。


🚀 快速开始

安装

通过 NuGet 安装:

dotnet add package BugFree.Core

或者通过包管理器控制台:

Install-Package BugFree.Core
使用示例
// 1. 编解码
using BugFree.Core.Extensions;

byte[] data = "Hello World".GetBytes();
string hex = data.ToHex(); // "48656c6c6f20576f726c64"
string base64 = data.ToBase64(); // "SGVsbG8gV29ybGQ="
string base64Url = data.ToBase64(urlSafe: true); // URL 安全模式
string base62 = data.ToBase62(); // 短 ID 友好编码
if (base62.TryToBase62(out var decoded)) { } // Try 安全解码

// 2. 枚举描述
enum Status { [Description("正常")] Active, [Description("已禁用")] Disabled }
string desc = Status.Active.GetDescription(); // "正常"

// 3. 单位格式化
long fileSize = 2_147_483_648;
string formatted = fileSize.UnitFormatSize(UnitFormatExtensions.UnitKind.Storage);
// 输出: "2.00 GB"

// 4. 路径安全
string safePath = PathSecurityHelper.NormalizePart("user/avatar.png");
// 拒绝 "../etc/passwd" 等路径穿越输入

// 5. 随机字符串
var options = new RandomStringOptions
{
 Length = 8,
 Kind = RandomStringKind.Alphanumeric,
 ExcludeConfusingCharacters = true
};
string code = SecureRandomStringGenerator.Generate(options);

// 6. IP 地址转换
if ("192.168.1.1".TryToIpUInt32(out uint ipValue)) { }
var (network, mask) = "10.0.0.0/24".ToCidrNetwork();

// 7. 集合操作
new[] { 1, 2, 3 }.ForEach(x => Console.WriteLine(x));
list.AddRange(otherItems);

// 8. 特性获取
typeof(MyEnum).GetFieldAttribute<DescriptionAttribute>("FieldName");
📦 项目结构
BugFree.Core/
├── Attributes/ # 自定义特性
│ └── DisplayAttribute.cs
├── Extensions/ # 扩展方法
│ ├── AttributeExtensions.cs # 字段特性扩展
│ ├── CodecExtensions.cs # 编解码扩展
│ ├── CollectionExtensions.cs # 集合扩展
│ ├── DictionaryExtensions.cs # 字典扩展
│ ├── EnumExtensions.cs # 枚举扩展
│ ├── IpConvertExtensions.cs # IP 地址转换
│ ├── PathSecurityHelper.cs # 路径安全防护
│ ├── TcpClientExtensions.cs # TCP 连接检测
│ └── UnitFormatExtensions.cs # 单位格式化
└── Tools/ # 工具类
 ├── ResourceStressController.cs # 资源压力测试
 └── SecureRandomStringGenerator.cs # 安全随机字符串
🛠️ 技术要求
  • .NET 8.0 / .NET 10.0
  • C# 最新语言版本
  • 支持 Windows / Linux 跨平台
🤝 参与贡献
  1. Fork 本仓库
  2. 新建特性分支 (git checkout -b feature/xxx)
  3. 提交代码 (git commit -m '添加xxx功能')
  4. 推送到分支 (git push origin feature/xxx)
  5. 新建 Pull Request
📄 开源协议

本项目基于 MIT 协议开源,详见 文件。

📮 联系我们
Product Versions Compatible and additional computed target framework versions.
.NET net8.0 net8.0 is compatible.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 is compatible.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on BugFree.Core:

Package Downloads
BugFree.Security

安全加密框架,集成 BCrypt/Argon2/Scrypt/BouncyCastle 等现代密码学算法,提供哈希、签名、加解密及随机数生成服务。

BugFree.Controllers.Api

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

跨平台基础工具库;IP/时间/编码/类型转换等扩展方法;数据同步基础设施;节假日计算工具。