mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-21 19:00:42 +08:00
- port_scan: fmt.Sprintf→JoinHostPort+fmtPort零分配地址格式化 (2.5x) - port_scan: strings.ToLower→containsFold零分配大小写不敏感匹配 (2.5x) - port_scan: slidingWindowSchedule修复semaphore泄漏bug - service_probe: readFromConn预分配4KB缓冲区消除扩容 - adaptive_pool: maybeAdjust用atomic CAS代替持锁检查, 99%免锁 - adaptive_timeout: Timeout锁外计算均值/标准差, 只锁缓存更新 - 新增perf_bench_test.go基准测试验证所有优化
103 lines
2.6 KiB
Go
103 lines
2.6 KiB
Go
package core
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// =============================================================================
|
|
// Benchmark: containsFold vs strings.ToLower + strings.Contains
|
|
// =============================================================================
|
|
|
|
func BenchmarkContainsFold(b *testing.B) {
|
|
err := errors.New("connection reset by peer: 192.168.1.1:445")
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
containsFold(err.Error(), "connection reset")
|
|
}
|
|
}
|
|
|
|
func BenchmarkStringsToLowerContains(b *testing.B) {
|
|
err := errors.New("connection reset by peer: 192.168.1.1:445")
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
strings.Contains(strings.ToLower(err.Error()), "connection reset")
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Benchmark: fmt.Sprintf vs net.JoinHostPort + fmtPort
|
|
// =============================================================================
|
|
|
|
func BenchmarkFmtSprintfAddr(b *testing.B) {
|
|
host := "192.168.1.1"
|
|
port := 445
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = fmt.Sprintf("%s:%d", host, port)
|
|
}
|
|
}
|
|
|
|
func BenchmarkJoinHostPortFmtPort(b *testing.B) {
|
|
host := "192.168.1.1"
|
|
port := 445
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = net.JoinHostPort(host, fmtPort(port))
|
|
}
|
|
}
|
|
|
|
func BenchmarkFmtPort(b *testing.B) {
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = fmtPort(445)
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Benchmark: readFromConn buffer pre-allocation
|
|
// =============================================================================
|
|
|
|
func BenchmarkAppendFromNil(b *testing.B) {
|
|
data := []byte("HTTP/1.1 200 OK\r\nServer: nginx")
|
|
chunk := data[:10]
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
var result []byte
|
|
result = append(result, chunk...)
|
|
result = append(result, chunk...)
|
|
_ = result
|
|
}
|
|
}
|
|
|
|
func BenchmarkAppendPreAllocated(b *testing.B) {
|
|
data := []byte("HTTP/1.1 200 OK\r\nServer: nginx")
|
|
chunk := data[:10]
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
result := make([]byte, 0, 4096)
|
|
result = append(result, chunk...)
|
|
result = append(result, chunk...)
|
|
_ = result
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Benchmark: AdaptiveTimeout computation under lock vs outside lock
|
|
// =============================================================================
|
|
|
|
func BenchmarkAdaptiveTimeoutComputation(b *testing.B) {
|
|
at := NewAdaptiveTimeout(3000 * 1000000) // 3s in ns
|
|
// Warm up: add 64 samples
|
|
for i := 0; i < 64; i++ {
|
|
at.Record(10 * 1000000) // 10ms in ns
|
|
}
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = at.Timeout()
|
|
}
|
|
}
|