Advertisement
Guest User

Untitled

a guest
Jun 17th, 2015
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.70 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "net/http"
  6.     "os"
  7.     "strconv"
  8.     //"math"
  9.     "time"
  10.     "github.com/gorilla/mux"
  11. )
  12.  
  13. func Async(url string, count int) <-chan int {
  14.     c := make(chan int)
  15.     go func() {
  16.         cnt := 0
  17.         for i := 0; i < count; i++ {
  18.             GetPage(url)
  19.             cnt = cnt + 1
  20.             if (cnt == 50) {
  21.                 c <- i              
  22.                 cnt = 0
  23.             }
  24.         }  
  25.     }()
  26.     return c
  27. }
  28.  
  29. func CallSync(w http.ResponseWriter, r *http.Request) {
  30.     t0 := time.Now()
  31.     c1 := Async("http://localhost:8080/", 2000)
  32.     c2 := Async("http://localhost:8080/", 2000)
  33.     c3 := Async("http://localhost:8080/", 2000)
  34.     c4 := Async("http://localhost:8080/", 2000)
  35.     c5 := Async("http://localhost:8080/", 2000)
  36.     c6 := Async("http://localhost:8080/", 2000)
  37.     c7 := Async("http://localhost:8080/", 2000)
  38.     c8 := Async("http://localhost:8080/", 2000)
  39.     c9 := Async("http://localhost:8080/", 2000)
  40.     c10 := Async("http://localhost:8080/", 2000)
  41.     c11 := Async("http://localhost:8080/", 2000)
  42.     c12 := Async("http://localhost:8080/", 2000)
  43.     c13 := Async("http://localhost:8080/", 2000)
  44.     c14 := Async("http://localhost:8080/", 2000)
  45.     c15 := Async("http://localhost:8080/", 2000)
  46.     c16 := Async("http://localhost:8080/", 2000)
  47.     c17 := Async("http://localhost:8080/", 2000)
  48.     c18 := Async("http://localhost:8080/", 2000)
  49.     c19 := Async("http://localhost:8080/", 2000)
  50.     c20 := Async("http://localhost:8080/", 2000)
  51.     count := 0
  52.     for count < 4000 {
  53.         select {
  54.             case v1 := <-c1:
  55.                 count = count + 50
  56.                 fmt.Printf("%v c1\n", v1)
  57.             case v2 := <-c2:
  58.                 count = count + 50
  59.                 fmt.Printf("%v c2\n", v2)
  60.             case v3 := <-c3:
  61.                 count = count + 50
  62.                 fmt.Printf("%v c3\n", v3)
  63.             case v4 := <-c4:
  64.                 count = count + 50
  65.                 fmt.Printf("%v c4\n", v4)
  66.             case v5 := <-c5:
  67.                 count = count + 50
  68.                 fmt.Printf("%v c5\n", v5)
  69.             case v6 := <-c6:
  70.                 count = count + 50         
  71.                 fmt.Printf("%v c6\n", v6)
  72.             case v7 := <-c7:
  73.                 count = count + 50         
  74.                 fmt.Printf("%v c7\n", v7)
  75.             case v8 := <-c8:
  76.                 count = count + 50         
  77.                 fmt.Printf("%v c8\n", v8)
  78.             case v9 := <-c9:
  79.                 count = count + 50         
  80.                 fmt.Printf("%v c9\n", v9)
  81.             case v10 := <-c10:
  82.                 count = count + 50             
  83.                 fmt.Printf("%v c10\n", v10)
  84.             case v11 := <-c11:
  85.                 count = count + 50
  86.                 fmt.Printf("%v c11\n", v11)
  87.             case v12 := <-c12:
  88.                 count = count + 50             
  89.                 fmt.Printf("%v c12\n", v12)
  90.             case v13 := <-c13:
  91.                 count = count + 50             
  92.                 fmt.Printf("%v c13\n", v13)
  93.             case v14 := <-c14:
  94.                 count = count + 50         
  95.                 fmt.Printf("%v c14\n", v14)
  96.             case v15 := <-c15:
  97.                 count = count + 50     
  98.                 fmt.Printf("%v c15\n", v15)
  99.             case v16 := <-c16:
  100.                 count = count + 50             
  101.                 fmt.Printf("%v c16\n", v16)
  102.             case v17 := <-c17:
  103.                 count = count + 50             
  104.                 fmt.Printf("%v c17\n", v17)
  105.             case v18 := <-c18:
  106.                 count = count + 50         
  107.                 fmt.Printf("%v c18\n", v18)
  108.             case v19 := <-c19:
  109.                 count = count + 50         
  110.                 fmt.Printf("%v c19\n", v19)
  111.             case v20 := <-c20:
  112.                 count = count + 50         
  113.                 fmt.Printf("%v c20\n", v20)
  114.             default:     
  115.         }
  116.     }
  117.     t1 := time.Now()
  118.     diff := t1.Sub(t0)
  119.     num := diff.Nanoseconds() / int64(time.Millisecond)
  120.     msec := float64(num) / 1000
  121.     reqsec := float64(count) / msec
  122.     fmt.Fprintf(w, "%v requests\n", count)
  123.     fmt.Fprintf(w, "Performed in %v\n", diff)
  124.     fmt.Fprintf(w, "At a rate of %v (requests/sec)\n", reqsec)
  125. }
  126.  
  127. func GetPage(page string) {
  128.     resp, err := http.Get(page)
  129.     if err != nil {
  130.     }  
  131.     defer resp.Body.Close()
  132. }
  133.  
  134. func main() {
  135.     addr := ":81"
  136.     pid := os.Getpid()
  137.     fmt.Println("Starting Server....")
  138.     fmt.Println("addr= " + addr)
  139.     fmt.Println("pid= " + strconv.Itoa(pid))
  140.     r := mux.NewRouter()
  141.     r.HandleFunc("/", CallSync)
  142.     http.Handle("/", r)
  143.     fmt.Println(http.ListenAndServe(addr, nil))
  144.     fmt.Println("Bye")
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement