Skip to content

Commit

Permalink
Feature parallelization of the tests (#1)
Browse files Browse the repository at this point in the history
* chore: The kind made no sense

* feat: Makes the connections in parallel

---------

Co-authored-by: javier.juarez <[email protected]>
  • Loading branch information
jjuarez and javier.juarez authored Feb 9, 2023
1 parent d9673f0 commit 645dd82
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
41 changes: 29 additions & 12 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package cmd

import (
"fmt"
"sync"
"time"

"github.com/jjuarez/simple-prober/internal/codes"
"github.com/jjuarez/simple-prober/internal/config"
"github.com/jjuarez/simple-prober/internal/logger"
"github.com/jjuarez/simple-prober/internal/model"
"github.com/jjuarez/simple-prober/internal/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -25,6 +27,31 @@ var (
logLevel string
)

func doTests(endpoints []model.Endpoint) map[string]bool {
start := time.Now()
defer func() {
log.Info(fmt.Sprintf("Execution time: %s", time.Since(start)))
}()

connectionResults := make(map[string]bool, len(endpoints))
wg := sync.WaitGroup{}
// Connection tests
for _, e := range endpoints {
wg.Add(1)
go func(e model.Endpoint) {
r, err := e.Connect(timeout)
if err != nil {
return
}
connectionResults[e.Name] = r
log.Debug(fmt.Sprintf("Tested: %s, with result: %v", e.Name, r))
wg.Done()
}(e)
}
wg.Wait()
return connectionResults
}

// checkCmd represents the check command
var checkCmd = &cobra.Command{
Use: "check",
Expand All @@ -43,29 +70,19 @@ var checkCmd = &cobra.Command{
if err != nil {
utils.ExitCommand(codes.ReadError, err)
}

// Connection tests
for _, e := range *eList {
r, err := e.TestConnection(timeout)
if err != nil {
log.Error(err)
}
log.Info(fmt.Sprintf("%v, %v\n", e, r))
}
results := doTests(*eList)
log.Info(results)
},
}

func init() {
var parameterTimeout int

rootCmd.AddCommand(checkCmd)

// The log level
rootCmd.PersistentFlags().StringVar(&logLevel, "loglevel", defaultLogLevel, "The log level")

// The confiuration file name
rootCmd.PersistentFlags().StringVar(&configFileName, "config", defaultConfigFileName, "Config file (default is: config/endpoints.yaml)")

// The connection timeout
rootCmd.PersistentFlags().IntVar(&parameterTimeout, "timeout", defaultTimeout, "Connection timeout")
timeout = time.Duration(parameterTimeout) * time.Second
Expand Down
12 changes: 6 additions & 6 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,42 @@ import (
"time"
)

// Stringable ...
type Stringable interface {
String() string
}

// Printable ...
type Printable interface {
Println()
}

// Endpoint The endpoint basic data.
type Endpoint struct {
Name string `yaml:"name,omitempty"`
Kind string `yaml:"kind,omitempty"`
Address string `yaml:"address"`
}

// New Creates an Endpoint.
func New(name string, kind string, address string) *Endpoint {
func New(name string, address string) *Endpoint {
return &Endpoint{
Name: name,
Kind: kind,
Address: address,
}
}

// String Implements the Stingable interface for Endpoint.
func (e Endpoint) String() string {
return fmt.Sprintf("%T(name:%s, kind:%s, address:%s)", e, e.Name, e.Kind, e.Address)
return fmt.Sprintf("%T(name:%s, address:%s)", e, e.Name, e.Address)
}

// Println Implements the Printable interface for Endpoint.
func (e Endpoint) Println() {
fmt.Println(e.String())
}

// TestConnection ...
func (e Endpoint) TestConnection(timeout time.Duration) (bool, error) {
// Connect ...
func (e Endpoint) Connect(timeout time.Duration) (bool, error) {
var dialer net.Dialer
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
Expand Down

0 comments on commit 645dd82

Please sign in to comment.