Skip to content

Instantly share code, notes, and snippets.

@patmigliaccio
Last active March 5, 2018 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patmigliaccio/f0d17a760aeeae78999859d7eec62b22 to your computer and use it in GitHub Desktop.
Save patmigliaccio/f0d17a760aeeae78999859d7eec62b22 to your computer and use it in GitHub Desktop.
A quick Go script, testing the performance of using concurrent channels for HTTP requests.
/**
* A quick Go script, testing the performance of using
* concurrent channels for HTTP requests.
*
* Example:
* $ go run concurrent_http.go https://google.com https://facebook.com
*
* -- Consecutive --
* GET: https://google.com
* 0.61 elapsed with response length: 10711 https://google.com
* GET: https://facebook.com
* 0.42 elapsed with response length: 218651 https://facebook.com
* -- 1.09s elapsed --
*
* -- Concurrent --
* GET: https://google.com
* GET: https://facebook.com
* 0.17 elapsed with response length: 10668 https://google.com
* 0.24 elapsed with response length: 219005 https://facebook.com
* -- 0.25s elapsed --
*
* Author: Pat Migliaccio <pat@patmigliaccio.com>
* License: MIT
*/
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
func main() {
Consecutive(os.Args)
Concurrent(os.Args)
}
// Consecutive makes HTTP requests consecutively
func Consecutive(args []string) {
fmt.Println("\n-- Consecutive --")
startOne := time.Now()
for _, url := range os.Args[1:] {
fmt.Printf("GET: %s\n", url)
MakeRequest(url)
}
fmt.Printf("-- %.2fs elapsed --\n\n", time.Since(startOne).Seconds())
}
// Concurrent makes HTTP requests concurrently in GoRoutines
func Concurrent(args []string) {
fmt.Println("-- Concurrent --")
startTwo := time.Now()
ch := make(chan string)
for _, url := range args[1:] {
fmt.Printf("GET: %s\n", url)
go MakeRequestConcurrent(url, ch)
}
for range args[1:] {
fmt.Println(<-ch)
}
fmt.Printf("-- %.2fs elapsed --\n\n", time.Since(startTwo).Seconds())
}
// MakeRequest calls an HTTP Get Request
func MakeRequest(url string) {
start := time.Now()
resp, _ := http.Get(url)
secs := time.Since(start).Seconds()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf("%.2f elapsed with response length: %d %s\n", secs, len(body), url)
}
// MakeRequestConcurrent calls an HTTP Get Request concurrently
func MakeRequestConcurrent(url string, ch chan<- string) {
start := time.Now()
resp, _ := http.Get(url)
secs := time.Since(start).Seconds()
body, _ := ioutil.ReadAll(resp.Body)
ch <- fmt.Sprintf("%.2f elapsed with response length: %d %s", secs, len(body), url)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment