Created
August 27, 2013 11:05
-
-
Save choplin/6352225 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
| "net" | |
| "fmt" | |
| "runtime" | |
| "sync" | |
| "time" | |
| ) | |
| func ping(times int, wg *sync.WaitGroup) { | |
| tcpAddr, _ := net.ResolveTCPAddr("tcp4", "localhost:1201") | |
| conn, _ := net.DialTCP("tcp", nil, tcpAddr) | |
| for i := 0; i < times; i++ { | |
| _, _ = conn.Write([]byte("Ping")) | |
| var buf [4]byte | |
| _, _ = conn.Read(buf[0:]) | |
| } | |
| wg.Done() | |
| conn.Close() | |
| } | |
| func main() { | |
| runtime.GOMAXPROCS(8) | |
| totalPing := 1000000 | |
| concurrentConnection := 10 | |
| pingsPerConnection := totalPing / concurrentConnection | |
| actualTotalPings := pingsPerConnection *concurrentConnection | |
| var wg sync.WaitGroup | |
| start := time.Now() | |
| for i:=0; i<concurrentConnection; i++ { | |
| wg.Add(1) | |
| go ping(pingsPerConnection, &wg) | |
| } | |
| wg.Wait() | |
| elapsed := 1000000 * time.Since(start).Seconds() | |
| fmt.Println(elapsed / float64(actualTotalPings)) | |
| } |
This file contains hidden or 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 ( | |
| "net" | |
| "runtime" | |
| ) | |
| func handleClient(conn net.Conn) { | |
| defer conn.Close() | |
| var buf [4]byte | |
| for { | |
| n, err := conn.Read(buf[0:]) | |
| if err != nil {return} | |
| if n > 0 { | |
| _, err := conn.Write([]byte("Pong")) | |
| if err != nil {return} | |
| } | |
| } | |
| } | |
| func main() { | |
| runtime.GOMAXPROCS(8) | |
| tcpAddr, _ := net.ResolveTCPAddr("tcp4", ":1201") | |
| listener, _ := net.ListenTCP("tcp", tcpAddr) | |
| for { | |
| conn, _ := listener.Accept() | |
| go handleClient(conn) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment