Created
February 26, 2014 09:23
-
-
Save iaintshine/9226385 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"os" | |
"runtime" | |
"sync" | |
"time" | |
) | |
var ( | |
services = []string{"http://www.onet.pl", "http://www.wp.pl", "http://www.google.pl"} | |
) | |
func timing(f func()) time.Duration { | |
start := time.Now() | |
f() | |
end := time.Now() | |
return end.Sub(start) | |
} | |
func querySequential() { | |
for _, service := range services { | |
fmt.Println("Fetching current data from service", service) | |
resp, err := http.Get(service) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, "Failed to fetch a data from the service", service, "reason", err) | |
continue | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, "Failed to fetch chunked body from service", service, "reason", err) | |
continue | |
} | |
fmt.Println("Read", len(body), "bytes from", service) | |
} | |
} | |
func queryParallel() { | |
finished := make(chan bool) | |
for _, service := range services { | |
go func() { | |
fmt.Println("Fetching current data from service", service) | |
resp, err := http.Get(service) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, "Failed to fetch a data from the service", service, "reason", err) | |
finished <- false | |
return | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, "Failed to fetch chunked body from service", service, "reason", err) | |
finished <- false | |
return | |
} | |
fmt.Println("Read", len(body), "bytes from", service) | |
finished <- true | |
}() | |
} | |
d := time.Duration(200 * time.Millisecond) | |
timeout := time.After(d) | |
for _ = range services { | |
select { | |
case <-finished: | |
continue | |
case <-timeout: | |
fmt.Fprintln(os.Stderr, "It took too long to fetch all the data. Timeout", d, "expired") | |
return | |
} | |
} | |
} | |
func queryParallelWaitGroup() { | |
var wg sync.WaitGroup | |
for _, service := range services { | |
wg.Add(1) | |
go func() { | |
defer wg.Done() | |
fmt.Println("Fetching current data from service", service) | |
resp, err := http.Get(service) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, "Failed to fetch a data from the service", service, "reason", err) | |
return | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, "Failed to fetch chunked body from service", service, "reason", err) | |
return | |
} | |
fmt.Println("Read", len(body), "bytes from", service) | |
}() | |
} | |
wg.Wait() | |
} | |
// TODO: Add a program that behaves like actors that send concurrently emails, some fetch data from websites - aka cradwlers, | |
// Than others are extracting possible email addresses and than list of email addresses is sent to email sender | |
// which sends to developer addresses, original text and a source | |
// Second actor can translate xml into json | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
fmt.Println("Number of logical CPUs", runtime.NumCPU()) | |
fmt.Println("GOMAXPROCS", runtime.GOMAXPROCS(0)) | |
fmt.Println("Sequential timing:", timing(querySequential)) | |
fmt.Println("Parallel timing:", timing(queryParallel)) | |
fmt.Println("Wait Group timing:", timing(queryParallelWaitGroup)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment