sourabh_sourabh | 6041e3f | 2024-09-30 11:13:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * Copyright (C) 2024 Nordix Foundation |
| 4 | * ================================================================================ |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | * SPDX-License-Identifier: Apache-2.0 |
| 18 | * ============LICENSE_END========================================================= |
| 19 | */ |
| 20 | |
| 21 | import { crypto } from 'k6/experimental/webcrypto'; |
| 22 | import { check } from 'k6'; |
| 23 | import { Writer, SchemaRegistry, SCHEMA_TYPE_STRING } from 'k6/x/kafka'; |
| 24 | |
| 25 | const testEventPayload = JSON.stringify(JSON.parse(open('../../resources/sampleAvcInputEvent.json'))); |
| 26 | const schemaRegistry = new SchemaRegistry(); |
| 27 | const kafkaProducer = new Writer({ |
| 28 | brokers: ['localhost:9092'], |
| 29 | topic: 'dmi-cm-events', |
| 30 | autoCreateTopic: true, |
| 31 | batchSize: 5000, |
| 32 | compression: 'gzip', |
| 33 | requestTimeout: 30000 |
| 34 | }); |
| 35 | |
| 36 | const TOTAL_MESSAGES = 100000; |
| 37 | const VIRTUAL_USERS = 1000; |
| 38 | |
| 39 | export const options = { |
| 40 | setupTimeout: '1m', |
| 41 | teardownTimeout: '1m', |
| 42 | scenarios: { |
| 43 | produceKafkaMessages: { |
| 44 | executor: 'shared-iterations', |
| 45 | exec: 'sendKafkaMessages', |
| 46 | vus: VIRTUAL_USERS, |
| 47 | iterations: TOTAL_MESSAGES, |
| 48 | maxDuration: '10m', |
| 49 | } |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | function getCloudEventHeaders() { |
| 54 | return { |
| 55 | ce_type: 'org.onap.cps.ncmp.events.avc1_0_0.AvcEvent', |
| 56 | ce_source: 'DMI', |
| 57 | ce_destination: 'dmi-cm-events', |
| 58 | ce_specversion: '1.0', |
| 59 | ce_time: new Date().toISOString(), |
| 60 | ce_id: crypto.randomUUID(), |
| 61 | ce_dataschema: 'urn:cps:org.onap.cps.ncmp.events.avc1_0_0.AvcEvent:1.0.0', |
| 62 | ce_correlationid: crypto.randomUUID() |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | export function sendKafkaMessages() { |
| 67 | const cloudEventHeaders = getCloudEventHeaders(); |
| 68 | |
| 69 | const avcCloudEvent = { |
| 70 | key: schemaRegistry.serialize({ |
| 71 | data: cloudEventHeaders.ce_correlationid, |
| 72 | schemaType: SCHEMA_TYPE_STRING, |
| 73 | }), |
| 74 | value: schemaRegistry.serialize({ |
| 75 | data: testEventPayload, |
| 76 | schemaType: SCHEMA_TYPE_STRING |
| 77 | }), |
| 78 | headers: cloudEventHeaders |
| 79 | }; |
| 80 | |
| 81 | try { |
| 82 | kafkaProducer.produce({ messages: [avcCloudEvent] }); |
| 83 | |
| 84 | const isMessageSent = check(kafkaProducer, { |
| 85 | 'Message sent successfully': (producer) => producer != null, |
| 86 | }); |
| 87 | |
| 88 | if (!isMessageSent) { |
| 89 | console.error('Failed to send message:', avcCloudEvent); |
| 90 | } |
| 91 | |
| 92 | } catch (error) { |
| 93 | console.error('Error during message production:', error, avcCloudEvent); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | export function teardown() { |
| 98 | kafkaProducer.close(); |
| 99 | } |