Last active
September 11, 2020 17:53
-
-
Save hunter32292/f5c9e4077213b58a41727fc1a1a34a61 to your computer and use it in GitHub Desktop.
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" | |
"log" | |
"math/rand" | |
"net/http" | |
"time" | |
"github.com/gorilla/mux" | |
) | |
var ( | |
count int | |
serverAddr string | |
targetAddr string | |
) | |
func main() { | |
flag.StringVar(&serverAddr, | |
"serverAddr", | |
"localhost:8081", | |
"Application host address") | |
flag.StringVar(&targetAddr, | |
"targetAddr", | |
"localhost:8080", | |
"The target server address") | |
flag.Parse() | |
handler := mux.NewRouter() | |
handler.HandleFunc("/start", start) | |
handler.HandleFunc("/hit", hit) | |
http.Handle("/", handler) | |
log.Fatal(http.ListenAndServe(serverAddr, handler)) | |
} | |
func start(w http.ResponseWriter, r *http.Request) { | |
go caller() // Send off on a thread to be finished | |
return | |
} | |
func caller() { | |
protocol := "http://" | |
endpoint := "/hit" | |
method := "GET" | |
url := protocol + targetAddr + endpoint | |
client := &http.Client{} | |
for { | |
rand.Seed(time.Now().UnixNano()) | |
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000)+100)) | |
count++ | |
req, err := http.NewRequest(method, url, nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
_, err = client.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Println("Count: ", count) | |
} | |
} | |
func hit(w http.ResponseWriter, r *http.Request) { | |
log.Println("HIT!") | |
w.WriteHeader(http.StatusOK) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment