Skip to content

Instantly share code, notes, and snippets.

@wegrata
Created July 25, 2013 12:01
Show Gist options
  • Save wegrata/6079008 to your computer and use it in GitHub Desktop.
Save wegrata/6079008 to your computer and use it in GitHub Desktop.
package main
import(
"log"
"math/big"
"crypto/rand"
"sync"
"net/http"
"net/url"
"strings"
"io/ioutil"
"time"
"fmt"
)
type AggregateResults struct{
Success int
Fail int
Total int
AverageDuration time.Duration
}
func (this AggregateResults) String() string{
return fmt.Sprintf("Success: %d\nFail: %d\nAverage Time: %fs", this.Success, this.Fail, this.AverageDuration.Seconds())
}
type Result struct {
Url string
Status int
Length time.Duration
}
func (this Result) String() string{
return fmt.Sprintf("%s took %fs with status %d", this.Url, this.Length.Seconds(), this.Status, )
}
type InMemoryCookieJar struct{
storage map[string][]http.Cookie
}
// buggy... but works
func (jar InMemoryCookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
for _, ck := range cookies {
path := ck.Domain
if ck.Path != "/" {
path = ck.Domain + ck.Path
}
if _, found := jar.storage[path]; found {
setted := false
for i, v := range jar.storage[path] {
if v.Name == ck.Name {
jar.storage[path][i] = *ck
setted = true
break
}
}
if ! setted {
jar.storage[path] = append(jar.storage[path], *ck)
}
} else {
jar.storage[path] = []http.Cookie{*ck}
}
}
}
func (jar InMemoryCookieJar) Cookies(u *url.URL) []*http.Cookie {
cks := []*http.Cookie{}
for pattern, cookies := range jar.storage {
if strings.Contains(u.String(), pattern) {
for i := range cookies {
cks = append(cks, &cookies[i])
}
}
}
return cks
}
func newCookieJar() http.CookieJar{
storage := make(map[string][]http.Cookie)
return &InMemoryCookieJar{storage}
}
func SendRequest(u string, jar http.CookieJar, results chan Result) {
client := http.Client{Jar: jar}
start := time.Now()
resp, e := client.Get(u)
if e == nil{
results <- Result{Status: resp.StatusCode, Length: time.Since(start), Url: u}
}else{
results <- Result{Status: 500, Length: time.Since(start), Url: u}
}
}
func DisplayResults(results chan Result, wg *sync.WaitGroup, ar *AggregateResults){
var timeTotal time.Duration
c := 0
for r := range results {
ar.Total++
c++
if r.Status >= 500 {
ar.Fail++
}else{
ar.Success++
}
timeTotal += r.Length
ar.AverageDuration = time.Duration(timeTotal / time.Duration(c))
log.Println(r)
wg.Done()
}
}
func main(){
jar := newCookieJar()
client := http.Client{Jar: jar}
_, e := client.PostForm("http://roundtriptest.azurewebsites.net",
url.Values{"UserName": {"groupadmin@lem.com"}, "Password": {"password"}})
if e != nil{
log.Println(e)
}else{
resultsChannel := make(chan Result)
requests := 5
var wg sync.WaitGroup
var totals AggregateResults
wg.Add(requests)
u := "http://roundtriptest.azurewebsites.net/Path/Index"
go DisplayResults(resultsChannel, &wg, &totals)
for i := 0; i < requests; i++ {
go func(){
delay, _ := rand.Int(rand.Reader, big.NewInt(20))
<- time.After(time.Duration(delay.Uint64()) * time.Second)
SendRequest(u, jar, resultsChannel)
}()
}
wg.Wait()
ioutil.WriteFile("results.txt", []byte(totals.String()), 0644)
log.Println(totals)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment