Skip to content

Commit 7baff9e

Browse files
oleiademstoykov
andauthored
Update src/data/markdown/docs/02 javascript api/07 k6-experimental/02 grpc/20 Client/20-Client-connect-connect-address-params.md
Co-authored-by: Mihail Stoykov <[email protected]>
1 parent b04ff59 commit 7baff9e

File tree

1 file changed

+50
-56
lines changed

1 file changed

+50
-56
lines changed

src/data/markdown/docs/02 javascript api/07 k6-experimental/02 grpc/20 Client/20-Client-connect-connect-address-params.md

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -66,67 +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/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";
7173

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
7375
// to run this example, make sure to replace the URLs, and
7476
// 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();
98107

99108
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));
130124
};
131125
```
132126
</div>

0 commit comments

Comments
 (0)