@@ -66,65 +66,61 @@ export default () => {
6666<div class =" code-group " data-props =' {"labels": ["Different TLS settings"], "lineNumbers": [true]} ' >
6767
6868``` javascript
69- import grpc from ' k6/net/grpc' ;
70- import { check } from ' k6' ;
71-
72- // note: there are no such services
73- const params = {
74- ' foo1.grpcbin.test.k6.io:9001' : {
75- plaintext: false ,
76- tls: {
77- cacerts: [open (' cacerts0.pem' )],
78- cert: open (' cert0.pem' ),
79- key: open (' key0.pem' ),
80- },
81- },
82- ' foo2.grpcbin.test.k6.io:9002' : {
83- plaintext: false ,
84- tls: {
85- cacerts: open (' cacerts1.pem' ),
86- cert: open (' cert1.pem' ),
87- key: open (' key1.pem' ),
88- password: ' cert1-passphrase' ,
89- },
90- },
91- };
92- const clients = {
93- ' foo1.grpcbin.test.k6.io:9001' : new grpc.Client (),
94- ' foo2.grpcbin.test.k6.io:9002' : new grpc.Client (),
95- };
69+ import grpc from " k6/experimental/grpc" ;
70+ import { check } from " k6" ;
71+ import { SharedArray } from " k6/data" ;
72+ import exec from " k6/execution" ;
73+
74+ // note: the services in this example don't exist. If you would like
75+ // to run this example, make sure to replace the URLs, and
76+ // the cacerts, cert, key, and password variables.
77+ const grpcArgs = new SharedArray (" grpc" , () => {
78+ // Using SharedArray here so that not every VU gets a copy of every certificate a key
79+ return [
80+ {
81+ host: " foo1.grpcbin.test.k6.io:9001" ,
82+ plaintext: false ,
83+ params: {
84+ tls: {
85+ cacerts: [open (" cacerts0.pem" )],
86+ cert: open (" cert0.pem" ),
87+ key: open (" key0.pem" ),
88+ },
89+ },
90+ },
91+ {
92+ host: " foo2.grpcbin.test.k6.io:9002" ,
93+ params: {
94+ plaintext: false ,
95+ tls: {
96+ cacerts: open (" cacerts1.pem" ),
97+ cert: open (" cert1.pem" ),
98+ key: open (" key1.pem" ),
99+ password: " cert1-passphrase" ,
100+ },
101+ },
102+ },
103+ ];
104+ });
105+
106+ const client = new grpc.Client ();
96107
97108export default () => {
98- if (__ITER === 0 ) {
99- clients[' foo1.grpcbin.test.k6.io:9001' ].connect (
100- ' foo1.grpcbin.test.k6.io:9001' ,
101- params[' foo1.grpcbin.test.k6.io:9001' ]
102- );
103- clients[' foo2.grpcbin.test.k6.io:9002' ].connect (
104- ' foo2.grpcbin.test.k6.io:9002' ,
105- params[' foo2.grpcbin.test.k6.io:9002' ]
106- );
107- }
108-
109- const response1 = clients[' foo1.grpcbin.test.k6.io:9001' ].invoke (' hello.HelloService/SayHello' , {
110- greeting: ' Bert' ,
111- });
112-
113- check (response1, {
114- ' status is OK ' : (r ) => r && r .status === grpc .StatusOK ,
115- });
116-
117- console .log (JSON .stringify (response1 .message ));
118-
119- const response2 = clients[' foo2.grpcbin.test.k6.io:9002' ].invoke (' hello.HelloService/SayHello' , {
120- greeting: ' Ernie' ,
121- });
122-
123- check (response2, {
124- ' status is OK ' : (r ) => r && r .status === grpc .StatusOK ,
125- });
126-
127- console .log (JSON .stringify (response2 .message ));
109+ if (__ITER === 0 ) {
110+ // Take one config and use it for this one VU
111+ let grpcArg = grpcArgs[exec .vu .idInTest % grpcArgs .length ];
112+ client .connect (grpcArg .host , grpcArg .params );
113+ }
114+
115+ const response = client .invoke (" hello.HelloService/SayHello" , {
116+ greeting: " Bert" ,
117+ });
118+
119+ check (response, {
120+ " status is OK " : (r ) => r && r .status === grpc .StatusOK ,
121+ });
122+
123+ console .log (JSON .stringify (response .message ));
128124};
129125```
130126</div >
0 commit comments