Skip to content

Commit

Permalink
Merge pull request #59 from kffl/refactor/lib-package
Browse files Browse the repository at this point in the history
refactor: move gocannon exported members to `lib` package
  • Loading branch information
kffl authored Jul 21, 2022
2 parents 9df5b36 + 9f39db4 commit e5cf194
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 27 deletions.
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ license: "Apache-2.0"
message: "Please use the following citation metadata."
repository-code: "https://github.com/kffl/gocannon"
title: "Gocannon - Performance-focused HTTP benchmarking tool"
version: "1.2.1"
version: "1.3.0"
2 changes: 1 addition & 1 deletion args.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func parseArgs() (common.Config, error) {
Target: app.Arg("target", "HTTP target URL with port (i.e. http://localhost:80/test or https://host:443/x)").Required().String(),
}

app.Version("1.2.1")
app.Version("1.3.0")
_, err := app.Parse(os.Args[1:])
return config, err
}
17 changes: 8 additions & 9 deletions gocannon.go → lib/gocannon.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package main
package lib

import (
"fmt"
"os"
"sync"

"github.com/kffl/gocannon/common"
"github.com/valyala/fasthttp"
)

func exitWithError(err error) {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}

// Gocannon represents a single gocannon instance with a config defined upon its creation.
type Gocannon struct {
cfg common.Config
Expand Down Expand Up @@ -76,7 +69,13 @@ func (g Gocannon) Run() (TestResults, error) {
var end int64
if p != nil {
plugTarget, plugMethod, plugBody, plugHeaders := p.BeforeRequest(cid)
code, start, end = performRequest(c, plugTarget, plugMethod, plugBody, plugHeaders)
code, start, end = performRequest(
c,
plugTarget,
plugMethod,
plugBody,
plugHeaders,
)
} else {
code, start, end = performRequest(c, *g.cfg.Target, *g.cfg.Method, *g.cfg.Body, *g.cfg.Headers)
}
Expand Down
2 changes: 1 addition & 1 deletion http.go → lib/http.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package lib

import (
"crypto/tls"
Expand Down
2 changes: 1 addition & 1 deletion http_test.go → lib/http_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package lib

import (
"net/http"
Expand Down
21 changes: 16 additions & 5 deletions integration_test.go → lib/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package lib

import (
"math"
Expand Down Expand Up @@ -136,13 +136,19 @@ func TestGocannonDefaultValues(t *testing.T) {
if creationErr == nil {
results, execErr := g.Run()
assert.Nil(t, execErr, "the load test should be completed without errors")
assert.Greater(t, results.GetReqPerSec(), 100.0, "a throughput of at least 100 req/s should be achieved")
assert.Greater(
t,
results.GetReqPerSec(),
100.0,
"a throughput of at least 100 req/s should be achieved",
)
}
}

func TestGocanonWithPlugin(t *testing.T) {

err := exec.Command("go", "build", "-race", "-buildmode=plugin", "-o", "_example_plugin/plugin.so", "_example_plugin/plugin.go").Run()
err := exec.Command("go", "build", "-race", "-buildmode=plugin", "-o", "../_example_plugin/plugin.so", "../_example_plugin/plugin.go").
Run()

assert.Nil(t, err, "the plugin should compile without an error")

Expand All @@ -159,7 +165,7 @@ func TestGocanonWithPlugin(t *testing.T) {
header := common.RequestHeaders{}
trustAll := true
format := "json"
plugin := "_example_plugin/plugin.so"
plugin := "../_example_plugin/plugin.so"
target := "http://localhost:3000/hello"

cfg := common.Config{
Expand Down Expand Up @@ -189,7 +195,12 @@ func TestGocanonWithPlugin(t *testing.T) {

assert.Nil(t, execErr, "the load test should be completed without errors")

assert.Greater(t, results.GetReqPerSec(), 100.0, "a throughput of at least 100 req/s should be achieved")
assert.Greater(
t,
results.GetReqPerSec(),
100.0,
"a throughput of at least 100 req/s should be achieved",
)
}

}
2 changes: 1 addition & 1 deletion plugin_loader.go → lib/plugin_loader.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package lib

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion stats_collector.go → lib/stats_collector.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package lib

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion stats_collector_test.go → lib/stats_collector_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package lib

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion target_test.go → lib/target_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package lib

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion timestamp.go → lib/timestamp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package lib

import "time"

Expand Down
2 changes: 1 addition & 1 deletion timestamp_test.go → lib/timestamp_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package lib

import (
"testing"
Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ package main

import (
"fmt"
"os"
"runtime"

"github.com/kffl/gocannon/lib"
)

func exitWithError(err error) {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}

func main() {
config, err := parseArgs()
if err != nil {
Expand All @@ -17,7 +25,7 @@ func main() {
printHeader(config)
}

g, err := NewGocannon(config)
g, err := lib.NewGocannon(config)

if err != nil {
exitWithError(err)
Expand Down
11 changes: 9 additions & 2 deletions print.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import (
"runtime"

"github.com/kffl/gocannon/common"
"github.com/kffl/gocannon/lib"
)

func printHeader(cfg common.Config) {
fmt.Printf("Attacking %s with %d connections over %s using %d CPUs\n", *cfg.Target, *cfg.Connections, *cfg.Duration, runtime.GOMAXPROCS(0))
fmt.Printf(
"Attacking %s with %d connections over %s using %d CPUs\n",
*cfg.Target,
*cfg.Connections,
*cfg.Duration,
runtime.GOMAXPROCS(0),
)
}

func printSummary(s TestResults) {
func printSummary(s lib.TestResults) {
fmt.Printf("Total Req: %8d\n", s.GetReqCount())
fmt.Printf("Req/s: %11.2f\n", s.GetReqPerSec())
}

0 comments on commit e5cf194

Please sign in to comment.