You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
2.3 KiB
109 lines
2.3 KiB
package main
|
|
|
|
import (
|
|
"IPRegion/global"
|
|
"IPRegion/ip2region"
|
|
"encoding/json"
|
|
"fmt"
|
|
"math/rand"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestIpInfo(tt *testing.T) {
|
|
address := "114.114.114.114"
|
|
var ipInfo ip2region.IpInfo
|
|
ipInfo, _ = ip2region.GetIPInfo(address, global.SearchType)
|
|
res, _ := json.Marshal(ipInfo)
|
|
fmt.Println(string(res))
|
|
|
|
}
|
|
|
|
func TestRandomIpInfo(tt *testing.T) {
|
|
rand.Seed(time.Now().UnixNano())
|
|
var ipInfo ip2region.IpInfo
|
|
for {
|
|
address := fmt.Sprintf("%d.%d.%d.%d", rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255))
|
|
done := make(chan struct{}, 1)
|
|
go func() {
|
|
fmt.Println(address)
|
|
ipInfo, _ = ip2region.GetIPInfo(address, global.SearchType)
|
|
res, _ := json.Marshal(ipInfo)
|
|
fmt.Println(string(res))
|
|
}()
|
|
select {
|
|
case <-done:
|
|
fmt.Println("call successfully!!!")
|
|
continue
|
|
case <-time.After(time.Duration(100 * time.Millisecond)):
|
|
fmt.Println("timeout!!!")
|
|
continue
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func AsyncCall() {
|
|
done := make(chan struct{}, 1)
|
|
|
|
//go func(ctx context.Context) {
|
|
// // 发送HTTP请求
|
|
// done <- struct{}{}
|
|
//}(ctx)
|
|
|
|
select {
|
|
case <-done:
|
|
fmt.Println("call successfully!!!")
|
|
return
|
|
case <-time.After(time.Duration(100 * time.Millisecond)):
|
|
fmt.Println("timeout!!!")
|
|
return
|
|
}
|
|
}
|
|
|
|
// TestIpRangeChannel 内存测试
|
|
func TestIpRangeChannel(tt *testing.T) {
|
|
ch := make(chan struct{}, 600)
|
|
defer close(ch)
|
|
for ip1 := 1; ip1 <= 255; ip1++ {
|
|
for ip2 := 0; ip2 <= 255; ip2++ {
|
|
for ip3 := 0; ip3 <= 255; ip3++ {
|
|
for ip4 := 1; ip4 <= 254; ip4++ {
|
|
ch <- struct{}{}
|
|
go func() {
|
|
address := fmt.Sprintf("%d.%d.%d.%d", ip1, ip2, ip3, ip4)
|
|
ipInfo, _ := ip2region.GetIPInfo(address, global.SearchType)
|
|
_, _ = json.Marshal(ipInfo)
|
|
fmt.Println(address)
|
|
<-ch
|
|
}()
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestIpRangeLock 不限制情况 内存漏完
|
|
func TestIpRangeLock(tt *testing.T) {
|
|
var lock sync.Mutex
|
|
for ip1 := 1; ip1 <= 255; ip1++ {
|
|
for ip2 := 0; ip2 <= 255; ip2++ {
|
|
for ip3 := 0; ip3 <= 255; ip3++ {
|
|
for ip4 := 1; ip4 <= 254; ip4++ {
|
|
go func() {
|
|
lock.Lock()
|
|
address := fmt.Sprintf("%d.%d.%d.%d", ip1, ip2, ip3, ip4)
|
|
ipInfo, _ := ip2region.GetIPInfo(address, global.SearchType)
|
|
res, _ := json.Marshal(ipInfo)
|
|
fmt.Println(string(res))
|
|
lock.Unlock()
|
|
}()
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|