-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
190 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
import "github.com/gadget-inc/dateilager/pkg/cli" | ||
|
||
func main() { | ||
cli.ConnTestExecute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package testutil | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
dlc "github.com/gadget-inc/dateilager/pkg/client" | ||
) | ||
|
||
type AttemptResult int | ||
|
||
const ( | ||
Ok AttemptResult = iota | ||
TimedOut | ||
Error | ||
) | ||
|
||
type Results struct { | ||
count int | ||
failCount int | ||
downtimeStart *time.Time | ||
downtimes []time.Duration | ||
} | ||
|
||
func (r *Results) Add(attempt AttemptResult) { | ||
now := time.Now() | ||
|
||
r.count += 1 | ||
if attempt != Ok { | ||
r.failCount += 1 | ||
} | ||
|
||
switch { | ||
case r.downtimeStart == nil && attempt != Ok: | ||
r.downtimeStart = &now | ||
case r.downtimeStart != nil && attempt == Ok: | ||
r.downtimes = append(r.downtimes, now.Sub(*r.downtimeStart)) | ||
r.downtimeStart = nil | ||
} | ||
} | ||
|
||
func (r *Results) Summarize() { | ||
var max time.Duration | ||
for _, duration := range r.downtimes { | ||
if duration > max { | ||
max = duration | ||
} | ||
} | ||
|
||
fmt.Println("--- Result ---") | ||
fmt.Printf("request count: %d\n", r.count) | ||
fmt.Printf("success rate: %2.f%%\n", float32(r.count-r.failCount)/float32(r.count)*100) | ||
fmt.Printf("max downtime: %s\n", max.String()) | ||
} | ||
|
||
func tryConnect(ctx context.Context, client *dlc.Client, timeout time.Duration) AttemptResult { | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
_, err := client.Get(ctx, 1, "a", nil, dlc.VersionRange{}) | ||
if os.IsTimeout(err) { | ||
fmt.Printf("conn timed out: %v\n", err) | ||
return TimedOut | ||
} | ||
if err != nil { | ||
fmt.Printf("conn error: %v\n", err) | ||
return Error | ||
} | ||
return Ok | ||
} | ||
|
||
func TestConnection(ctx context.Context, client *dlc.Client) error { | ||
results := Results{} | ||
|
||
clock := time.NewTicker(100 * time.Millisecond) | ||
defer clock.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
results.Summarize() | ||
return nil | ||
case <-clock.C: | ||
results.Add(tryConnect(ctx, client, 50*time.Millisecond)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/gadget-inc/dateilager/internal/logger" | ||
"github.com/gadget-inc/dateilager/internal/testutil" | ||
dlc "github.com/gadget-inc/dateilager/pkg/client" | ||
"github.com/gadget-inc/dateilager/pkg/version" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
func NewConnTestCommand() *cobra.Command { | ||
var ( | ||
client *dlc.Client | ||
server string | ||
) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "conn-test", | ||
Short: "DateiLager connection test", | ||
DisableAutoGenTag: true, | ||
Version: version.Version, | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
cmd.SilenceUsage = true // silence usage when an error occurs after flags have been parsed | ||
|
||
config := zap.NewDevelopmentConfig() | ||
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | ||
|
||
err := logger.Init(config) | ||
if err != nil { | ||
return fmt.Errorf("could not initialize logger: %w", err) | ||
} | ||
|
||
ctx, cancel := context.WithCancel(cmd.Context()) | ||
|
||
client, err = dlc.NewClient(ctx, server) | ||
if err != nil { | ||
cancel() | ||
return fmt.Errorf("could not connect to server %s: %w", server, err) | ||
} | ||
|
||
osSignals := make(chan os.Signal, 1) | ||
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM) | ||
go func() { | ||
<-osSignals | ||
cancel() | ||
}() | ||
|
||
return testutil.TestConnection(ctx, client) | ||
}, | ||
PersistentPostRunE: func(cmd *cobra.Command, _ []string) error { | ||
if client != nil { | ||
client.Close() | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
flags := cmd.PersistentFlags() | ||
|
||
flags.StringVar(&server, "server", "", "Server GRPC address") | ||
|
||
return cmd | ||
} | ||
|
||
func ConnTestExecute() { | ||
ctx := context.Background() | ||
cmd := NewConnTestCommand() | ||
|
||
err := cmd.ExecuteContext(ctx) | ||
|
||
if err != nil { | ||
logger.Fatal(ctx, "connection test failed", zap.Error(err)) | ||
} | ||
|
||
logger.Info(ctx, "connection test complete") | ||
_ = logger.Sync() | ||
} |