Skip to content

Commit

Permalink
sonarqube: fix duplicate in bench test
Browse files Browse the repository at this point in the history
  • Loading branch information
surendratiwari3 committed Feb 6, 2024
1 parent 19cf780 commit 4368d24
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 81 deletions.
38 changes: 2 additions & 36 deletions bench/amqpnostorage/consumer/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -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{}{
Expand Down
48 changes: 3 additions & 45 deletions bench/amqpnostorage/publisher/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
49 changes: 49 additions & 0 deletions bench/amqpnostorage/setup.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4368d24

Please sign in to comment.