Created
October 15, 2012 18:32
-
-
Save jasonmoo/3894201 to your computer and use it in GitHub Desktop.
Go rpc client over tcp
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 ( | |
"log" | |
"net" | |
"net/rpc" | |
"sync" | |
"time" | |
) | |
func main() { | |
reqs := 100000 | |
address, err := net.ResolveTCPAddr("tcp", "localhost:8080") | |
if err != nil { | |
log.Println(err) | |
log.Fatal("Unable to resolve address") | |
} | |
conn, _ := net.DialTCP("tcp", nil, address) | |
defer conn.Close() | |
client := rpc.NewClient(conn) | |
defer client.Close() | |
w := new(sync.WaitGroup) | |
w.Add(reqs) | |
start := time.Now() | |
for i := 0; i < reqs; i++ { | |
go func() { | |
var reply string | |
err := client.Call("System.Ping", "ping", &reply) | |
if err != nil { | |
log.Fatal("ping error:", err) | |
} | |
if reply != "pong" { | |
log.Println("ping error: did not receive pong") | |
} | |
// log.Println(reply) | |
w.Done() | |
}() | |
} | |
w.Wait() | |
run_time := time.Since(start) | |
log.Printf("total time: %s\n", run_time) | |
log.Printf("reqs/sec: %.0f\n", float64(reqs)/float64(run_time.Seconds())) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment