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.
This commit is contained in:
Simon Eskildsen
2013-08-20 11:17:03 -04:00
parent 1579b4dcb0
commit 4f0eb93696
2 changed files with 9 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"regexp"
"math/rand" "math/rand"
"net/http" "net/http"
"os" "os"
@@ -29,10 +30,10 @@ func readTargets(source io.Reader) (Targets, error) {
lines := make([]string, 0) lines := make([]string, 0)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() 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 { if err := scanner.Err(); err != nil {
return Targets{}, err return Targets{}, err
@@ -41,6 +42,10 @@ func readTargets(source io.Reader) (Targets, error) {
return NewTargets(lines) return NewTargets(lines)
} }
func skipLine(line string) (bool) {
return regexp.MustCompile(`^\s*((\/\/)|$)`).MatchString(line)
}
// NewTargets instantiates Targets from a slice of strings // NewTargets instantiates Targets from a slice of strings
func NewTargets(lines []string) (Targets, error) { func NewTargets(lines []string) (Targets, error) {
targets := make([]*http.Request, 0) targets := make([]*http.Request, 0)

View File

@@ -7,7 +7,7 @@ import (
) )
func TestReadTargets(t *testing.T) { 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) targets, err := readTargets(lines)
if err != nil { if err != nil {
t.Fatalf("Couldn't parse valid source: %s", err) t.Fatalf("Couldn't parse valid source: %s", err)