Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
Add support for udp protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
agile6v committed Mar 18, 2019
1 parent c6bdebb commit 918ce83
Show file tree
Hide file tree
Showing 8 changed files with 601 additions and 80 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Squeeze
![Build Status](https://travis-ci.org/agile6v/squeeze.svg?branch=master) ![version](https://img.shields.io/badge/version-0.1.0--beta-bule.svg) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
![Build Status](https://travis-ci.org/agile6v/squeeze.svg?branch=master) [![version](https://img.shields.io/badge/version-0.1.0--beta-bule.svg)](https://github.com/agile6v/squeeze/releases/tag/v0.1.0) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)

**Squeeze** is a modern, easy-to-use, and highly capable load-testing tool. It uses the Master-Slave pattern to simulate any number of users hitting the target. In addition, Squeeze provides the command line and web-based tool to create test tasks and display test results.
**Squeeze** is a modern, easy-to-use, and highly capable load-testing tool. It uses the Master-Slave pattern to simulate any number of users hitting the target. In addition, Squeeze provides the command line and web-based tool to create tasks and display results.

# Table of Contents
- [Features](#features)
Expand Down
95 changes: 95 additions & 0 deletions cmd/udp/udp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2019 Squeeze Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package udp

import (
"os"
"fmt"
"math"
"errors"
"os/signal"
"github.com/agile6v/squeeze/pkg/config"
"github.com/agile6v/squeeze/pkg/pb"
log "github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/agile6v/squeeze/pkg/proto/builder"
)

func UDPCmd(configArgs *config.ProtoConfigArgs) *cobra.Command {
udpOptions := config.NewUDPOptions()
udpCmd := &cobra.Command{
Use: "udp",
Short: "udp protocol benchmark",
Long: `udp protocol benchmark`,
Args: cobra.ExactArgs(1),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return udpOptions.Validate(args)
},
RunE: func(cmd *cobra.Command, args []string) error {
configArgs.Options = udpOptions
builder := builder.NewBuilder(pb.Protocol_UDP)

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
fmt.Printf("\nCanceling...\n")
_, err := builder.CancelTask(configArgs)
if err != nil {
log.Errorf("failed to cancel task %s", err)
}
}()

resp, err := builder.CreateTask(configArgs)
if err != nil {
log.Errorf("failed to create task %s", err)
if resp != "" {
return errors.New(resp)
}
return err
}

if configArgs.Callback != "" {
fmt.Printf("%s", resp)
return nil
}

ret, err := builder.Render(resp)
if err != nil {
log.Errorf("failed to render response, %s", err)
return err
}

fmt.Printf("%s", ret)
return nil
},
}

udpCmd.PersistentFlags().IntVarP(&udpOptions.Requests, "requests", "n",
math.MaxInt32, "Number of requests to perform")
udpCmd.PersistentFlags().IntVarP(&udpOptions.Concurrency, "concurrency", "c",
1, "Number of multiple requests to make at a time")
udpCmd.PersistentFlags().IntVarP(&udpOptions.MsgLength, "message-length", "l",
1, "The length of the message to send")
udpCmd.PersistentFlags().IntVarP(&udpOptions.Timeout, "timeout", "s",
30, "Timeout in seconds (Default is 30 seconds)")
udpCmd.PersistentFlags().IntVarP(&udpOptions.Duration, "duration", "z",
0, "Duration of application to send requests. if duration is specified, n is ignored.")
udpCmd.PersistentFlags().IntVar(&udpOptions.MaxResults, "maxResults", 1000000,
"The maximum number of response results that can be used")

return udpCmd
}

36 changes: 33 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package config

import (
"fmt"
"net"
"errors"
"net/url"
"github.com/agile6v/squeeze/pkg/util"
Expand Down Expand Up @@ -106,6 +107,35 @@ func (httpOpts *HttpOptions) Validate(args []string) error {
return nil
}

type UDPOptions struct {
Addr string
Requests int
Concurrency int
Timeout int
Duration int
MsgLength int
MaxResults int
}

func NewUDPOptions() *UDPOptions {
return &UDPOptions{}
}

func (udpOptions *UDPOptions) Validate(args []string) error {
if udpOptions.Concurrency < 1 {
return fmt.Errorf("option --concurrency must be greater than 0.")
}

// Check the validity of the target address
_, _, err := net.SplitHostPort(args[0])
if err != nil {
return err
}

udpOptions.Addr = args[0]
return nil
}

// WsOptions contains websocket protocol runtime parameters
type WsOptions struct {
Scheme string
Expand All @@ -131,11 +161,11 @@ func (wsOptions *WsOptions) Validate(args []string) error {
// Check the validity of the target URL
u, err := url.Parse(args[0])
if err != nil {
return err
}
return err
}

if u.Scheme == "" || u.Path == "" {
return errors.New("URL Scheme or Path cannot be empty.")
return errors.New("URL Scheme or Path cannot be empty.")
}

wsOptions.Scheme = u.Scheme
Expand Down
Loading

0 comments on commit 918ce83

Please sign in to comment.