Skip to content

Instantly share code, notes, and snippets.

@nono
Created June 27, 2011 10:53
Show Gist options
  • Select an option

  • Save nono/1048668 to your computer and use it in GitHub Desktop.

Select an option

Save nono/1048668 to your computer and use it in GitHub Desktop.
Stressing my long-polling server
package main
import (
"fmt"
"http"
"net"
"os"
"os/signal"
"runtime"
"strconv"
)
var rawUrl string = "http://localhost:8000/"
func Connect() {
for {
url, err := http.ParseURL(rawUrl)
if err != nil {
fmt.Println(err)
return
}
var req http.Request
req.Method = "GET"
req.Header = http.Header{}
req.URL = url
dial, err := net.Dial("tcp", url.Host)
if err != nil {
fmt.Println(err)
return
}
conn := http.NewClientConn(dial, nil)
conn.Do(&req)
conn.Close()
}
}
func InstallSignalHandler() {
for s := range signal.Incoming {
if s == os.SIGINT {
fmt.Printf("\nCtrl-C signalled\n")
os.Exit(0)
}
}
}
func main() {
nbConns := 1000
if len(os.Args) > 1 {
c, err := strconv.Btoi64(os.Args[1], 0)
if err != nil {
fmt.Println(err)
return
}
nbConns = int(c)
}
fmt.Printf("Opening %d connections\n", nbConns)
for i := 0; i < nbConns; i++ {
go Connect()
runtime.Gosched()
}
InstallSignalHandler()
}
include $(GOROOT)/src/Make.inc
TARG=go-bench
GOFILES=go-bench.go
include $(GOROOT)/src/Make.cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment