Learning how to time pulling data from multiple URLs...
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.

45 lines
754 B

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "time"
  7. )
  8. func getPage(url string) (int, time.Duration, error) {
  9. start := time.Now()
  10. resp, err := http.Get(url)
  11. if err != nil {
  12. return 0, 0, err
  13. }
  14. defer resp.Body.Close()
  15. body, err := ioutil.ReadAll(resp.Body)
  16. if err != nil {
  17. return 0, 0, err
  18. }
  19. return len(body), time.Since(start), nil
  20. }
  21. func main() {
  22. sites := map[string]string{
  23. "Google": "http://google.com",
  24. "Yahoo": "http://yahoo.com",
  25. "Bing": "http://bing.com",
  26. }
  27. start := time.Now()
  28. for name, url := range sites {
  29. length, dur, err := getPage(url)
  30. if err != nil {
  31. fmt.Printf("%s %s\n", name, err)
  32. }
  33. fmt.Printf("%s %d %v\n", name, length, dur)
  34. }
  35. fmt.Printf("Total time: %v\n", time.Since(start))
  36. }