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.
65 lines
1.1 KiB
65 lines
1.1 KiB
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type Data struct {
|
|
length int
|
|
duration time.Duration
|
|
}
|
|
|
|
func getPage(url string) (Data, error) {
|
|
|
|
var d Data
|
|
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
return d, err
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
start := time.Now()
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return d, err
|
|
}
|
|
dur := time.Since(start)
|
|
|
|
d.length = len(body)
|
|
d.duration = dur
|
|
|
|
return d, nil
|
|
}
|
|
|
|
func main() {
|
|
sites := map[string]string{
|
|
"Google": "http://google.com",
|
|
"Yahoo": "http://yahoo.com",
|
|
"Bing": "http://bing.com",
|
|
"Apple": "http://apple.com",
|
|
"Samsung": "http://samsung.com",
|
|
"Microsoft": "http://microsoft.com",
|
|
"Ubuntu": "http://ubuntu.com",
|
|
"Verizon": "http://verizonwireless.com",
|
|
"Comcast": "http://comcast.com",
|
|
}
|
|
|
|
start := time.Now()
|
|
|
|
for name, url := range sites {
|
|
data, err := getPage(url)
|
|
if err != nil {
|
|
fmt.Printf("%s %s\n", name, err)
|
|
}
|
|
fmt.Printf("%-10s %6.2f KB %7.2f ms\n",
|
|
name,
|
|
float32(data.length)/1024,
|
|
float32(data.duration)/1000000)
|
|
}
|
|
|
|
fmt.Printf("Total time: %.3f seconds\n", time.Since(start).Seconds())
|
|
}
|