mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-23 19:51:52 +08:00
perf: 六项性能优化
- DNS 解析缓存:sync.Map 缓存避免重复系统调用 - 凭据测试 TCP 预检:不可达目标直接跳过全部凭据 - Web 探测 HTTP Client 复用:全局共享连接池 - 端口扫描 Bloom Filter 去重:替代 map 降低内存 - 进度条 atomic 累加 + 50ms 节流渲染:消除锁竞争 - 服务探针预解码:Init 时预编译,运行时零解码开销
This commit is contained in:
+26
-17
@@ -143,16 +143,24 @@ func (pm *ProgressManager) UpdateProgress(increment int64) {
|
||||
return
|
||||
}
|
||||
|
||||
pm.mu.Lock()
|
||||
defer pm.mu.Unlock()
|
||||
|
||||
pm.current += increment
|
||||
if pm.current > pm.total {
|
||||
pm.current = pm.total
|
||||
// 原子累加,避免高并发下的锁竞争
|
||||
newCurrent := atomic.AddInt64(&pm.current, increment)
|
||||
if newCurrent > pm.total {
|
||||
atomic.StoreInt64(&pm.current, pm.total)
|
||||
}
|
||||
|
||||
// 更新活跃时间
|
||||
pm.lastActivity = time.Now()
|
||||
// 节流渲染:距上次渲染不足 50ms 则跳过
|
||||
now := time.Now()
|
||||
pm.mu.RLock()
|
||||
lastAct := pm.lastActivity
|
||||
pm.mu.RUnlock()
|
||||
if now.Sub(lastAct) < 50*time.Millisecond {
|
||||
return
|
||||
}
|
||||
|
||||
pm.mu.Lock()
|
||||
pm.lastActivity = now
|
||||
pm.mu.Unlock()
|
||||
|
||||
pm.renderProgress()
|
||||
}
|
||||
@@ -170,7 +178,7 @@ func (pm *ProgressManager) FinishProgress() {
|
||||
pm.mu.Lock()
|
||||
defer pm.mu.Unlock()
|
||||
|
||||
pm.current = pm.total
|
||||
atomic.StoreInt64(&pm.current, pm.total)
|
||||
pm.renderProgress()
|
||||
|
||||
// 停止活跃指示器
|
||||
@@ -220,11 +228,12 @@ func (pm *ProgressManager) generateProgressBar() string {
|
||||
return base
|
||||
}
|
||||
|
||||
percentage := float64(pm.current) / float64(pm.total) * 100
|
||||
percentage := float64(atomic.LoadInt64(&pm.current)) / float64(pm.total) * 100
|
||||
elapsed := time.Since(pm.startTime)
|
||||
current := atomic.LoadInt64(&pm.current)
|
||||
|
||||
// 计算速度
|
||||
speed := float64(pm.current) / elapsed.Seconds()
|
||||
speed := float64(current) / elapsed.Seconds()
|
||||
speedStr := ""
|
||||
if speed > 0 {
|
||||
speedStr = fmt.Sprintf(" %.0f/s", speed)
|
||||
@@ -232,8 +241,8 @@ func (pm *ProgressManager) generateProgressBar() string {
|
||||
|
||||
// 计算预估剩余时间
|
||||
var eta string
|
||||
if pm.current > 0 && pm.current < pm.total {
|
||||
totalTime := elapsed * time.Duration(pm.total) / time.Duration(pm.current)
|
||||
if current > 0 && current < pm.total {
|
||||
totalTime := elapsed * time.Duration(pm.total) / time.Duration(current)
|
||||
remaining := totalTime - elapsed
|
||||
if remaining > 0 {
|
||||
eta = fmt.Sprintf(" ETA:%s", formatDuration(remaining))
|
||||
@@ -245,7 +254,7 @@ func (pm *ProgressManager) generateProgressBar() string {
|
||||
|
||||
// 计算固定部分的宽度
|
||||
fixedPart := fmt.Sprintf("%s %s %5.1f%% [] (%d/%d)%s%s %s",
|
||||
pm.description, spinner, percentage, pm.current, pm.total, speedStr, eta, packetInfo)
|
||||
pm.description, spinner, percentage, current, pm.total, speedStr, eta, packetInfo)
|
||||
fixedWidth := displayWidth(fixedPart)
|
||||
|
||||
// 计算进度条槽位可用宽度(预留2字符余量)
|
||||
@@ -272,7 +281,7 @@ func (pm *ProgressManager) generateProgressBar() string {
|
||||
|
||||
// 构建最终进度条
|
||||
result := fmt.Sprintf("%s %s %5.1f%% %s (%d/%d)%s%s",
|
||||
pm.description, spinner, percentage, bar, pm.current, pm.total, speedStr, eta)
|
||||
pm.description, spinner, percentage, bar, current, pm.total, speedStr, eta)
|
||||
|
||||
if packetInfo != "" {
|
||||
result += " " + packetInfo
|
||||
@@ -470,7 +479,7 @@ func (pm *ProgressManager) GetPercent() float64 {
|
||||
if !pm.isActive || pm.total == 0 {
|
||||
return 0
|
||||
}
|
||||
return float64(pm.current) / float64(pm.total) * 100
|
||||
return float64(atomic.LoadInt64(&pm.current)) / float64(pm.total) * 100
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
@@ -512,7 +521,7 @@ func (pm *ProgressManager) renderProgressUnsafe() {
|
||||
// 计算当前百分比(避免除零)
|
||||
currentPercent := 0
|
||||
if pm.total > 0 {
|
||||
currentPercent = int((pm.current * 100) / pm.total)
|
||||
currentPercent = int((atomic.LoadInt64(&pm.current) * 100) / pm.total)
|
||||
}
|
||||
|
||||
// 只在百分比变化时更新,减少不必要的渲染
|
||||
|
||||
Reference in New Issue
Block a user