Skip to content

Instantly share code, notes, and snippets.

View un4ckn0wl3z's full-sized avatar
🎯
Complex solutions may seem impressive, but simplicity often leads to success.

Anuwat Khongchuai un4ckn0wl3z

🎯
Complex solutions may seem impressive, but simplicity often leads to success.
View GitHub Profile
#!/bin/bash
# Define the Kafka broker and topic
BROKER_LIST="localhost:9092"
TOPIC="mfaf.exampleTopic"
# Loop to simulate load test
while true; do
# Generate some random JSON data and save it to a file
# echo "{\"key\": \"value\"}" > m.json
{"header":{"version":"1.0","timestamp":"xxxx","orgService":"xxxx","tid":"xxxx","lastTid":"xxxx","session":"xxxx","transaction":"xxxx","communication":"unicast|multicast|broadcast","groupTags":[],"identity":{"device":"xxxxx","public":"xxxxx","user":"xxxxx"},"tmfSpec":"none|TMFxxx","baseApiVersion":"none|4.0.0|4.1.0|5.0.0","schemaVersion":"1.0.0"},"body":{"example":"xxx","@referredType":"1111"}}
#!/bin/bash
# Set the parameters
bootstrap_server="localhost:9092"
topic="mfaf.exampleTopic"
messages_per_iteration=100
total_iterations=10
# JSON message template
json_template='{
^ ?(.+)$
"$1",
function timeToDecimal(timeString) {
var timeParts = timeString.split(':');
var hours = parseInt(timeParts[0]);
var minutes = parseInt(timeParts[1]) || 0;
var seconds = parseInt(timeParts[2]) || 0;
var decimalHours = hours + (minutes / 60) + (seconds / 3600);
return decimalHours;
}
function formatDate(dateString) {
const date = new Date(dateString);
const options = {
day: '2-digit',
month: '2-digit',
year: 'numeric',
timeZone: 'Asia/Bangkok',
//timeZoneName: 'short'
};
// kafka-consumer.service.ts
import { Injectable } from '@nestjs/common';
import { Kafka, Consumer, EachMessagePayload } from 'kafkajs';
@Injectable()
export class KafkaConsumerService {
private kafkaConsumer: Consumer;
private isConsuming: boolean;
constructor() {
// kafka-producer.service.ts
import { Injectable } from '@nestjs/common';
import { Kafka } from 'kafkajs';
@Injectable()
export class KafkaProducerService {
private kafkaProducer;
constructor() {
this.kafkaProducer = new Kafka({
const fs = require('fs');
function readFilePromise(fileName, encoding, cb) {
fs.readFile(fileName, encoding, cb);
}
async function run() {
try {
const data = await (async () => {
return new Promise(async (resolve) => {
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Kafka, Consumer, Producer, EachMessagePayload } from 'kafkajs';
import { removePathTraversalCharacters } from 'src/app.module';
@Injectable()
export class KafkaService implements OnModuleInit, OnModuleDestroy {
private kafka: Kafka;
private producer: Producer;
private consumer: Consumer;