blob: e0191d0535a6e6b20802ecb9c0ee5d352bd6796f [file] [log] [blame]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001package benchmark
2
3import (
4 "io"
5 "testing"
6
Matt Rosencrantzdbc1be22015-02-28 15:15:49 -08007 "v.io/x/ref/profiles/internal/ipc/stream/manager"
Jungho Ahnaf9dce02014-12-11 18:42:38 -08008
Jiri Simsa6ac95222015-02-23 16:11:49 -08009 "v.io/v23/naming"
10 "v.io/v23/options"
Matt Rosencrantzdbc1be22015-02-28 15:15:49 -080011 "v.io/x/ref/profiles/internal/ipc/stream"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070012)
13
14const (
15 // Shorthands
Asim Shankarcc044212014-10-15 23:25:26 -070016 securityNone = options.VCSecurityNone
17 securityTLS = options.VCSecurityConfidential
Jiri Simsa5293dcb2014-05-10 09:56:38 -070018)
19
20type listener struct {
21 ln stream.Listener
22 ep naming.Endpoint
23}
24
25// createListeners returns N (stream.Listener, naming.Endpoint) pairs, such
26// that calling stream.Manager.Dial to each of the endpoints will end up
27// creating a new VIF.
Asim Shankarcc044212014-10-15 23:25:26 -070028func createListeners(mode options.VCSecurityLevel, m stream.Manager, N int) (servers []listener, err error) {
Jiri Simsa5293dcb2014-05-10 09:56:38 -070029 for i := 0; i < N; i++ {
30 var l listener
Asim Shankar7cf29002014-10-09 00:38:37 -070031 if l.ln, l.ep, err = m.Listen("tcp", "127.0.0.1:0", mode); err != nil {
Jiri Simsa5293dcb2014-05-10 09:56:38 -070032 return
33 }
34 servers = append(servers, l)
35 }
36 return
37}
38
Asim Shankarcc044212014-10-15 23:25:26 -070039func benchmarkFlow(b *testing.B, mode options.VCSecurityLevel, nVIFs, nVCsPerVIF, nFlowsPerVC int) {
Jiri Simsa5293dcb2014-05-10 09:56:38 -070040 client := manager.InternalNew(naming.FixedRoutingID(0xcccccccc))
41 server := manager.InternalNew(naming.FixedRoutingID(0x55555555))
42
43 lns, err := createListeners(mode, server, nVIFs)
44 if err != nil {
45 b.Fatal(err)
46 }
47
48 nFlows := nVIFs * nVCsPerVIF * nFlowsPerVC
49 rchan := make(chan io.ReadCloser, nFlows)
50 wchan := make(chan io.WriteCloser, nFlows)
51
52 go func() {
53 defer close(wchan)
54 for i := 0; i < nVIFs; i++ {
55 ep := lns[i].ep
56 for j := 0; j < nVCsPerVIF; j++ {
Asim Shankar7cf29002014-10-09 00:38:37 -070057 vc, err := client.Dial(ep, mode)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070058 if err != nil {
59 b.Error(err)
60 return
61 }
62 for k := 0; k < nFlowsPerVC; k++ {
63 flow, err := vc.Connect()
64 if err != nil {
65 b.Error(err)
66 return
67 }
68 // Flows are "Accepted" by the remote
69 // end only on the first Write.
70 if _, err := flow.Write([]byte("hello")); err != nil {
71 b.Error(err)
72 return
73 }
74 wchan <- flow
75 }
76 }
77 }
78 }()
79
80 go func() {
81 defer close(rchan)
82 for i := 0; i < nVIFs; i++ {
83 ln := lns[i].ln
84 nFlowsPerVIF := nVCsPerVIF * nFlowsPerVC
85 for j := 0; j < nFlowsPerVIF; j++ {
86 flow, err := ln.Accept()
87 if err != nil {
88 b.Error(err)
89 return
90 }
91 rchan <- flow
92 }
93 }
94 }()
95
96 var readers []io.ReadCloser
97 for r := range rchan {
98 readers = append(readers, r)
99 }
100 var writers []io.WriteCloser
101 for w := range wchan {
102 writers = append(writers, w)
103 }
104 if b.Failed() {
105 return
106 }
107 (&throughputTester{b: b, readers: readers, writers: writers}).Run()
108}