Skip to content

Instantly share code, notes, and snippets.

@eeue56
Created January 22, 2016 19:49
Show Gist options
  • Select an option

  • Save eeue56/448797d9c2807da9b563 to your computer and use it in GitHub Desktop.

Select an option

Save eeue56/448797d9c2807da9b563 to your computer and use it in GitHub Desktop.
package main
import "net/http"
import "encoding/json"
import "fmt"
const URL = "https://api.github.com/users/JEG2/repos"
const AMOUNT = 10
const CPUS = 4
func downloadAndParse(url string) []map[string]interface{} {
res, _ := http.Get(url)
defer res.Body.Close()
var data []map[string]interface{}
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&data); err != nil {
panic(err)
}
return data
}
func parseRepo(url string) map[string]interface{} {
res, _ := http.Get(url)
defer res.Body.Close()
var data map[string]interface{}
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&data); err != nil {
panic(err)
}
return data
}
func serial() {
jeg2 := downloadAndParse(URL)
for _, repo := range jeg2[:AMOUNT] {
makeRequest(repo)
}
}
func concurrent() {
jeg2 := downloadAndParse(URL)
repos := make(chan map[string]interface{}, AMOUNT)
done := make(chan bool, 1)
for i := 0; i < CPUS; i++ {
go func() {
for {
repo, rest := <-repos
if !rest {
done <- true
return
}
makeRequest(repo)
}
}()
}
for _, repo := range jeg2[:AMOUNT] {
repos <- repo
}
close(repos)
<-done
}
func makeRequest(repo map[string]interface{}) {
url := repo["url"].(string)
name := parseRepo(url)["full_name"]
fmt.Printf("Fetched %s repo\n", name)
}
// Noahs-MacBook-Pro:Go noah$ go test src/github.com/eeue56/caching-layer/jeg_test.go src/github.com/eeue56/caching-layer/jeg.go -bench . -count 1
// testing: warning: no tests to run
// PASS
// BenchmarkSerial-8 Fetched JEG2/advent_of_code_2015 repo
// Fetched JEG2/amalgalite repo
// Fetched JEG2/asciimation repo
// Fetched JEG2/attachment_fu repo
// Fetched JEG2/better_bj repo
// Fetched JEG2/bird_of_paradise repo
// Fetched JEG2/bottles_of_beer_song repo
// Fetched JEG2/broadsides repo
// Fetched JEG2/browser_captcha repo
// Fetched JEG2/challenges_for_game_designers repo
// 1 5767628399 ns/op
// BenchmarkConcurrent-8 Fetched JEG2/asciimation repo
// Fetched JEG2/advent_of_code_2015 repo
// Fetched JEG2/attachment_fu repo
// Fetched JEG2/amalgalite repo
// Fetched JEG2/better_bj repo
// Fetched JEG2/bird_of_paradise repo
// Fetched JEG2/bottles_of_beer_song repo
// 1 1326897523 ns/op
// ok command-line-arguments 7.107s
package main
import "testing"
func BenchmarkSerial(b *testing.B) {
for i := 0; i < b.N; i++ {
serial()
}
}
func BenchmarkConcurrent(b *testing.B) {
for i := 0; i < b.N; i++ {
concurrent()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment