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
|
package main
import(
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"sync"
"time"
)
type Limit struct {
number int
channel chan struct{}
}
// Limit struct 初始化
func New(number int) *Limit {
return &Limit{
number: number,
channel: make(chan struct{}, number),
}
}
// Run 方法:创建有限的 go f 函数的 goroutine
func (limit *Limit) Run(f func()) {
limit.channel <- struct{}{}
go func() {
f()
<-limit.channel
}()
}
// WaitGroup 对象内部有一个计数器,从0开始
// 有三个方法:Add(), Done(), Wait() 用来控制计数器的数量
var wg = sync.WaitGroup{}
const (
concurrency = 5 // 控制并发量
)
func main() {
start := time.Now()
limit := New(concurrency) // New Limit 控制并发量
// 接口请求URL
apiUrl := "http://apis.juhe.cn/mobile/get" // 不要使用接口地址测试
//max := int(math.Pow10(8)) // 模拟一千万数据
max := 5 // 先测试5次吧
// 初始化参数
param := url.Values{}
param.Set("key", "您申请的KEY") // 接口请求Key
for i := 0; i < max; i++ {
wg.Add(1)
value := i
goFunc := func() {
fmt.Printf("start func: %dn", value)
// 配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参
phone := RandMobile()
param.Set("phone", phone) // 需要查询的手机号码或手机号码前7位
// 发送请求
data, err := Get(apiUrl, param)
if err != nil {
fmt.Println(err)
return
}
// 其它逻辑代码...
fmt.Println("phone: ", phone, string(data))
wg.Done()
}
limit.Run(goFunc)
}
// 阻塞代码防止退出
wg.Wait()
fmt.Printf("耗时: %fs", time.Now().Sub(start).Seconds())
}
// Get 方式发起网络请求
func Get(apiURL string, params url.Values) (rs []byte, err error) {
var Url *url.URL
Url, err = url.Parse(apiURL)
if err != nil {
fmt.Printf("解析url错误:rn%v", err)
return nil, err
}
//如果参数中有中文参数,这个方法会进行URLEncode
Url.RawQuery = params.Encode()
resp, err := http.Get(Url.String())
if err != nil {
fmt.Println("err:", err)
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
var MobilePrefix = [...]string{"130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "145", "147", "150", "151", "152", "153", "155", "156", "157", "158", "159", "170", "176", "177", "178", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189"}
// GeneratorPhone 生成手机号码
func RandMobile() string {
return MobilePrefix[RandInt(0, len(MobilePrefix))] + fmt.Sprintf("%0*d", 8, RandInt(0, 100000000))
}
// 指定范围随机 int
func RandInt(min, max int) int {
rand.Seed(time.Now().UnixNano())
return min + rand.Intn(max-min)
}
|