Commit d85b32ab authored by Weixin Deng's avatar Weixin Deng
Browse files

Add wait group to send rpcs at the same time

parent 8ea12c09
......@@ -12,9 +12,9 @@ import (
"time"
)
func Assert(assert bool, msg string) {
func Assert(assert bool, msg ...any) {
if !assert {
log.Fatal(msg)
log.Fatal(msg...)
}
}
......@@ -117,19 +117,22 @@ type Client struct {
addr string
}
func (c *Client) SendRequest(request Request, clock *Clock, wg *sync.WaitGroup) {
defer wg.Done()
func (c *Client) SendRequest(
request Request,
clock *Clock,
wgInit *sync.WaitGroup,
wgStart *sync.WaitGroup,
wgFinish *sync.WaitGroup) {
defer wgFinish.Done()
client, err := rpc.Dial("tcp", request.dstAddr)
if err != nil {
log.Fatal("[Client error] ", err)
}
Assert(err == nil, "[Client error] ", err)
defer client.Close()
var reply Reply
wgInit.Done()
wgStart.Wait()
clock.Record()
err = client.Call("Server.HandleRequest", request, &reply)
if err != nil {
log.Fatal("[Client error] ", err)
}
Assert(err == nil, "[Client error] ", err)
clock.Stop()
}
......@@ -144,9 +147,7 @@ func (s *Server) Start() {
s.server = rpc.NewServer()
s.server.Register(s)
listener, err := net.Listen("tcp", s.addr)
if err != nil {
log.Fatal("[Server error] ", err)
}
Assert(err == nil, "[Server error] ", err)
s.listener = listener
maxSize := 1 << 20
for i := 0; i < maxSize; i++ {
......@@ -180,13 +181,19 @@ func startClient(args *Args) {
ServerDelay: time.Duration(args.serverDelay) * time.Microsecond,
}
clocks := make([]Clock, args.numServers)
var wg sync.WaitGroup
var wgInit sync.WaitGroup
var wgStart sync.WaitGroup
var wgFinish sync.WaitGroup
wgStart.Add(1)
for i := 0; i < args.numServers; i++ {
wg.Add(1)
wgInit.Add(1)
wgFinish.Add(1)
request.dstAddr = args.servers[i]
go client.SendRequest(request, &clocks[i], &wg)
go client.SendRequest(request, &clocks[i], &wgInit, &wgStart, &wgFinish)
}
wg.Wait()
wgInit.Wait()
wgStart.Done()
wgFinish.Wait()
var start time.Time
var end time.Time
var delayAvg time.Duration
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment