Skip to content

Commit

Permalink
Improve csv output
Browse files Browse the repository at this point in the history
  • Loading branch information
pgilad committed Mar 13, 2018
1 parent adfbcf7 commit aa9768d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ Parse JMeter JTL files, supporting:
- Cookies
- More...

The parser is a **stream decoder**, meaning it's safe to use
for very large files
The parser is a **stream decoder**, meaning it's safe to use for very large files

## Usage

Expand Down Expand Up @@ -45,10 +44,12 @@ $ ./compare.sh data.jtl

- CSV

Currently CSV output is not configurable, and outputs only:
- label
- timestamp
- latency
Sample CSV output:
```csv
Label,Timestamp,Response Time,Latency,Users
Label1,1519028940050,1205,1205,1
Label2,1519028941268,93,93,1
```

## License

Expand Down
8 changes: 4 additions & 4 deletions jtl/jtl.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,25 @@ type HttpSample struct {
NestedSamples
}

func (s Sample) MarshalJSON() ([]byte, error) {
func (sample Sample) MarshalJSON() ([]byte, error) {
type Alias Sample
return json.Marshal(&struct {
Type string `json:"type"`
Alias
}{
Type: "sample",
Alias: (Alias)(s),
Alias: (Alias)(sample),
})
}

func (s HttpSample) MarshalJSON() ([]byte, error) {
func (sample HttpSample) MarshalJSON() ([]byte, error) {
type Alias HttpSample
return json.Marshal(&struct {
Type string `json:"type"`
Alias
}{
Type: "httpSample",
Alias: (Alias)(s),
Alias: (Alias)(sample),
})
}

Expand Down
34 changes: 29 additions & 5 deletions output/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func CSV(output <-chan interface{}) {
defer writer.Flush()

// write csv header
writer.Write([]string{"Label", "Timestamp", "Latency", "Users"})
writer.Write([]string{"Label", "Timestamp", "Response Time", "Latency", "Users"})

for {
element, more := <-output
Expand All @@ -33,8 +33,32 @@ func CSV(output <-chan interface{}) {
}
}

func writeHttpSample(sample jtl.HttpSample, writer *csv.Writer) {
data := []string{*sample.Label, strconv.FormatUint(*sample.Timestamp, 10), strconv.Itoa(*sample.Latency), strconv.Itoa(*sample.NA)}
func createCSVRow(sample interface{}) []string {
// TODO: remove duplication
switch s := sample.(type) {
case jtl.Sample:
return []string{
*s.Label,
strconv.FormatUint(*s.Timestamp, 10),
strconv.Itoa(*s.ElapsedTime),
strconv.Itoa(*s.ElapsedTime),
strconv.Itoa(*s.NA),
}
case jtl.HttpSample:
return []string{
*s.Label,
strconv.FormatUint(*s.Timestamp, 10),
strconv.Itoa(*s.ElapsedTime),
strconv.Itoa(*s.ElapsedTime),
strconv.Itoa(*s.NA),
}
default:
return []string{}
}
}

func writeSample(sample jtl.Sample, writer *csv.Writer) {
data := createCSVRow(sample)
writer.Write(data)
for _, row := range sample.Samples {
writeSample(row, writer)
Expand All @@ -44,8 +68,8 @@ func writeHttpSample(sample jtl.HttpSample, writer *csv.Writer) {
}
}

func writeSample(sample jtl.Sample, writer *csv.Writer) {
data := []string{*sample.Label, strconv.FormatUint(*sample.Timestamp, 10), strconv.Itoa(*sample.Latency), strconv.Itoa(*sample.NA)}
func writeHttpSample(sample jtl.HttpSample, writer *csv.Writer) {
data := createCSVRow(sample)
writer.Write(data)
for _, row := range sample.Samples {
writeSample(row, writer)
Expand Down

0 comments on commit aa9768d

Please sign in to comment.