mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 11:29:48 +08:00
refactor: commands
This commit is contained in:
1
cli/README.md
Normal file
1
cli/README.md
Normal file
@@ -0,0 +1 @@
|
||||
# hrp cli
|
||||
70
cli/hrp/cmd/boom.go
Normal file
70
cli/hrp/cmd/boom.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/hrp"
|
||||
"github.com/httprunner/hrp/internal/boomer"
|
||||
)
|
||||
|
||||
// boomCmd represents the boom command
|
||||
var boomCmd = &cobra.Command{
|
||||
Use: "boom",
|
||||
Short: "run load test with boomer",
|
||||
Long: `run yaml/json testcase files for load test`,
|
||||
Example: ` $ hrp boom demo.json # run specified json testcase file
|
||||
$ hrp boom demo.yaml # run specified yaml testcase file
|
||||
$ hrp boom examples/ # run testcases in specified folder`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
boomer.SetUlimit(10240) // ulimit -n 10240
|
||||
setLogLevel("WARN") // disable info logs for load testing
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
var paths []hrp.ITestCase
|
||||
for _, arg := range args {
|
||||
paths = append(paths, &hrp.TestCasePath{Path: arg})
|
||||
}
|
||||
hrpBoomer := hrp.NewBoomer(spawnCount, spawnRate)
|
||||
hrpBoomer.SetRateLimiter(maxRPS, requestIncreaseRate)
|
||||
if !disableConsoleOutput {
|
||||
hrpBoomer.AddOutput(boomer.NewConsoleOutput())
|
||||
}
|
||||
if prometheusPushgatewayURL != "" {
|
||||
hrpBoomer.AddOutput(boomer.NewPrometheusPusherOutput(prometheusPushgatewayURL, "hrp"))
|
||||
}
|
||||
hrpBoomer.EnableCPUProfile(cpuProfile, cpuProfileDuration)
|
||||
hrpBoomer.EnableMemoryProfile(memoryProfile, memoryProfileDuration)
|
||||
hrpBoomer.Run(paths...)
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
spawnCount int
|
||||
spawnRate float64
|
||||
maxRPS int64
|
||||
requestIncreaseRate string
|
||||
memoryProfile string
|
||||
memoryProfileDuration time.Duration
|
||||
cpuProfile string
|
||||
cpuProfileDuration time.Duration
|
||||
prometheusPushgatewayURL string
|
||||
disableConsoleOutput bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(boomCmd)
|
||||
|
||||
boomCmd.Flags().Int64Var(&maxRPS, "max-rps", 0, "Max RPS that boomer can generate, disabled by default.")
|
||||
boomCmd.Flags().StringVar(&requestIncreaseRate, "request-increase-rate", "-1", "Request increase rate, disabled by default.")
|
||||
boomCmd.Flags().IntVar(&spawnCount, "spawn-count", 1, "The number of users to spawn for load testing")
|
||||
boomCmd.Flags().Float64Var(&spawnRate, "spawn-rate", 1, "The rate for spawning users")
|
||||
boomCmd.Flags().StringVar(&memoryProfile, "mem-profile", "", "Enable memory profiling.")
|
||||
boomCmd.Flags().DurationVar(&memoryProfileDuration, "mem-profile-duration", 30*time.Second, "Memory profile duration.")
|
||||
boomCmd.Flags().StringVar(&cpuProfile, "cpu-profile", "", "Enable CPU profiling.")
|
||||
boomCmd.Flags().DurationVar(&cpuProfileDuration, "cpu-profile-duration", 30*time.Second, "CPU profile duration.")
|
||||
boomCmd.Flags().StringVar(&prometheusPushgatewayURL, "prometheus-gateway", "", "Prometheus Pushgateway url.")
|
||||
boomCmd.Flags().BoolVar(&disableConsoleOutput, "disable-console-output", false, "Disable console output.")
|
||||
}
|
||||
15
cli/hrp/cmd/doc_test.go
Normal file
15
cli/hrp/cmd/doc_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra/doc"
|
||||
)
|
||||
|
||||
// run this test to generate markdown docs for hrp command
|
||||
func TestGenMarkdownTree(t *testing.T) {
|
||||
err := doc.GenMarkdownTree(rootCmd, "../../../docs/cmd")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
59
cli/hrp/cmd/har2case.go
Normal file
59
cli/hrp/cmd/har2case.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/hrp/internal/har2case"
|
||||
)
|
||||
|
||||
// har2caseCmd represents the har2case command
|
||||
var har2caseCmd = &cobra.Command{
|
||||
Use: "har2case $har_path...",
|
||||
Short: "Convert HAR to json/yaml testcase files",
|
||||
Long: `Convert HAR to json/yaml testcase files`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
var outputFiles []string
|
||||
for _, arg := range args {
|
||||
var outputPath string
|
||||
var err error
|
||||
|
||||
har := har2case.NewHAR(arg)
|
||||
|
||||
// specify output dir
|
||||
if outputDir != "" {
|
||||
har.SetOutputDir(outputDir)
|
||||
}
|
||||
|
||||
// generate json/yaml files
|
||||
if genYAMLFlag {
|
||||
outputPath, err = har.GenYAML()
|
||||
} else {
|
||||
outputPath, err = har.GenJSON()
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outputFiles = append(outputFiles, outputPath)
|
||||
}
|
||||
log.Info().Strs("output", outputFiles).Msg("convert testcase success")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
genJSONFlag bool
|
||||
genYAMLFlag bool
|
||||
outputDir string
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(har2caseCmd)
|
||||
har2caseCmd.Flags().BoolVarP(&genJSONFlag, "to-json", "j", false, "convert to JSON format (default)")
|
||||
har2caseCmd.Flags().BoolVarP(&genYAMLFlag, "to-yaml", "y", false, "convert to JSON format")
|
||||
har2caseCmd.Flags().StringVarP(&outputDir, "output-dir", "d", "", "specify output directory, default to the same dir with har file")
|
||||
}
|
||||
75
cli/hrp/cmd/root.go
Normal file
75
cli/hrp/cmd/root.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/hrp/internal/version"
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "hrp",
|
||||
Short: "One-stop solution for HTTP(S) testing.",
|
||||
Long: `
|
||||
██╗ ██╗████████╗████████╗██████╗ ██████╗ ██╗ ██╗███╗ ██╗███╗ ██╗███████╗██████╗
|
||||
██║ ██║╚══██╔══╝╚══██╔══╝██╔══██╗██╔══██╗██║ ██║████╗ ██║████╗ ██║██╔════╝██╔══██╗
|
||||
███████║ ██║ ██║ ██████╔╝██████╔╝██║ ██║██╔██╗ ██║██╔██╗ ██║█████╗ ██████╔╝
|
||||
██╔══██║ ██║ ██║ ██╔═══╝ ██╔══██╗██║ ██║██║╚██╗██║██║╚██╗██║██╔══╝ ██╔══██╗
|
||||
██║ ██║ ██║ ██║ ██║ ██║ ██║╚██████╔╝██║ ╚████║██║ ╚████║███████╗██║ ██║
|
||||
╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝
|
||||
|
||||
hrp (HttpRunner+) aims to be a one-stop solution for HTTP(S) testing, covering API testing,
|
||||
load testing and digital experience monitoring (DEM). Enjoy! ✨ 🚀 ✨
|
||||
|
||||
License: Apache-2.0
|
||||
Website: https://httprunner.com
|
||||
Github: https://github.com/httprunner/hrp
|
||||
Copyright 2021 debugtalk`,
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
if !logJSON {
|
||||
log.Logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger()
|
||||
log.Info().Msg("Set log to color console other than JSON format.")
|
||||
}
|
||||
},
|
||||
Version: version.VERSION,
|
||||
}
|
||||
|
||||
var (
|
||||
logLevel string
|
||||
logJSON bool
|
||||
)
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "INFO", "set log level")
|
||||
rootCmd.PersistentFlags().BoolVar(&logJSON, "log-json", false, "set log to json format")
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func setLogLevel(level string) {
|
||||
level = strings.ToUpper(level)
|
||||
log.Info().Str("level", level).Msg("Set log level")
|
||||
switch level {
|
||||
case "DEBUG":
|
||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||
case "INFO":
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
case "WARN":
|
||||
zerolog.SetGlobalLevel(zerolog.WarnLevel)
|
||||
case "ERROR":
|
||||
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
|
||||
case "FATAL":
|
||||
zerolog.SetGlobalLevel(zerolog.FatalLevel)
|
||||
case "PANIC":
|
||||
zerolog.SetGlobalLevel(zerolog.PanicLevel)
|
||||
}
|
||||
}
|
||||
53
cli/hrp/cmd/run.go
Normal file
53
cli/hrp/cmd/run.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/hrp"
|
||||
)
|
||||
|
||||
// runCmd represents the run command
|
||||
var runCmd = &cobra.Command{
|
||||
Use: "run $path...",
|
||||
Short: "run API test",
|
||||
Long: `run yaml/json testcase files for API test`,
|
||||
Example: ` $ hrp run demo.json # run specified json testcase file
|
||||
$ hrp run demo.yaml # run specified yaml testcase file
|
||||
$ hrp run examples/ # run testcases in specified folder`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
var paths []hrp.ITestCase
|
||||
for _, arg := range args {
|
||||
paths = append(paths, &hrp.TestCasePath{Path: arg})
|
||||
}
|
||||
runner := hrp.NewRunner(nil).
|
||||
SetDebug(!silentFlag).
|
||||
SetFailfast(!continueOnFailure)
|
||||
if proxyUrl != "" {
|
||||
runner.SetProxyUrl(proxyUrl)
|
||||
}
|
||||
err := runner.Run(paths...)
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
continueOnFailure bool
|
||||
silentFlag bool
|
||||
proxyUrl string
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(runCmd)
|
||||
runCmd.Flags().BoolVar(&continueOnFailure, "continue-on-failure", false, "continue running next step when failure occurs")
|
||||
runCmd.Flags().BoolVarP(&silentFlag, "silent", "s", false, "disable logging request & response details")
|
||||
runCmd.Flags().StringVarP(&proxyUrl, "proxy-url", "p", "", "set proxy url")
|
||||
// runCmd.Flags().BoolP("gen-html-report", "r", false, "Generate HTML report")
|
||||
}
|
||||
28
cli/hrp/cmd/scaffold.go
Normal file
28
cli/hrp/cmd/scaffold.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/hrp/internal/builtin"
|
||||
)
|
||||
|
||||
var scaffoldCmd = &cobra.Command{
|
||||
Use: "startproject $project_name",
|
||||
Short: "Create a scaffold project",
|
||||
Args: cobra.ExactValidArgs(1),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := builtin.CreateScaffold(args[0])
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(scaffoldCmd)
|
||||
}
|
||||
13
cli/hrp/main.go
Normal file
13
cli/hrp/main.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/httprunner/hrp/cli/hrp/cmd"
|
||||
"github.com/httprunner/hrp/internal/sentry"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Flush buffered events before the program terminates.
|
||||
defer sentry.Flush()
|
||||
|
||||
cmd.Execute()
|
||||
}
|
||||
23
cli/scripts/build.sh
Normal file
23
cli/scripts/build.sh
Normal file
@@ -0,0 +1,23 @@
|
||||
#!/bin/bash
|
||||
# build hrp cli binary for testing
|
||||
# release will be triggered on github actions, see .github/workflows/release.yml
|
||||
|
||||
# Usage:
|
||||
# $ make build
|
||||
# or
|
||||
# $ bash cli/scripts/build.sh
|
||||
|
||||
set -e
|
||||
set -x
|
||||
|
||||
# prepare path
|
||||
mkdir -p "output"
|
||||
bin_path="output/hrp"
|
||||
|
||||
# build
|
||||
go build -ldflags '-s -w' -o "$bin_path" cli/hrp/main.go
|
||||
|
||||
# check output and version
|
||||
ls -lh "$bin_path"
|
||||
chmod +x "$bin_path"
|
||||
./"$bin_path" -v
|
||||
84
cli/scripts/install.sh
Normal file
84
cli/scripts/install.sh
Normal file
@@ -0,0 +1,84 @@
|
||||
#!/bin/bash
|
||||
# install hrp with one shell command
|
||||
# curl -sL https://raw.githubusercontent.com/httprunner/hrp/main/cli/scripts/install.sh | bash
|
||||
|
||||
set -e
|
||||
|
||||
function echoError() {
|
||||
echo -e "\033[31m✘ $1\033[0m" # red
|
||||
}
|
||||
export -f echoError
|
||||
|
||||
function echoInfo() {
|
||||
echo -e "\033[32m✔ $1\033[0m" # green
|
||||
}
|
||||
export -f echoInfo
|
||||
|
||||
function echoWarn() {
|
||||
echo -e "\033[33m! $1\033[0m" # yellow
|
||||
}
|
||||
export -f echoError
|
||||
|
||||
function get_latest_version() {
|
||||
# <title>Release v0.4.0 · httprunner/hrp · GitHub</title>
|
||||
curl -sL https://github.com/httprunner/hrp/releases/latest | grep '<title>Release' | cut -d" " -f4
|
||||
}
|
||||
|
||||
function get_arch() {
|
||||
arch=$(uname -m)
|
||||
if [ "$arch" == "x86_64" ]; then
|
||||
arch="amd64"
|
||||
fi
|
||||
echo "$arch"
|
||||
}
|
||||
|
||||
function main() {
|
||||
echoInfo "Detect target hrp package..."
|
||||
version=$(get_latest_version)
|
||||
echo "Latest version: $version"
|
||||
os=$(uname -s)
|
||||
echo "Current OS: $os"
|
||||
arch=$(get_arch)
|
||||
echo "Current ARCH: $arch"
|
||||
pkg="hrp-$version-$os-$arch.tar.gz"
|
||||
url="https://github.com/httprunner/hrp/releases/download/$version/$pkg"
|
||||
echo "Selected package: $url"
|
||||
echo
|
||||
|
||||
echoInfo "Created temp dir..."
|
||||
tmp_dir=$(mktemp -d -t hrp)
|
||||
echo "$tmp_dir"
|
||||
cd "$tmp_dir"
|
||||
echo
|
||||
|
||||
echoInfo "Downloading..."
|
||||
curl -L $url -o "$pkg"
|
||||
echo
|
||||
|
||||
echoInfo "Extracting..."
|
||||
tar -zxf "$pkg"
|
||||
ls -lh
|
||||
echo
|
||||
|
||||
echoInfo "Installing..."
|
||||
if hrp -v > /dev/null; then
|
||||
echoWarn "$(hrp -v) exists, remove first !!!"
|
||||
echo "$ rm -rf $(which hrp)"
|
||||
rm -rf "$(which hrp)"
|
||||
fi
|
||||
|
||||
echo "chmod +x hrp && mv hrp /usr/local/bin/"
|
||||
chmod +x hrp
|
||||
mv hrp /usr/local/bin/
|
||||
echo
|
||||
|
||||
echoInfo "Check installation..."
|
||||
echo "$ which hrp"
|
||||
which hrp
|
||||
echo "$ hrp -v"
|
||||
hrp -v
|
||||
echo "$ hrp -h"
|
||||
hrp -h
|
||||
}
|
||||
|
||||
main
|
||||
Reference in New Issue
Block a user