Last active
December 16, 2015 15:49
-
-
Save nexneo/5458411 to your computer and use it in GitHub Desktop.
Simulate async and sync message posting
This file contains 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 ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
// "math/rand" | |
"net/http" | |
"time" | |
) | |
var jobs int | |
var async bool | |
func init() { | |
flag.IntVar(&jobs, "n", 10, "No of jobs") | |
flag.BoolVar(&async, "async", false, "Run async") | |
} | |
type MessagePosting struct { | |
idx int | |
status string | |
} | |
func main() { | |
t1 := time.Now() | |
flag.Parse() | |
c := make(chan string) | |
FirePostings(jobs, c) | |
if async { | |
for i := jobs; i > 0; { | |
select { | |
case t := <-c: | |
fmt.Print(t) | |
i-- | |
} | |
} | |
} | |
fmt.Printf("\nTime: %.2fs\n", time.Now().Sub(t1).Seconds()) | |
} | |
func (mp *MessagePosting) process(c chan string) { | |
var length int64 | |
t1 := time.Now() | |
if mp.status == "queued" { | |
res, err := http.Get("http://google.com") | |
// time.Sleep(duration()) | |
res, err = http.Get("http://yahoo.com") | |
mp.status = "done" | |
by, err := ioutil.ReadAll(res.Body) | |
if err != nil { | |
panic(err) | |
} else { | |
length = int64(len(by)) | |
} | |
} | |
d := time.Now().Sub(t1).Seconds() | |
data := []interface{}{mp.idx, mp.status, d, length} | |
if async { | |
c <- fmt.Sprintf("%d %s (%.2fs) %d\n", data...) | |
} else { | |
fmt.Printf("%d %s (%.2fs) %d\n", data...) | |
} | |
} | |
func FirePostings(howmany int, c chan string) { | |
fmt.Println("Queued:", howmany, "jobs\n") | |
fmt.Println("Job# Runtime Size") | |
for i := 1; i < howmany+1; i++ { | |
mp := &MessagePosting{ | |
idx: i, | |
status: "queued", | |
} | |
// if async run goroutine for each job | |
if async { | |
go mp.process(c) | |
} else { | |
mp.process(c) | |
} | |
} | |
} | |
// func duration() time.Duration { | |
// return time.Duration(rand.Int63n(int64(1 * time.Second))) | |
// } |
This file contains 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
$ go run client.go -n 10 | |
Queued: 10 jobs | |
Job# Runtime Size | |
1 done (0.67s) 231847 | |
2 done (0.57s) 231887 | |
3 done (0.56s) 231292 | |
4 done (0.59s) 231175 | |
5 done (0.56s) 231759 | |
6 done (0.57s) 231871 | |
7 done (0.56s) 231160 | |
8 done (0.57s) 231812 | |
9 done (0.59s) 231219 | |
10 done (0.59s) 231116 | |
Time: 5.82s | |
$ go run client.go -n 10 -async | |
Queued: 10 jobs | |
Job# Runtime Size | |
5 done (0.70s) 154600 | |
2 done (0.72s) 231881 | |
9 done (0.73s) 231807 | |
10 done (0.73s) 231808 | |
3 done (0.74s) 231911 | |
8 done (0.74s) 231149 | |
6 done (0.74s) 231267 | |
4 done (0.75s) 231854 | |
7 done (0.75s) 232139 | |
1 done (0.80s) 231137 | |
Time: 0.80s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment