Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "net/http"
- "os"
- "strconv"
- //"math"
- "time"
- "github.com/gorilla/mux"
- )
- func Async(url string, count int) <-chan int {
- c := make(chan int)
- go func() {
- cnt := 0
- for i := 0; i < count; i++ {
- GetPage(url)
- cnt = cnt + 1
- if (cnt == 50) {
- c <- i
- cnt = 0
- }
- }
- }()
- return c
- }
- func CallSync(w http.ResponseWriter, r *http.Request) {
- t0 := time.Now()
- c1 := Async("http://localhost:8080/", 2000)
- c2 := Async("http://localhost:8080/", 2000)
- c3 := Async("http://localhost:8080/", 2000)
- c4 := Async("http://localhost:8080/", 2000)
- c5 := Async("http://localhost:8080/", 2000)
- c6 := Async("http://localhost:8080/", 2000)
- c7 := Async("http://localhost:8080/", 2000)
- c8 := Async("http://localhost:8080/", 2000)
- c9 := Async("http://localhost:8080/", 2000)
- c10 := Async("http://localhost:8080/", 2000)
- c11 := Async("http://localhost:8080/", 2000)
- c12 := Async("http://localhost:8080/", 2000)
- c13 := Async("http://localhost:8080/", 2000)
- c14 := Async("http://localhost:8080/", 2000)
- c15 := Async("http://localhost:8080/", 2000)
- c16 := Async("http://localhost:8080/", 2000)
- c17 := Async("http://localhost:8080/", 2000)
- c18 := Async("http://localhost:8080/", 2000)
- c19 := Async("http://localhost:8080/", 2000)
- c20 := Async("http://localhost:8080/", 2000)
- count := 0
- for count < 4000 {
- select {
- case v1 := <-c1:
- count = count + 50
- fmt.Printf("%v c1\n", v1)
- case v2 := <-c2:
- count = count + 50
- fmt.Printf("%v c2\n", v2)
- case v3 := <-c3:
- count = count + 50
- fmt.Printf("%v c3\n", v3)
- case v4 := <-c4:
- count = count + 50
- fmt.Printf("%v c4\n", v4)
- case v5 := <-c5:
- count = count + 50
- fmt.Printf("%v c5\n", v5)
- case v6 := <-c6:
- count = count + 50
- fmt.Printf("%v c6\n", v6)
- case v7 := <-c7:
- count = count + 50
- fmt.Printf("%v c7\n", v7)
- case v8 := <-c8:
- count = count + 50
- fmt.Printf("%v c8\n", v8)
- case v9 := <-c9:
- count = count + 50
- fmt.Printf("%v c9\n", v9)
- case v10 := <-c10:
- count = count + 50
- fmt.Printf("%v c10\n", v10)
- case v11 := <-c11:
- count = count + 50
- fmt.Printf("%v c11\n", v11)
- case v12 := <-c12:
- count = count + 50
- fmt.Printf("%v c12\n", v12)
- case v13 := <-c13:
- count = count + 50
- fmt.Printf("%v c13\n", v13)
- case v14 := <-c14:
- count = count + 50
- fmt.Printf("%v c14\n", v14)
- case v15 := <-c15:
- count = count + 50
- fmt.Printf("%v c15\n", v15)
- case v16 := <-c16:
- count = count + 50
- fmt.Printf("%v c16\n", v16)
- case v17 := <-c17:
- count = count + 50
- fmt.Printf("%v c17\n", v17)
- case v18 := <-c18:
- count = count + 50
- fmt.Printf("%v c18\n", v18)
- case v19 := <-c19:
- count = count + 50
- fmt.Printf("%v c19\n", v19)
- case v20 := <-c20:
- count = count + 50
- fmt.Printf("%v c20\n", v20)
- default:
- }
- }
- t1 := time.Now()
- diff := t1.Sub(t0)
- num := diff.Nanoseconds() / int64(time.Millisecond)
- msec := float64(num) / 1000
- reqsec := float64(count) / msec
- fmt.Fprintf(w, "%v requests\n", count)
- fmt.Fprintf(w, "Performed in %v\n", diff)
- fmt.Fprintf(w, "At a rate of %v (requests/sec)\n", reqsec)
- }
- func GetPage(page string) {
- resp, err := http.Get(page)
- if err != nil {
- }
- defer resp.Body.Close()
- }
- func main() {
- addr := ":81"
- pid := os.Getpid()
- fmt.Println("Starting Server....")
- fmt.Println("addr= " + addr)
- fmt.Println("pid= " + strconv.Itoa(pid))
- r := mux.NewRouter()
- r.HandleFunc("/", CallSync)
- http.Handle("/", r)
- fmt.Println(http.ListenAndServe(addr, nil))
- fmt.Println("Bye")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement