⚡ Optimize crypto performance and memory management
This commit is contained in:
336
README_CN.md
336
README_CN.md
@@ -26,6 +26,7 @@ go-xcipher 是一个高性能、易用的 Go 加密库,基于 ChaCha20-Poly130
|
||||
- 🧠 智能内存管理,减少内存分配和 GC 压力
|
||||
- ⏹️ 支持可取消的操作,适合长时间运行的任务
|
||||
- 🛡️ 全面的错误处理和安全检查
|
||||
- 🖥️ CPU架构感知优化,针对不同硬件平台自动调整参数
|
||||
|
||||
## 🔧 安装
|
||||
|
||||
@@ -77,7 +78,7 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### 流式加密
|
||||
### 流式加密(基本用法)
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -104,11 +105,49 @@ func main() {
|
||||
outputFile, _ := os.Create("大文件.encrypted")
|
||||
defer outputFile.Close()
|
||||
|
||||
// 设置流选项
|
||||
// 使用默认选项加密流
|
||||
err := cipher.EncryptStream(inputFile, outputFile, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("文件加密完成")
|
||||
}
|
||||
```
|
||||
|
||||
### 并行处理大文件
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"github.com/landaiqing/go-xcipher"
|
||||
"golang.org/x/crypto/chacha20poly1305"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建密钥
|
||||
key := make([]byte, chacha20poly1305.KeySize)
|
||||
|
||||
// 初始化加密器
|
||||
cipher := xcipher.NewXCipher(key)
|
||||
|
||||
// 打开要加密的文件
|
||||
inputFile, _ := os.Open("大文件.dat")
|
||||
defer inputFile.Close()
|
||||
|
||||
// 创建输出文件
|
||||
outputFile, _ := os.Create("大文件.encrypted")
|
||||
defer outputFile.Close()
|
||||
|
||||
// 设置流选项 - 启用并行处理
|
||||
options := xcipher.DefaultStreamOptions()
|
||||
options.UseParallel = true // 启用并行处理
|
||||
options.BufferSize = 64 * 1024 // 设置缓冲区大小
|
||||
options.CollectStats = true // 收集性能统计
|
||||
options.UseParallel = true // 启用并行处理
|
||||
options.MaxWorkers = 8 // 设置最大工作线程数
|
||||
options.BufferSize = 256 * 1024 // 设置较大的缓冲区大小
|
||||
options.CollectStats = true // 收集性能统计
|
||||
|
||||
// 加密流
|
||||
stats, err := cipher.EncryptStreamWithOptions(inputFile, outputFile, options)
|
||||
@@ -119,6 +158,167 @@ func main() {
|
||||
// 显示性能统计
|
||||
fmt.Printf("处理用时: %v\n", stats.Duration())
|
||||
fmt.Printf("处理速度: %.2f MB/s\n", stats.Throughput)
|
||||
fmt.Printf("并行处理: %v, 工作线程数: %d\n", stats.ParallelProcessing, stats.WorkerCount)
|
||||
fmt.Printf("处理数据量: %.2f MB\n", float64(stats.BytesProcessed) / 1024 / 1024)
|
||||
fmt.Printf("数据块数: %d, 平均块大小: %.2f KB\n", stats.BlocksProcessed, stats.AvgBlockSize / 1024)
|
||||
}
|
||||
```
|
||||
|
||||
### 使用自适应参数优化
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"github.com/landaiqing/go-xcipher"
|
||||
"golang.org/x/crypto/chacha20poly1305"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建密钥
|
||||
key := make([]byte, chacha20poly1305.KeySize)
|
||||
|
||||
// 初始化加密器
|
||||
cipher := xcipher.NewXCipher(key)
|
||||
|
||||
// 打开要加密的文件
|
||||
inputFile, _ := os.Open("大文件.dat")
|
||||
defer inputFile.Close()
|
||||
|
||||
// 创建输出文件
|
||||
outputFile, _ := os.Create("大文件.encrypted")
|
||||
defer outputFile.Close()
|
||||
|
||||
// 获取优化的流选项 - 自动根据系统环境选择最佳参数
|
||||
options := xcipher.GetOptimizedStreamOptions()
|
||||
options.CollectStats = true
|
||||
|
||||
// 查看系统优化信息
|
||||
sysInfo := xcipher.GetSystemOptimizationInfo()
|
||||
fmt.Printf("CPU架构: %s, 核心数: %d\n", sysInfo.Architecture, sysInfo.NumCPUs)
|
||||
fmt.Printf("支持AVX: %v, 支持AVX2: %v\n", sysInfo.HasAVX, sysInfo.HasAVX2)
|
||||
fmt.Printf("推荐缓冲区大小: %d KB\n", sysInfo.RecommendedBufferSize / 1024)
|
||||
fmt.Printf("推荐工作线程数: %d\n", sysInfo.RecommendedWorkers)
|
||||
|
||||
// 加密流
|
||||
stats, err := cipher.EncryptStreamWithOptions(inputFile, outputFile, options)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 显示性能统计
|
||||
fmt.Printf("处理用时: %v\n", stats.Duration())
|
||||
fmt.Printf("处理速度: %.2f MB/s\n", stats.Throughput)
|
||||
}
|
||||
```
|
||||
|
||||
### 支持取消的长时间操作
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
"github.com/landaiqing/go-xcipher"
|
||||
"golang.org/x/crypto/chacha20poly1305"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建密钥
|
||||
key := make([]byte, chacha20poly1305.KeySize)
|
||||
|
||||
// 初始化加密器
|
||||
cipher := xcipher.NewXCipher(key)
|
||||
|
||||
// 打开要加密的文件
|
||||
inputFile, _ := os.Open("超大文件.dat")
|
||||
defer inputFile.Close()
|
||||
|
||||
// 创建输出文件
|
||||
outputFile, _ := os.Create("超大文件.encrypted")
|
||||
defer outputFile.Close()
|
||||
|
||||
// 创建可取消的上下文
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30 * time.Second)
|
||||
defer cancel() // 确保资源被释放
|
||||
|
||||
// 设置带取消功能的流选项
|
||||
options := xcipher.DefaultStreamOptions()
|
||||
options.UseParallel = true
|
||||
options.CancelChan = ctx.Done() // 设置取消信号
|
||||
|
||||
// 在另一个goroutine中执行加密
|
||||
resultChan := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := cipher.EncryptStreamWithOptions(inputFile, outputFile, options)
|
||||
resultChan <- err
|
||||
}()
|
||||
|
||||
// 等待结果或超时
|
||||
select {
|
||||
case err := <-resultChan:
|
||||
if err != nil {
|
||||
fmt.Printf("加密错误: %v\n", err)
|
||||
} else {
|
||||
fmt.Println("加密成功完成")
|
||||
}
|
||||
case <-ctx.Done():
|
||||
fmt.Println("操作超时或被取消")
|
||||
// 等待操作确实停止
|
||||
err := <-resultChan
|
||||
fmt.Printf("取消后的结果: %v\n", err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 内存缓冲区处理示例
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"github.com/landaiqing/go-xcipher"
|
||||
"golang.org/x/crypto/chacha20poly1305"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建密钥
|
||||
key := make([]byte, chacha20poly1305.KeySize)
|
||||
|
||||
// 初始化加密器
|
||||
cipher := xcipher.NewXCipher(key)
|
||||
|
||||
// 准备要加密的数据
|
||||
data := []byte("这是一些要加密的敏感数据,使用内存缓冲区而不是文件进行处理")
|
||||
|
||||
// 创建源读取器和目标写入器
|
||||
source := bytes.NewReader(data)
|
||||
var encrypted bytes.Buffer
|
||||
|
||||
// 加密数据
|
||||
if err := cipher.EncryptStream(source, &encrypted, nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("原始数据大小: %d 字节\n", len(data))
|
||||
fmt.Printf("加密后大小: %d 字节\n", encrypted.Len())
|
||||
|
||||
// 解密数据
|
||||
var decrypted bytes.Buffer
|
||||
if err := cipher.DecryptStream(bytes.NewReader(encrypted.Bytes()), &decrypted, nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("解密后大小: %d 字节\n", decrypted.Len())
|
||||
fmt.Printf("解密后内容: %s\n", decrypted.String())
|
||||
}
|
||||
```
|
||||
|
||||
@@ -133,25 +333,43 @@ type XCipher struct {
|
||||
|
||||
// 流处理的统计信息
|
||||
type StreamStats struct {
|
||||
StartTime time.Time
|
||||
EndTime time.Time
|
||||
BytesProcessed int64
|
||||
BlocksProcessed int
|
||||
AvgBlockSize float64
|
||||
Throughput float64
|
||||
ParallelProcessing bool
|
||||
WorkerCount int
|
||||
BufferSize int
|
||||
StartTime time.Time // 开始时间
|
||||
EndTime time.Time // 结束时间
|
||||
BytesProcessed int64 // 处理的字节数
|
||||
BlocksProcessed int // 处理的数据块数
|
||||
AvgBlockSize float64 // 平均块大小
|
||||
Throughput float64 // 吞吐量 (MB/s)
|
||||
ParallelProcessing bool // 是否使用了并行处理
|
||||
WorkerCount int // 工作线程数
|
||||
BufferSize int // 缓冲区大小
|
||||
}
|
||||
|
||||
// 流处理选项
|
||||
type StreamOptions struct {
|
||||
BufferSize int
|
||||
UseParallel bool
|
||||
MaxWorkers int
|
||||
AdditionalData []byte
|
||||
CollectStats bool
|
||||
CancelChan <-chan struct{}
|
||||
BufferSize int // 缓冲区大小
|
||||
UseParallel bool // 是否使用并行处理
|
||||
MaxWorkers int // 最大工作线程数
|
||||
AdditionalData []byte // 附加验证数据
|
||||
CollectStats bool // 是否收集性能统计
|
||||
CancelChan <-chan struct{} // 取消信号通道
|
||||
}
|
||||
|
||||
// 系统优化信息
|
||||
type OptimizationInfo struct {
|
||||
Architecture string // CPU架构
|
||||
NumCPUs int // CPU核心数
|
||||
HasAVX bool // 是否支持AVX指令集
|
||||
HasAVX2 bool // 是否支持AVX2指令集
|
||||
HasSSE41 bool // 是否支持SSE4.1指令集
|
||||
HasNEON bool // 是否支持ARM NEON指令集
|
||||
EstimatedL1Cache int // 估计L1缓存大小
|
||||
EstimatedL2Cache int // 估计L2缓存大小
|
||||
EstimatedL3Cache int // 估计L3缓存大小
|
||||
RecommendedBufferSize int // 推荐的缓冲区大小
|
||||
RecommendedWorkers int // 推荐的工作线程数
|
||||
ParallelThreshold int // 并行处理阈值
|
||||
LastMeasuredThroughput float64 // 上次测量的吞吐量
|
||||
SamplesCount int // 样本数
|
||||
}
|
||||
```
|
||||
|
||||
@@ -165,14 +383,86 @@ type StreamOptions struct {
|
||||
- `(x *XCipher) EncryptStreamWithOptions(reader io.Reader, writer io.Writer, options StreamOptions) (*StreamStats, error)` - 使用自定义选项加密流
|
||||
- `(x *XCipher) DecryptStreamWithOptions(reader io.Reader, writer io.Writer, options StreamOptions) (*StreamStats, error)` - 使用自定义选项解密流
|
||||
- `DefaultStreamOptions() StreamOptions` - 获取默认流处理选项
|
||||
- `GetOptimizedStreamOptions() StreamOptions` - 获取优化的流处理选项(自动适应当前系统)
|
||||
- `GetSystemOptimizationInfo() *OptimizationInfo` - 获取系统优化信息
|
||||
|
||||
## 🚀 性能
|
||||
## 🧪 测试与基准测试
|
||||
|
||||
go-xcipher 经过优化,可处理各种规模的数据,从小型消息到大型文件。以下是一些性能基准测试结果:
|
||||
### 运行单元测试
|
||||
|
||||
```bash
|
||||
# 运行所有测试
|
||||
go test
|
||||
|
||||
# 运行所有测试并显示详细输出
|
||||
go test -v
|
||||
|
||||
# 运行特定测试
|
||||
go test -run TestStreamParallelProcessing
|
||||
|
||||
# 运行特定测试组
|
||||
go test -run TestStream
|
||||
```
|
||||
|
||||
### 运行基准测试
|
||||
|
||||
```bash
|
||||
# 运行所有基准测试
|
||||
go test -bench=.
|
||||
|
||||
# 运行特定基准测试
|
||||
go test -bench=BenchmarkEncrypt
|
||||
|
||||
# 运行流处理性能矩阵基准测试
|
||||
go test -bench=BenchmarkStreamPerformanceMatrix
|
||||
|
||||
# 带内存分配统计的基准测试
|
||||
go test -bench=. -benchmem
|
||||
|
||||
# 多次运行以获得更准确的结果
|
||||
go test -bench=. -count=5
|
||||
```
|
||||
|
||||
### 性能分析
|
||||
|
||||
```bash
|
||||
# CPU性能分析
|
||||
go test -bench=BenchmarkStreamPerformanceMatrix -cpuprofile=cpu.prof
|
||||
|
||||
# 内存分析
|
||||
go test -bench=BenchmarkStreamPerformanceMatrix -memprofile=mem.prof
|
||||
|
||||
# 使用pprof查看性能分析结果
|
||||
go tool pprof cpu.prof
|
||||
go tool pprof mem.prof
|
||||
```
|
||||
|
||||
## 🚀 性能优化亮点
|
||||
|
||||
go-xcipher 经过多方面优化,可处理各种规模的数据,从小型消息到大型文件。以下是主要优化亮点:
|
||||
|
||||
### 自适应参数优化
|
||||
- 基于CPU架构和系统特性自动调整缓冲区大小和工作线程数
|
||||
- 运行时根据处理数据特性动态调整参数,实现最佳性能
|
||||
- 专门针对不同指令集(AVX, AVX2, SSE4.1, NEON)进行优化
|
||||
|
||||
### 高效并行处理
|
||||
- 智能决策何时使用并行处理,避免小数据并行带来的开销
|
||||
- 基于CPU核心数和缓存特性优化工作线程分配
|
||||
- 使用工作池和任务队列减少线程创建/销毁开销
|
||||
- 数据块自动平衡,确保各线程负载均衡
|
||||
|
||||
### 内存优化
|
||||
- 零拷贝技术减少内存数据复制操作
|
||||
- 内存缓冲池复用,显著减少GC压力
|
||||
- 批量处理和写入缓冲,减少系统调用次数
|
||||
- 缓冲区大小根据L1/L2/L3缓存特性优化,提高缓存命中率
|
||||
|
||||
### 性能数据
|
||||
- 小数据包加密:~1.5 GB/s
|
||||
- 大文件并行加密:~4.0 GB/s (取决于CPU核心数和硬件)
|
||||
- 内存效率:即使处理大文件,内存使用量仍保持在较低水平
|
||||
- 内存效率:处理大文件时内存使用量保持稳定,避免OOM风险
|
||||
- 基准测试结果表明比标准库实现快2-10倍(取决于数据大小和处理方式)
|
||||
|
||||
## 🤝 贡献
|
||||
|
||||
|
Reference in New Issue
Block a user