From 4368d2489c67d4e54b4b72290d7cf8b1053f2079 Mon Sep 17 00:00:00 2001 From: surendratiwari3 Date: Tue, 6 Feb 2024 08:58:36 +0530 Subject: [PATCH] sonarqube: fix duplicate in bench test --- .../amqpnostorage/consumer/benchmark_test.go | 38 +------------- .../amqpnostorage/publisher/benchmark_test.go | 48 ++---------------- bench/amqpnostorage/setup.go | 49 +++++++++++++++++++ 3 files changed, 54 insertions(+), 81 deletions(-) create mode 100644 bench/amqpnostorage/setup.go diff --git a/bench/amqpnostorage/consumer/benchmark_test.go b/bench/amqpnostorage/consumer/benchmark_test.go index fbd3df5..8e14151 100644 --- a/bench/amqpnostorage/consumer/benchmark_test.go +++ b/bench/amqpnostorage/consumer/benchmark_test.go @@ -1,55 +1,21 @@ package consumer import ( - "context" - "github.com/sirupsen/logrus" - "github.com/surendratiwari3/paota/internal/config" + "github.com/surendratiwari3/paota/bench/amqpnostorage" "github.com/surendratiwari3/paota/internal/logger" "github.com/surendratiwari3/paota/internal/schema" "github.com/surendratiwari3/paota/workerpool" - "os" "testing" "time" ) func BenchmarkAmqpNoStore(b *testing.B) { - workerPool := SetupConsumer() + workerPool := amqpnostorage.SetupWorkerPool() for n := 0; n < b.N; n++ { Consumer(workerPool) } } -func SetupConsumer() workerpool.Pool { - logger.ApplicationLogger = logrus.StandardLogger() - cnf := config.Config{ - Broker: "amqp", - TaskQueueName: "paota_task_queue", - AMQP: &config.AMQPConfig{ - Url: "amqp://localhost:55005/", - Exchange: "paota_task_exchange", - ExchangeType: "direct", - BindingKey: "paota_task_binding_key", - PrefetchCount: 100, - ConnectionPoolSize: 10, - }, - } - err := config.GetConfigProvider().SetApplicationConfig(cnf) - if err != nil { - logger.ApplicationLogger.Error("config error", err) - return nil - } - newWorkerPool, err := workerpool.NewWorkerPool(context.Background(), 10, "testWorker") - if err != nil { - logger.ApplicationLogger.Error("workerPool is not created", err) - os.Exit(0) - } else if newWorkerPool == nil { - logger.ApplicationLogger.Info("workerPool is nil") - os.Exit(0) - } - logger.ApplicationLogger.Info("newWorkerPool created successfully") - return newWorkerPool -} - func Consumer(pool workerpool.Pool) { // Register tasks regTasks := map[string]interface{}{ diff --git a/bench/amqpnostorage/publisher/benchmark_test.go b/bench/amqpnostorage/publisher/benchmark_test.go index 8b68999..80b34a8 100644 --- a/bench/amqpnostorage/publisher/benchmark_test.go +++ b/bench/amqpnostorage/publisher/benchmark_test.go @@ -3,65 +3,23 @@ package publisher import ( "context" "encoding/json" - "github.com/sirupsen/logrus" - "github.com/surendratiwari3/paota/internal/config" - "github.com/surendratiwari3/paota/internal/logger" + "github.com/surendratiwari3/paota/bench/amqpnostorage" "github.com/surendratiwari3/paota/internal/schema" "github.com/surendratiwari3/paota/workerpool" - "os" "testing" ) func BenchmarkAmqpNoStore(b *testing.B) { - workerPool := SetupPublisher() + workerPool := amqpnostorage.SetupWorkerPool() for n := 0; n < b.N; n++ { Publisher(workerPool) } } -// UserRecord represents the structure of user records. -type UserRecord struct { - ID string `json:"id"` - Name string `json:"name"` - Email string `json:"email"` - // Add other fields as needed -} - -func SetupPublisher() workerpool.Pool { - logger.ApplicationLogger = logrus.StandardLogger() - cnf := config.Config{ - Broker: "amqp", - TaskQueueName: "paota_task_queue", - AMQP: &config.AMQPConfig{ - Url: "amqp://localhost:55005/", - Exchange: "paota_task_exchange", - ExchangeType: "direct", - BindingKey: "paota_task_binding_key", - PrefetchCount: 100, - ConnectionPoolSize: 10, - }, - } - err := config.GetConfigProvider().SetApplicationConfig(cnf) - if err != nil { - logger.ApplicationLogger.Error("config error", err) - return nil - } - newWorkerPool, err := workerpool.NewWorkerPool(context.Background(), 10, "testWorker") - if err != nil { - logger.ApplicationLogger.Error("workerPool is not created", err) - os.Exit(0) - } else if newWorkerPool == nil { - logger.ApplicationLogger.Info("workerPool is nil") - os.Exit(0) - } - logger.ApplicationLogger.Info("newWorkerPool created successfully") - return newWorkerPool -} - func Publisher(pool workerpool.Pool) { // Replace this with the received user record - user := UserRecord{ + user := amqpnostorage.UserRecord{ ID: "1", Name: "John Doe", Email: "john.doe@example.com", diff --git a/bench/amqpnostorage/setup.go b/bench/amqpnostorage/setup.go new file mode 100644 index 0000000..3ecce3d --- /dev/null +++ b/bench/amqpnostorage/setup.go @@ -0,0 +1,49 @@ +package amqpnostorage + +import ( + "context" + "github.com/sirupsen/logrus" + "github.com/surendratiwari3/paota/internal/config" + "github.com/surendratiwari3/paota/internal/logger" + "github.com/surendratiwari3/paota/workerpool" + "os" +) + +// UserRecord represents the structure of user records. +type UserRecord struct { + ID string `json:"id"` + Name string `json:"name"` + Email string `json:"email"` + // Add other fields as needed +} + +func SetupWorkerPool() workerpool.Pool { + logger.ApplicationLogger = logrus.StandardLogger() + cnf := config.Config{ + Broker: "amqp", + TaskQueueName: "paota_task_queue", + AMQP: &config.AMQPConfig{ + Url: "amqp://localhost:55005/", + Exchange: "paota_task_exchange", + ExchangeType: "direct", + BindingKey: "paota_task_binding_key", + PrefetchCount: 100, + ConnectionPoolSize: 10, + }, + } + err := config.GetConfigProvider().SetApplicationConfig(cnf) + if err != nil { + logger.ApplicationLogger.Error("config error", err) + return nil + } + newWorkerPool, err := workerpool.NewWorkerPool(context.Background(), 10, "testWorker") + if err != nil { + logger.ApplicationLogger.Error("workerPool is not created", err) + os.Exit(0) + } else if newWorkerPool == nil { + logger.ApplicationLogger.Info("workerPool is nil") + os.Exit(0) + } + logger.ApplicationLogger.Info("newWorkerPool created successfully") + return newWorkerPool +}