func (sg *SpdyGun) Ping() { if sg.client == nil { return } pingStart := time.Now() pinged, err := sg.client.Ping(time.Second * 15) if err != nil { log.Printf("client: ping: %s\n", err) } if !pinged { log.Printf("client: ping: timed out\n") } ss := aggregate.AcquireSample(float64(pingStart.UnixNano())/1e9, "PING") ss.RT = int(time.Since(pingStart).Seconds() * 1e6) if err == nil && pinged { ss.ProtoCode = 200 } else { ss.Err = err ss.ProtoCode = 500 } sg.results <- ss if err != nil { sg.Connect() } }
func (sg *SpdyGun) Connect() error { // FIXME: rewrite connection logic, it isn't thread safe right now. start := time.Now() ss := aggregate.AcquireSample(float64(start.UnixNano())/1e9, "CONNECT") defer func() { ss.RT = int(time.Since(start).Seconds() * 1e6) sg.results <- ss }() config := tls.Config{ InsecureSkipVerify: true, NextProtos: []string{"spdy/3.1"}, } conn, err := tls.Dial("tcp", sg.target, &config) if err != nil { ss.Err = err ss.NetCode = 999 return err } client, err := spdy.NewClientConn(conn) if err != nil { ss.Err = err ss.NetCode = 999 return err } else { ss.ProtoCode = 200 } if sg.client != nil { sg.Close() } sg.client = client return nil }
// Shoot to target, this method is not thread safe func (hg *HttpGun) Shoot(ctx context.Context, a ammo.Ammo) error { if hg.client == nil { hg.Connect() } start := time.Now() ss := aggregate.AcquireSample(float64(start.UnixNano())/1e9, "REQUEST") defer func() { ss.RT = int(time.Since(start).Seconds() * 1e6) hg.results <- ss }() // now send the request to obtain a http response ha, ok := a.(*ammo.Http) if !ok { panic(fmt.Sprintf("Got '%T' instead of 'HttpAmmo'", a)) } if ha.Tag != "" { ss.Tag += "|" + ha.Tag } var uri string if hg.ssl { uri = "https://" + ha.Host + ha.Uri } else { uri = "http://" + ha.Host + ha.Uri } req, err := http.NewRequest(ha.Method, uri, nil) if err != nil { log.Printf("Error making HTTP request: %s\n", err) ss.Err = err ss.NetCode = 999 return err } for k, v := range ha.Headers { req.Header.Set(k, v) } req.URL.Host = hg.target res, err := hg.client.Do(req) if err != nil { log.Printf("Error performing a request: %s\n", err) ss.Err = err ss.NetCode = 999 return err } defer res.Body.Close() _, err = io.Copy(ioutil.Discard, res.Body) if err != nil { log.Printf("Error reading response body: %s\n", err) ss.Err = err ss.NetCode = 999 return err } // TODO: make this an optional verbose answ_log output //data := make([]byte, int(res.ContentLength)) // _, err = res.Body.(io.Reader).Read(data) // fmt.Println(string(data)) ss.ProtoCode = res.StatusCode return nil }
func (l *LogGun) Shoot(ctx context.Context, a ammo.Ammo) error { log.Println("Log message: ", a.(*ammo.Log).Message) l.results <- aggregate.AcquireSample(float64(time.Now().UnixNano())/1e9, "REQUEST") return nil }