-
Notifications
You must be signed in to change notification settings - Fork 14
/
async_test.go
66 lines (58 loc) · 1.56 KB
/
async_test.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
66
package async
import (
"fmt"
"io/ioutil"
"net/http"
"runtime"
"testing"
"time"
)
func TestAsync(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
fmt.Println("program start")
useTime := make([]int64, 2)
startTime := time.Now().UnixNano()
get("http://www.baidu.com")
fmt.Println("name: baidu, done")
get("http://www.sina.com")
fmt.Println("name: sina, done")
get("http://www.sohu.com")
fmt.Println("name: sohu, done")
get("http://www.163.com")
fmt.Println("name: 163, done")
useTime[0] = time.Now().UnixNano() - startTime
fmt.Printf("single-threaded use time: %d\n", useTime[0])
fmt.Println("==============\nasync start")
startTime = time.Now().UnixNano()
async := New()
async.Add("baidu", get, "http://www.baidu.com")
async.Add("sina", get, "http://www.sina.com")
async.Add("sohu", get, "http://www.sohu.com")
async.Add("163", get, "http://www.163.com")
if chans, ok := async.Run(); ok {
rs := <-chans
if len(rs) == 4 {
for k := range rs {
fmt.Printf("name: %s, \t done \n", k)
}
} else {
t.Error("async not execution all task")
}
}
useTime[1] = time.Now().UnixNano() - startTime
fmt.Printf("async use time: %d \n=====\n", useTime[1])
if useTime[1] < useTime[0] {
fmt.Printf("async faster than single-threaded: %d \n", useTime[0]-useTime[1])
} else {
fmt.Printf("single-threaded faster than async: %d \n", useTime[1]-useTime[0])
}
}
func get(url string) string {
if rs, err := http.Get(url); err == nil {
defer rs.Body.Close()
if res, err := ioutil.ReadAll(rs.Body); err == nil {
return string(res)
}
}
return ""
}