-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
211 lines (183 loc) · 5.07 KB
/
Copy pathbenchmark_test.go
File metadata and controls
211 lines (183 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package proxy
import (
"bufio"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"testing"
"time"
)
const proxyScrapeAPI = "https://api.proxyscrape.com/v4/free-proxy-list/get?request=display_proxies&proxy_format=protocolipport&format=json"
type proxyHawkConfig struct {
Timeout time.Duration
MaxConcurrent int
ValidationURL string
ValidationPattern string
}
type benchConfig struct {
ProxyHawk proxyHawkConfig
}
type proxyResult struct {
ProxyURL string
Latency time.Duration
Working bool
Error error
}
func fetchProxiesFromAPI() ([]string, error) {
resp, err := http.Get(proxyScrapeAPI)
if err != nil {
return nil, fmt.Errorf("failed to fetch proxies: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response: %v", err)
}
var proxies []string
scanner := bufio.NewScanner(strings.NewReader(string(body)))
for scanner.Scan() {
proxy := scanner.Text()
// Split by space in case there are multiple proxies per line
for _, p := range strings.Fields(proxy) {
// Convert socks4:// prefix to socks4: to match our format
p = strings.ReplaceAll(p, "socks4://", "socks4:")
// Convert http:// prefix to http: to match our format
p = strings.ReplaceAll(p, "http://", "http:")
proxies = append(proxies, p)
}
}
return proxies, nil
}
func BenchmarkProxyScrapeChecking(b *testing.B) {
// Create test config
cfg := &benchConfig{
ProxyHawk: proxyHawkConfig{
Timeout: 10 * time.Second,
MaxConcurrent: 50,
ValidationURL: "https://www.google.com",
ValidationPattern: ".*",
},
}
// Fetch proxies from ProxyScrape API
resp, err := http.Get("https://api.proxyscrape.com/v2/?request=displayproxies&protocol=http&timeout=10000&country=all&ssl=all&anonymity=all")
if err != nil {
b.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
b.Fatal(err)
}
// Parse proxies and clean the URLs
proxyList := strings.Split(string(body), "\n")
cleanProxyList := make([]string, 0, len(proxyList))
for _, proxy := range proxyList {
proxy = strings.TrimSpace(proxy)
if proxy != "" {
cleanProxyList = append(cleanProxyList, proxy)
}
}
b.Logf("Fetched %d proxies from ProxyScrape API", len(cleanProxyList))
// Reset timer before starting proxy checks
b.ResetTimer()
// Run the benchmark
for i := 0; i < b.N; i++ {
// Create channels for results and concurrency control
results := make(chan *proxyResult, len(cleanProxyList))
sem := make(chan struct{}, cfg.ProxyHawk.MaxConcurrent)
var wg sync.WaitGroup
// Check each proxy
workingProxies := 0
totalProxies := len(cleanProxyList)
var totalLatency time.Duration
var errors []error
// Start a goroutine to close results channel when all workers are done
wg.Add(len(cleanProxyList))
go func() {
wg.Wait()
close(results)
}()
// Launch proxy checkers
for _, proxy := range cleanProxyList {
sem <- struct{}{} // Acquire semaphore
go func(proxyStr string) {
defer wg.Done()
defer func() { <-sem }() // Release semaphore
// Create proxy URL
proxyURL, err := url.Parse("http://" + proxyStr)
if err != nil {
results <- &proxyResult{
ProxyURL: proxyStr,
Error: err,
}
return
}
// Create HTTP client with proxy
client := &http.Client{
Timeout: 5 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: &http.Transport{
DisableKeepAlives: true,
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
// Test proxy
start := time.Now()
resp, err := client.Get(cfg.ProxyHawk.ValidationURL)
if err != nil {
results <- &proxyResult{
ProxyURL: proxyStr,
Error: err,
}
return
}
defer resp.Body.Close()
latency := time.Since(start)
// Check response
if resp.StatusCode != http.StatusOK {
results <- &proxyResult{
ProxyURL: proxyStr,
Error: fmt.Errorf("unexpected status code: %d", resp.StatusCode),
}
return
}
results <- &proxyResult{
ProxyURL: proxyStr,
Latency: latency,
Working: true,
}
}(proxy)
}
// Process results
for result := range results {
if result.Working {
workingProxies++
totalLatency += result.Latency
} else if result.Error != nil {
errors = append(errors, fmt.Errorf("Proxy %s error: %v", result.ProxyURL, result.Error))
}
}
// Log results
b.Logf("Working proxies: %d/%d (%.2f%%)", workingProxies, totalProxies, float64(workingProxies)/float64(totalProxies)*100)
if workingProxies > 0 {
avgLatency := totalLatency / time.Duration(workingProxies)
b.Logf("Average response time: %v", avgLatency)
}
// Log sample of errors if no working proxies found
if workingProxies == 0 && len(errors) > 0 {
b.Log("Sample of proxy check errors:")
for i := 0; i < 5 && i < len(errors); i++ {
b.Log(errors[i])
}
}
}
}