Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | package benchmark |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "testing" |
| 6 | |
Matt Rosencrantz | dbc1be2 | 2015-02-28 15:15:49 -0800 | [diff] [blame] | 7 | "v.io/x/ref/profiles/internal/ipc/stream/manager" |
Jungho Ahn | af9dce0 | 2014-12-11 18:42:38 -0800 | [diff] [blame] | 8 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 9 | "v.io/v23/naming" |
| 10 | "v.io/v23/options" |
Matt Rosencrantz | dbc1be2 | 2015-02-28 15:15:49 -0800 | [diff] [blame] | 11 | "v.io/x/ref/profiles/internal/ipc/stream" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | const ( |
| 15 | // Shorthands |
Asim Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 16 | securityNone = options.VCSecurityNone |
| 17 | securityTLS = options.VCSecurityConfidential |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 18 | ) |
| 19 | |
| 20 | type 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 Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 28 | func createListeners(mode options.VCSecurityLevel, m stream.Manager, N int) (servers []listener, err error) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 29 | for i := 0; i < N; i++ { |
| 30 | var l listener |
Asim Shankar | 7cf2900 | 2014-10-09 00:38:37 -0700 | [diff] [blame] | 31 | if l.ln, l.ep, err = m.Listen("tcp", "127.0.0.1:0", mode); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 32 | return |
| 33 | } |
| 34 | servers = append(servers, l) |
| 35 | } |
| 36 | return |
| 37 | } |
| 38 | |
Asim Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 39 | func benchmarkFlow(b *testing.B, mode options.VCSecurityLevel, nVIFs, nVCsPerVIF, nFlowsPerVC int) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 40 | 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 Shankar | 7cf2900 | 2014-10-09 00:38:37 -0700 | [diff] [blame] | 57 | vc, err := client.Dial(ep, mode) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 58 | 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 | } |