|
@ -4,22 +4,24 @@ import ( |
|
|
"fmt" |
|
|
"fmt" |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
"net/http" |
|
|
"net/http" |
|
|
|
|
|
"time" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func getPage(url string) (int, error) { |
|
|
|
|
|
|
|
|
func getPage(url string) (int, time.Duration, error) { |
|
|
|
|
|
|
|
|
|
|
|
start := time.Now() |
|
|
resp, err := http.Get(url) |
|
|
resp, err := http.Get(url) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return 0, err |
|
|
|
|
|
|
|
|
return 0, 0, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
defer resp.Body.Close() |
|
|
defer resp.Body.Close() |
|
|
body, err := ioutil.ReadAll(resp.Body) |
|
|
body, err := ioutil.ReadAll(resp.Body) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return 0, err |
|
|
|
|
|
|
|
|
return 0, 0, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return len(body), nil |
|
|
|
|
|
|
|
|
return len(body), time.Since(start), nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func main() { |
|
|
func main() { |
|
@ -29,11 +31,15 @@ func main() { |
|
|
"Bing": "http://bing.com", |
|
|
"Bing": "http://bing.com", |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
start := time.Now() |
|
|
|
|
|
|
|
|
for name, url := range sites { |
|
|
for name, url := range sites { |
|
|
length, err := getPage(url) |
|
|
|
|
|
|
|
|
length, dur, err := getPage(url) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
fmt.Printf("%s %s\n", name, err) |
|
|
fmt.Printf("%s %s\n", name, err) |
|
|
} |
|
|
} |
|
|
fmt.Printf("%s %d\n", name, length) |
|
|
|
|
|
|
|
|
fmt.Printf("%s %d %v\n", name, length, dur) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fmt.Printf("Total time: %v\n", time.Since(start)) |
|
|
} |
|
|
} |