@@ -66,67 +66,61 @@ export default () => {
66
66
<div class =" code-group " data-props =' {"labels": ["Different TLS settings"], "lineNumbers": [true]} ' >
67
67
68
68
``` javascript
69
- import grpc from ' k6/experimental/grpc' ;
70
- import { check } from ' k6' ;
69
+ import grpc from " k6/experimental/grpc" ;
70
+ import { check } from " k6" ;
71
+ import { SharedArray } from " k6/data" ;
72
+ import exec from " k6/execution" ;
71
73
72
- // note: the services in this example don't exist. If you would like
74
+ // note: the services in this example don't exist. If you would like
73
75
// to run this example, make sure to replace the URLs, and
74
76
// the cacerts, cert, key, and password variables.
75
- const params = {
76
- ' foo1.grpcbin.test.k6.io:9001' : {
77
- plaintext: false ,
78
- tls: {
79
- cacerts: [open (' cacerts0.pem' )],
80
- cert: open (' cert0.pem' ),
81
- key: open (' key0.pem' ),
82
- },
83
- },
84
- ' foo2.grpcbin.test.k6.io:9002' : {
85
- plaintext: false ,
86
- tls: {
87
- cacerts: open (' cacerts1.pem' ),
88
- cert: open (' cert1.pem' ),
89
- key: open (' key1.pem' ),
90
- password: ' cert1-passphrase' ,
91
- },
92
- },
93
- };
94
- const clients = {
95
- ' foo1.grpcbin.test.k6.io:9001' : new grpc.Client (),
96
- ' foo2.grpcbin.test.k6.io:9002' : new grpc.Client (),
97
- };
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 ();
98
107
99
108
export default () => {
100
- if (__ITER === 0 ) {
101
- clients[' foo1.grpcbin.test.k6.io:9001' ].connect (
102
- ' foo1.grpcbin.test.k6.io:9001' ,
103
- params[' foo1.grpcbin.test.k6.io:9001' ]
104
- );
105
- clients[' foo2.grpcbin.test.k6.io:9002' ].connect (
106
- ' foo2.grpcbin.test.k6.io:9002' ,
107
- params[' foo2.grpcbin.test.k6.io:9002' ]
108
- );
109
- }
110
-
111
- const response1 = clients[' foo1.grpcbin.test.k6.io:9001' ].invoke (' hello.HelloService/SayHello' , {
112
- greeting: ' Bert' ,
113
- });
114
-
115
- check (response1, {
116
- ' status is OK ' : (r ) => r && r .status === grpc .StatusOK ,
117
- });
118
-
119
- console .log (JSON .stringify (response1 .message ));
120
-
121
- const response2 = clients[' foo2.grpcbin.test.k6.io:9002' ].invoke (' hello.HelloService/SayHello' , {
122
- greeting: ' Ernie' ,
123
- });
124
-
125
- check (response2, {
126
- ' status is OK ' : (r ) => r && r .status === grpc .StatusOK ,
127
- });
128
-
129
- 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 ));
130
124
};
131
125
```
132
126
</div >
0 commit comments