-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp.go
65 lines (53 loc) · 1.23 KB
/
http.go
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
package main
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"sync"
"time"
)
func test() {
// 设置自定义代理
proxyURL, _ := url.Parse("http://127.0.0.1:8080")
// 创建一个带超时的 HTTP 客户端
client := &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
// 并发请求数量
concurrency := 200
// 使用 WaitGroup 等待所有请求完成
var wg sync.WaitGroup
wg.Add(concurrency)
startTime := time.Now()
for i := 0; i < concurrency; i++ {
go func() {
defer wg.Done()
// 发起 GET 请求
resp, err := client.Get("https://api.m.jd.com/")
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
// 读取响应内容
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return
}
// 打印响应内容长度
fmt.Printf("Response: %v \n", string(body))
}()
}
// 等待所有请求完成
wg.Wait()
// 计算总耗时
elapsed := time.Since(startTime)
fmt.Printf("Total time: %s\n", elapsed)
}