From 4f0eb9369673bce69e7edcbaef974ff9a2f4d2fe Mon Sep 17 00:00:00 2001 From: Simon Eskildsen Date: Tue, 20 Aug 2013 11:17:03 -0400 Subject: [PATCH 1/2] Add handling of comments in targets.txt Skips lines that start with `//`, to allow commments in `targets.txt`. This is handy when you want to attach a comment on why a specific endpoint is tested, in a `targets.txt` used as part of a benchmark suite. --- lib/targets.go | 11 ++++++++--- lib/targets_test.go | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/targets.go b/lib/targets.go index cff6b5c..586d065 100644 --- a/lib/targets.go +++ b/lib/targets.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io" + "regexp" "math/rand" "net/http" "os" @@ -29,10 +30,10 @@ func readTargets(source io.Reader) (Targets, error) { lines := make([]string, 0) for scanner.Scan() { line := scanner.Text() - if line = strings.TrimSpace(line); line == "" { // Empty line - continue + + if !skipLine(line) { // Not a comment or blank line + lines = append(lines, line) } - lines = append(lines, line) } if err := scanner.Err(); err != nil { return Targets{}, err @@ -41,6 +42,10 @@ func readTargets(source io.Reader) (Targets, error) { return NewTargets(lines) } +func skipLine(line string) (bool) { + return regexp.MustCompile(`^\s*((\/\/)|$)`).MatchString(line) +} + // NewTargets instantiates Targets from a slice of strings func NewTargets(lines []string) (Targets, error) { targets := make([]*http.Request, 0) diff --git a/lib/targets_test.go b/lib/targets_test.go index 17947ef..9d113c7 100644 --- a/lib/targets_test.go +++ b/lib/targets_test.go @@ -7,7 +7,7 @@ import ( ) func TestReadTargets(t *testing.T) { - lines := bytes.NewBufferString("GET http://lolcathost:9999/\n\nHEAD http://lolcathost:9999/\n") + lines := bytes.NewBufferString("GET http://lolcathost:9999/\n\n // HEAD http://lolcathost.com this is a comment \nHEAD http://lolcathost:9999/\n") targets, err := readTargets(lines) if err != nil { t.Fatalf("Couldn't parse valid source: %s", err) From 1dadca892af4dabb7d2093bb31ef44bf430863e6 Mon Sep 17 00:00:00 2001 From: Simon Eskildsen Date: Wed, 21 Aug 2013 13:04:52 -0400 Subject: [PATCH 2/2] Remove regexp for comment detection --- lib/targets.go | 9 ++------- lib/targets_test.go | 2 +- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/targets.go b/lib/targets.go index 586d065..aa550c1 100644 --- a/lib/targets.go +++ b/lib/targets.go @@ -4,7 +4,6 @@ import ( "bufio" "fmt" "io" - "regexp" "math/rand" "net/http" "os" @@ -31,8 +30,8 @@ func readTargets(source io.Reader) (Targets, error) { for scanner.Scan() { line := scanner.Text() - if !skipLine(line) { // Not a comment or blank line - lines = append(lines, line) + if line = strings.TrimSpace(line); line != "" && line[0:2] != "//" { // A comment or blank line + lines = append(lines, line) } } if err := scanner.Err(); err != nil { @@ -42,10 +41,6 @@ func readTargets(source io.Reader) (Targets, error) { return NewTargets(lines) } -func skipLine(line string) (bool) { - return regexp.MustCompile(`^\s*((\/\/)|$)`).MatchString(line) -} - // NewTargets instantiates Targets from a slice of strings func NewTargets(lines []string) (Targets, error) { targets := make([]*http.Request, 0) diff --git a/lib/targets_test.go b/lib/targets_test.go index 9d113c7..6b7e99e 100644 --- a/lib/targets_test.go +++ b/lib/targets_test.go @@ -7,7 +7,7 @@ import ( ) func TestReadTargets(t *testing.T) { - lines := bytes.NewBufferString("GET http://lolcathost:9999/\n\n // HEAD http://lolcathost.com this is a comment \nHEAD http://lolcathost:9999/\n") + lines := bytes.NewBufferString("GET http://lolcathost:9999/\n\n // HEAD http://lolcathost.com this is a comment \nHEAD http://lolcathost:9999/\n") targets, err := readTargets(lines) if err != nil { t.Fatalf("Couldn't parse valid source: %s", err)