Skip to content

Commit

Permalink
fix incorrect charts while rps<1
Browse files Browse the repository at this point in the history
update readme.md
optimize timeout interruption
  • Loading branch information
six-ddc committed Jun 18, 2021
1 parent 08afb6e commit 25ded30
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 95 deletions.
110 changes: 109 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,113 @@
# plow
A high-performance HTTP benchmarking tool with real-time web UI and terminal displaying

Plow is a HTTP(S) benchmarking tool, written in Golang. It uses excellent [fasthttp](https://github.com/valyala/fasthttp#http-client-comparison-with-nethttp) instead of Go's default net/http due to its lightning fast performance.

Plow runs at a specified connections(`-c`) concurrently and **real-time** records a summary statistics, histogram of execution time and calculates percentiles to display on Web UI and terminal. It can run for a set duration(`-d`), for a fixed number of requests(`-n`), or until Ctrl-C interrupted.

The implementation of real-time computing Histograms and Quantiles using stream-based algorithms inspired by [prometheus](https://github.com/prometheus/client_golang) with low memory and CPU bounds. so it's almost no additional performance overhead for benchmarking.

![](https://github.com/six-ddc/plow/blob/main/demo.gif?raw=true)

```text
❯ ./plow http://127.0.0.1:8080/hello -c 20
Benchmarking http://127.0.0.1:8080/hello using 20 connection(s).
> Real-time charts is listening on http://127.0.0.1:18888/
Summary:
Elapsed 8.6s
Count 969657
2xx 776392
4xx 193265
RPS 112741.713
Reads 10.192MB/s
Writes 6.774MB/s
Statistics Min Mean StdDev Max
Latency 32µs 176µs 37µs 1.839ms
RPS 108558.4 112818.12 2456.63 115949.98
Latency Percentile:
P50 P75 P90 P95 P99 P99.9 P99.99
173µs 198µs 222µs 238µs 274µs 352µs 498µs
Latency Histogram:
141µs 273028 ■■■■■■■■■■■■■■■■■■■■■■■■
177µs 458955 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
209µs 204717 ■■■■■■■■■■■■■■■■■■
235µs 26146 ■■
269µs 6029 ■
320µs 721
403µs 58
524µs 3
```

## Installation

Binary and image distributions are available through the [releases](https://github.com/six-ddc/plow/releases) assets page.

### Via Go

```bash
go get github.com/six-ddc/plow
```

### Via Homebrew

*Coming soon*

## Usage

### Options

```bash
usage: plow [<flags>] <url>

A high-performance HTTP benchmarking tool with real-time web UI and terminal displaying

Example:

plow http://127.0.0.1:8080/ -c 20 -n 100000
plow https://httpbin.org/post -c 20 -d 5m --body @file.json -T 'application/json' -m POST

Flags:
--help Show context-sensitive help.
-c, --concurrency=1 Number of connections to run concurrently
-n, --requests=-1 Number of requests to run
-d, --duration=DURATION Duration of test, examples: -d 10s -d 3m
-i, --interval=200ms Print snapshot result every interval, use 0 to print once at the end
--seconds Use seconds as time unit to print
--body=BODY HTTP request body, if start the body with @, the rest should be a filename to read
--stream Specify whether to stream file specified by '--body @file' using chunked encoding or to read into memory
-m, --method="GET" HTTP method
-H, --header=K:V ... Custom HTTP headers
--host=HOST Host header
-T, --content=CONTENT Content-Type header
--listen=":18888" Listen addr to serve Web UI
--link="127.0.0.1:18888" Link addr used for show Web html and request backend server
--timeout=DURATION Timeout for each http request
--dial-timeout=DURATION Timeout for dial addr
--req-timeout=DURATION Timeout for full request writing
--resp-timeout=DURATION Timeout for full response reading
--socks5=ip:port Socks5 proxy
--version Show application version.

Args:
<url> request url
```
### Examples
Basic usage:
```bash
plow http://127.0.0.1:8080/ -c 20 -n 10000 -d 10s
```
POST a json file:
```bash
plow http://127.0.0.1:8080/ -c 20 --body @file.json -T 'application/json' -m POST
```
### License
See [LICENSE](https://github.com/six-ddc/plow/blob/master/LICENSE).
77 changes: 34 additions & 43 deletions charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,10 @@ import (
"github.com/valyala/fasthttp"
"net"
"strings"
"sync"
"text/template"
"time"
)

func init() {
templates.PageTpl = `
{{- define "page" }}
<!DOCTYPE html>
<html>
{{- template "header" . }}
<body>
<p align="center">🚀 <a href="https://github.com/six-ddc/plow"><b>plow</b></a> <em>is a high-performance HTTP benchmarking tool with real-time web UI and terminal displaying</em></p>
<style> .box { justify-content:center; display:flex; flex-wrap:wrap } </style>
<div class="box"> {{- range .Charts }} {{ template "base" . }} {{- end }} </div>
</body>
</html>
{{ end }}
`
}

var (
assertsPath = "/echarts/statics/"
apiPath = "/data"
Expand All @@ -43,7 +26,7 @@ var (
)

const (
DefaultTemplate string = `
ViewTpl = `
$(function () { setInterval({{ .ViewID }}_sync, {{ .Interval }}); });
function {{ .ViewID }}_sync() {
$.ajax({
Expand All @@ -64,10 +47,23 @@ function {{ .ViewID }}_sync() {
}
});
}`
PageTpl = `
{{- define "page" }}
<!DOCTYPE html>
<html>
{{- template "header" . }}
<body>
<p align="center">🚀 <a href="https://github.com/six-ddc/plow"><b>Plow</b></a> %s</p>
<style> .box { justify-content:center; display:flex; flex-wrap:wrap } </style>
<div class="box"> {{- range .Charts }} {{ template "base" . }} {{- end }} </div>
</body>
</html>
{{ end }}
`
)

func (c *Charts) genViewTemplate(vid, route string) string {
tpl, err := template.New("view").Parse(DefaultTemplate)
tpl, err := template.New("view").Parse(ViewTpl)
if err != nil {
panic("failed to parse template " + err.Error())
}
Expand All @@ -88,7 +84,7 @@ func (c *Charts) genViewTemplate(vid, route string) string {

buf := bytes.Buffer{}
if err := tpl.Execute(&buf, d); err != nil {
panic("statsview: failed to execute template " + err.Error())
panic("failed to execute template " + err.Error())
}

return buf.String()
Expand Down Expand Up @@ -137,21 +133,20 @@ func (c *Charts) newRPSView() components.Charter {
}

type Metrics struct {
Values []float64 `json:"values"`
Time string `json:"time"`
Values []interface{} `json:"values"`
Time string `json:"time"`
}

type Charts struct {
listenAddr string
linkAddr string
page *components.Page
ln net.Listener
lock sync.Mutex
reportData ChartsReport
dataFunc func() *ChartsReport
}

func NewCharts(listenAddr string, linkAddr string, dataFunc func() *ChartsReport) (*Charts, error) {
func NewCharts(listenAddr string, linkAddr string, dataFunc func() *ChartsReport, desc string) (*Charts, error) {
templates.PageTpl = fmt.Sprintf(PageTpl, desc)
ln, err := net.Listen("tcp4", listenAddr)
if err != nil {
return nil, err
Expand Down Expand Up @@ -180,17 +175,24 @@ func (c *Charts) Handler(ctx *fasthttp.RequestCtx) {
default:
if strings.HasPrefix(path, apiPath) {
view := path[len(apiPath)+1:]
var values []float64
c.lock.Lock()
var values []interface{}
reportData := c.dataFunc()
switch view {
case latencyView:
values = append(values, c.dataFunc().Latency.min/1e6)
values = append(values, c.dataFunc().Latency.Mean()/1e6)
values = append(values, c.dataFunc().Latency.max/1e6)
if reportData != nil {
values = append(values, reportData.Latency.min/1e6)
values = append(values, reportData.Latency.Mean()/1e6)
values = append(values, reportData.Latency.max/1e6)
} else {
values = append(values, nil, nil, nil)
}
case rpsView:
values = append(values, c.dataFunc().RPS)
if reportData != nil {
values = append(values, reportData.RPS)
} else {
values = append(values, nil)
}
}
c.lock.Unlock()
metrics := &Metrics{
Time: time.Now().Format(timeFormat),
Values: values,
Expand All @@ -203,17 +205,6 @@ func (c *Charts) Handler(ctx *fasthttp.RequestCtx) {
}

func (c *Charts) Serve() {
go func() {
ticker := time.NewTicker(refreshInterval)
for {
select {
case <-ticker.C:
c.lock.Lock()
c.reportData = *c.dataFunc()
c.lock.Unlock()
}
}
}()
server := fasthttp.Server{
Handler: cors.DefaultHandler().CorsMiddleware(c.Handler),
}
Expand Down
18 changes: 11 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package main

import (
"fmt"
"gopkg.in/alecthomas/kingpin.v3-unstable"
"io/ioutil"
"os"
"strings"

"gopkg.in/alecthomas/kingpin.v3-unstable"
)

var (
Expand All @@ -22,7 +23,7 @@ var (
host = kingpin.Flag("host", "Host header").String()
contentType = kingpin.Flag("content", "Content-Type header").Short('T').String()

chartsListenAddr = kingpin.Flag("listen", "Listen addr to serve Web UI").Default("127.0.0.1:18888").String()
chartsListenAddr = kingpin.Flag("listen", "Listen addr to serve Web UI").Default(":18888").String()
chartsLinkAddr = kingpin.Flag("link", "Link addr used for show Web html and request backend server").Default("127.0.0.1:18888").String()
timeout = kingpin.Flag("timeout", "Timeout for each http request").PlaceHolder("DURATION").Duration()
dialTimeout = kingpin.Flag("dial-timeout", "Timeout for dial addr").PlaceHolder("DURATION").Duration()
Expand Down Expand Up @@ -91,14 +92,17 @@ Example:
host: *host,
}

fmt.Printf("Benchmarking %s", *url)
var desc string
desc = fmt.Sprintf("Benchmarking %s", *url)
if *requests > 0 {
fmt.Printf(" with %d request(s)", *requests)
desc += fmt.Sprintf(" with %d request(s)", *requests)
}
if *duration > 0 {
fmt.Printf(" for %s", duration.String())
desc += fmt.Sprintf(" for %s", duration.String())
}
fmt.Printf(" using %d connection(s)\n", *concurrency)
desc += fmt.Sprintf(" using %d connection(s).", *concurrency)

fmt.Println(desc)
if *chartsListenAddr != "" {
fmt.Printf("> Real-time charts is listening on http://%s/\n", *chartsLinkAddr)
}
Expand All @@ -115,7 +119,7 @@ Example:
go report.Collect(requester.RecordChan())

if *chartsListenAddr != "" {
charts, err := NewCharts(*chartsListenAddr, *chartsLinkAddr, report.Charts)
charts, err := NewCharts(*chartsListenAddr, *chartsLinkAddr, report.Charts, desc)
if err != nil {
errAndExit(err.Error())
return
Expand Down
17 changes: 13 additions & 4 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type StreamReport struct {

latencyWithinSec *Stats
rpsWithinSec float64
noDateWithinSec bool

readBytes int64
writeBytes int64
Expand Down Expand Up @@ -131,6 +132,9 @@ func (s *StreamReport) Collect(records <-chan *ReportRecord) {
*s.latencyWithinSec = *latencyWithinSecTemp
s.rpsWithinSec = rps
latencyWithinSecTemp.Reset()
s.noDateWithinSec = false
} else {
s.noDateWithinSec = true
}
s.lock.Unlock()
case <-s.doneChan:
Expand Down Expand Up @@ -235,7 +239,7 @@ func (s *StreamReport) Snapshot() *SnapshotReport {

rs.Percentiles = make([]*struct {
Percentile float64
Latency time.Duration
Latency time.Duration
}, len(quantiles))
for i, p := range quantiles {
rs.Percentiles[i] = &struct {
Expand Down Expand Up @@ -271,9 +275,14 @@ type ChartsReport struct {

func (s *StreamReport) Charts() *ChartsReport {
s.lock.Lock()
cr := &ChartsReport{
RPS: s.rpsWithinSec,
Latency: *s.latencyWithinSec,
var cr *ChartsReport
if s.noDateWithinSec {
cr = nil
} else {
cr = &ChartsReport{
RPS: s.rpsWithinSec,
Latency: *s.latencyWithinSec,
}
}
s.lock.Unlock()
return cr
Expand Down
Loading

0 comments on commit 25ded30

Please sign in to comment.