Skip to content

Commit aa9768d

Browse files
committed
Improve csv output
1 parent adfbcf7 commit aa9768d

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ Parse JMeter JTL files, supporting:
1010
- Cookies
1111
- More...
1212

13-
The parser is a **stream decoder**, meaning it's safe to use
14-
for very large files
13+
The parser is a **stream decoder**, meaning it's safe to use for very large files
1514

1615
## Usage
1716

@@ -45,10 +44,12 @@ $ ./compare.sh data.jtl
4544

4645
- CSV
4746

48-
Currently CSV output is not configurable, and outputs only:
49-
- label
50-
- timestamp
51-
- latency
47+
Sample CSV output:
48+
```csv
49+
Label,Timestamp,Response Time,Latency,Users
50+
Label1,1519028940050,1205,1205,1
51+
Label2,1519028941268,93,93,1
52+
```
5253

5354
## License
5455

jtl/jtl.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,25 @@ type HttpSample struct {
7979
NestedSamples
8080
}
8181

82-
func (s Sample) MarshalJSON() ([]byte, error) {
82+
func (sample Sample) MarshalJSON() ([]byte, error) {
8383
type Alias Sample
8484
return json.Marshal(&struct {
8585
Type string `json:"type"`
8686
Alias
8787
}{
8888
Type: "sample",
89-
Alias: (Alias)(s),
89+
Alias: (Alias)(sample),
9090
})
9191
}
9292

93-
func (s HttpSample) MarshalJSON() ([]byte, error) {
93+
func (sample HttpSample) MarshalJSON() ([]byte, error) {
9494
type Alias HttpSample
9595
return json.Marshal(&struct {
9696
Type string `json:"type"`
9797
Alias
9898
}{
9999
Type: "httpSample",
100-
Alias: (Alias)(s),
100+
Alias: (Alias)(sample),
101101
})
102102
}
103103

output/csv.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func CSV(output <-chan interface{}) {
1313
defer writer.Flush()
1414

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

1818
for {
1919
element, more := <-output
@@ -33,8 +33,32 @@ func CSV(output <-chan interface{}) {
3333
}
3434
}
3535

36-
func writeHttpSample(sample jtl.HttpSample, writer *csv.Writer) {
37-
data := []string{*sample.Label, strconv.FormatUint(*sample.Timestamp, 10), strconv.Itoa(*sample.Latency), strconv.Itoa(*sample.NA)}
36+
func createCSVRow(sample interface{}) []string {
37+
// TODO: remove duplication
38+
switch s := sample.(type) {
39+
case jtl.Sample:
40+
return []string{
41+
*s.Label,
42+
strconv.FormatUint(*s.Timestamp, 10),
43+
strconv.Itoa(*s.ElapsedTime),
44+
strconv.Itoa(*s.ElapsedTime),
45+
strconv.Itoa(*s.NA),
46+
}
47+
case jtl.HttpSample:
48+
return []string{
49+
*s.Label,
50+
strconv.FormatUint(*s.Timestamp, 10),
51+
strconv.Itoa(*s.ElapsedTime),
52+
strconv.Itoa(*s.ElapsedTime),
53+
strconv.Itoa(*s.NA),
54+
}
55+
default:
56+
return []string{}
57+
}
58+
}
59+
60+
func writeSample(sample jtl.Sample, writer *csv.Writer) {
61+
data := createCSVRow(sample)
3862
writer.Write(data)
3963
for _, row := range sample.Samples {
4064
writeSample(row, writer)
@@ -44,8 +68,8 @@ func writeHttpSample(sample jtl.HttpSample, writer *csv.Writer) {
4468
}
4569
}
4670

47-
func writeSample(sample jtl.Sample, writer *csv.Writer) {
48-
data := []string{*sample.Label, strconv.FormatUint(*sample.Timestamp, 10), strconv.Itoa(*sample.Latency), strconv.Itoa(*sample.NA)}
71+
func writeHttpSample(sample jtl.HttpSample, writer *csv.Writer) {
72+
data := createCSVRow(sample)
4973
writer.Write(data)
5074
for _, row := range sample.Samples {
5175
writeSample(row, writer)

0 commit comments

Comments
 (0)