diff --git a/k6_test/grpc/grpc_client.js b/k6_test/grpc/grpc_client.js new file mode 100644 index 0000000..271d2d1 --- /dev/null +++ b/k6_test/grpc/grpc_client.js @@ -0,0 +1,45 @@ +import { check } from "k6"; +import { Counter } from "k6/metrics"; +import grpc from "k6/net/grpc"; + +const grpcReqs = new Counter("grpc_reqs"); + +function getClient(protoFilePath) { + const client = new grpc.Client(); + + client.load([], protoFilePath); + + return client; +} + +function checkResponse(res) { + check(res, { + "status is OK": (r) => r && r.status === grpc.StatusOK, + }); +} + +export class GrpcClient { + constructor(options) { + this.grpcHost = options.grpcHost || 'modelmesh-serving:8033'; + this.client = getClient(options.protoFilePath || '../k6_test/kfs_inference_v2.proto'); + this.inferRPCName = options.inferRPCName || 'inference.GRPCInferenceService/ModelInfer'; + + // Client can't connect on the init context + this.connected = false; + } + + infer(data, params) { + if (!this.connected) { + this.client.connect(this.grpcHost, { plaintext: true }); + this.connected = true; + } + + const res = this.client.invoke(this.inferRPCName, data, params); + checkResponse(res); + grpcReqs.add(1); + } + + close() { + this.client.close(); + } +} \ No newline at end of file diff --git a/k6_test/grpc/script_grpc_skmnist.js b/k6_test/grpc/script_grpc_skmnist.js index ff05a12..ded272d 100644 --- a/k6_test/grpc/script_grpc_skmnist.js +++ b/k6_test/grpc/script_grpc_skmnist.js @@ -1,37 +1,23 @@ -import grpc from 'k6/net/grpc'; -import { check } from 'k6'; -import execution from 'k6/execution'; +import { GrpcClient } from "../k6_test/grpc/grpc_client.js"; {{k6_opts}} -const client = new grpc.Client(); -client.load([], '../k6_test/kfs_inference_v2.proto'); +const sharedClient = new GrpcClient({ + grpcHost: '{{base_url}}' +}); const inputsData = JSON.parse(open(`../k6_test/payloads/{{payload}}`)); let params = { tags: { model_name: `{{model_name}}` }, } -export function setup(){ -// Abort on connection errors - try { - client.connect('{{base_url}}', { plaintext: true}); - } catch (error) { - check(error, {"Setup error": (error) => error === null}) - execution.test.abort(error); - } -} - export default () => { - client.connect('{{base_url}}', { plaintext: true }); const data = { "model_name": "{{model_name}}", "inputs": inputsData["inputs"] }; - const response = client.invoke('inference.GRPCInferenceService/ModelInfer', data, params); - - check(response, { - 'status is OK': (response) => response && response.status === grpc.StatusOK, - }); + sharedClient.infer(data, params); +}; - client.close(); +export function teardown() { + sharedClient.close(); };