Created
April 4, 2012 19:59
-
-
Save kylelemons/2305151 to your computer and use it in GitHub Desktop.
Server and client RPC benchmarks
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 ( | |
"net/rpc" | |
"fmt" | |
"sync" | |
. "testing" | |
) | |
type Struct1 struct { | |
A int | |
B string | |
C Struct2 | |
} | |
type Struct2 struct { | |
A int | |
B int | |
} | |
type Struct3 struct { | |
A string | |
B string | |
C []byte | |
} | |
func main() { | |
client, _ := rpc.Dial("tcp", "localhost:2000") | |
p1 := &Struct1{ | |
A: 10, | |
B: "HelloWorld", | |
C: Struct2{4, 1}, | |
} | |
p2 := &Struct3{} | |
fmt.Println("sequential: ", Benchmark(func(b *B) { | |
for i := 0; i < b.N; i++ { | |
client.Call("Something.Call1", p1, p2) | |
} | |
})) | |
fmt.Println("parallel: ", Benchmark(func(b *B) { | |
var wg sync.WaitGroup | |
wg.Add(b.N) | |
for i := 0; i < b.N; i++ { | |
go func() { | |
defer wg.Done() | |
client.Call("Something.Call1", p1, p2) | |
}() | |
} | |
wg.Wait() | |
})) | |
} |
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 ( | |
"net" | |
"net/rpc" | |
"strconv" | |
) | |
type Struct1 struct { | |
A int | |
B string | |
C Struct2 | |
} | |
type Struct2 struct { | |
A int | |
B int | |
} | |
type Struct3 struct { | |
A string | |
B string | |
C []byte | |
} | |
type Something int | |
func (s *Something) Call1(s1 *Struct1, s3 *Struct3) error { | |
// waste some time | |
s3.A += "42" | |
s3.B += s1.B | |
s3.A += strconv.Itoa(s1.A + s1.C.A + s1.C.B) | |
s3.C = []byte(s3.A + s3.B + s1.B) | |
return nil | |
} | |
func main() { | |
server, _ := net.Listen("tcp", ":2000") | |
something := new(Something) | |
rpc.Register(something) | |
for { | |
client, _ := server.Accept() | |
go rpc.ServeConn(client) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment