Skip to content

Commit

Permalink
Merge pull request #11 from kffl/feat/lib
Browse files Browse the repository at this point in the history
feat: enable usage of speedbump as a library/dependency
  • Loading branch information
kffl authored Jul 19, 2022
2 parents e31db1d + 28559ec commit 8d1d153
Show file tree
Hide file tree
Showing 16 changed files with 1,481 additions and 62 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
# speedbump - TCP proxy for simulating variable network latency
# speedbump - TCP proxy with variable latency
<div align="center">
<img alt="Speedbump Logo" src="https://github.com/kffl/speedbump/raw/HEAD/assets/speedbump.gif" width="480" height="auto"/>
<img alt="speedbump logo" src="https://github.com/kffl/speedbump/raw/HEAD/assets/speedbump.gif" width="480" height="auto"/>
</div>
Speedbump is a TCP proxy written in Go which allows for simulating variable network latency.

[![CI Workflow](https://github.com/kffl/speedbump/workflows/CI/badge.svg)](https://github.com/kffl/speedbump/actions) [![Go Report Card](https://goreportcard.com/badge/github.com/kffl/speedbump)](https://goreportcard.com/report/github.com/kffl/speedbump) [![Docker Image Version](https://img.shields.io/docker/v/kffl/speedbump)](https://hub.docker.com/r/kffl/speedbump)
[![CI Workflow](https://github.com/kffl/speedbump/workflows/CI/badge.svg)](https://github.com/kffl/speedbump/actions) [![Go Report Card](https://goreportcard.com/badge/github.com/kffl/speedbump)](https://goreportcard.com/report/github.com/kffl/speedbump) [![Docker Image Version](https://img.shields.io/docker/v/kffl/speedbump)](https://hub.docker.com/r/kffl/speedbump) [![GoDoc](https://godoc.org/github.com/kffl/speedbump/lib?status.svg)](https://godoc.org/github.com/kffl/speedbump/lib)

Speedbump is a TCP proxy which allows for simulating variable network latency.
## Example usage

## Usage

Spawn a new instance listening on port 2000 that proxies TCP traffic to localhost:80 with a base latency of 100ms and sine wave amplitude of 100ms (resulting in maximum added latency being 200ms and minimum being 0), period of which is 1m:
Spawn a new instance listening on port 2000 that proxies TCP traffic to localhost:80 with a base latency of 100ms and sine wave amplitude of 100ms (resulting in maximum added latency being 200ms and minimum being 0), period of which is 1 minute:

```
speedbump --latency=100ms --sine-amplitude=100ms --sine-period=1m --port=2000 localhost:80
```

Spawn a new instance with a base latency of 300ms and a sawtooth wave latency summand with amplitude of 200ms and period of 2 minutes (visualized by the graph below):

```
speedbump --latency=200ms --saw-amplitude=200ms --saw-period=2m --port=2000 localhost:80
```
<div align="center">
<img alt="speedbump sawtooth wave graph" src="https://github.com/kffl/speedbump/raw/HEAD/assets/sawtooth.svg" width="800" height="auto"/>
</div>

## CLI Arguments Reference:

Output of `speedbump --help`:
Expand All @@ -40,6 +48,10 @@ Args:
<destination> TCP proxy destination in host:post format.
```

## Using speedbump as a library

Speedbump can be used as a Go library via its `lib` package. Check `lib` [README](lib/README.md) for additional information.

## License

Copyright Paweł Kuffel 2022, licensed under Apache 2.0 License.
Expand Down
19 changes: 10 additions & 9 deletions args.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"github.com/kffl/speedbump/lib"
"gopkg.in/alecthomas/kingpin.v2"
)

func parseArgs(args []string) (*SpeedbumpCfg, error) {
func parseArgs(args []string) (*lib.SpeedbumpCfg, error) {
var app = kingpin.New("speedbump", "TCP proxy for simulating variable network latency.")

var (
Expand Down Expand Up @@ -35,23 +36,23 @@ func parseArgs(args []string) (*SpeedbumpCfg, error) {
String()
)

app.Version("0.1.0-rc2")
app.Version("0.1.0-rc4")
_, err := app.Parse(args)

if err != nil {
return nil, err
}

var cfg = SpeedbumpCfg{
var cfg = lib.SpeedbumpCfg{
Port: *port,
DestAddr: *destAddr,
BufferSize: int(*bufferSize),
Latency: &LatencyCfg{
base: *latency,
sineAmplitude: *sineAmplitude,
sinePeriod: *sinePeriod,
sawAmplitute: *sawAmplitute,
sawPeriod: *sawPeriod,
Latency: &lib.LatencyCfg{
Base: *latency,
SineAmplitude: *sineAmplitude,
SinePeriod: *sinePeriod,
SawAmplitute: *sawAmplitute,
SawPeriod: *sawPeriod,
},
LogLevel: *logLevel,
}
Expand Down
14 changes: 7 additions & 7 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func TestParseArgsDefault(t *testing.T) {
assert.Equal(t, cfg.DestAddr, "localhost:80")
assert.Equal(t, cfg.Port, 8000)
assert.Equal(t, 0xffff+1, cfg.BufferSize)
assert.Equal(t, time.Millisecond*5, cfg.Latency.base)
assert.Equal(t, time.Duration(0), cfg.Latency.sineAmplitude)
assert.Equal(t, time.Millisecond*5, cfg.Latency.Base)
assert.Equal(t, time.Duration(0), cfg.Latency.SineAmplitude)
}

func TestParseArgsError(t *testing.T) {
Expand All @@ -37,9 +37,9 @@ func TestParseArgsAll(t *testing.T) {
assert.Equal(t, cfg.DestAddr, "host:777")
assert.Equal(t, cfg.Port, 1234)
assert.Equal(t, 200, cfg.BufferSize)
assert.Equal(t, time.Millisecond*100, cfg.Latency.base)
assert.Equal(t, time.Millisecond*50, cfg.Latency.sineAmplitude)
assert.Equal(t, time.Minute, cfg.Latency.sinePeriod)
assert.Equal(t, time.Duration(0), cfg.Latency.sawAmplitute)
assert.Equal(t, time.Duration(0), cfg.Latency.sawPeriod)
assert.Equal(t, time.Millisecond*100, cfg.Latency.Base)
assert.Equal(t, time.Millisecond*50, cfg.Latency.SineAmplitude)
assert.Equal(t, time.Minute, cfg.Latency.SinePeriod)
assert.Equal(t, time.Duration(0), cfg.Latency.SawAmplitute)
assert.Equal(t, time.Duration(0), cfg.Latency.SawPeriod)
}
Loading

0 comments on commit 8d1d153

Please sign in to comment.