Skip to content

Commit 6523c87

Browse files
authored
feat: add jsonl output support (#251)
1 parent d41df68 commit 6523c87

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Flags:
4141
--custom-url=CUSTOM-URL Specify the url of the server instead of fetching from speedtest.net.
4242
--saving-mode Test with few resources, though low accuracy (especially > 30Mbps).
4343
--json Output results in json format.
44+
--jsonl Output results in jsonl format (one json object per line).
4445
--unix Output results in unix like format.
4546
--location=LOCATION Change the location with a precise coordinate (format: lat,lon).
4647
--city=CITY Change the location with a predefined city label.

speedtest.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"github.com/showwin/speedtest-go/speedtest/transport"
8-
"gopkg.in/alecthomas/kingpin.v2"
97
"io"
108
"log"
119
"os"
@@ -15,6 +13,9 @@ import (
1513
"sync/atomic"
1614
"time"
1715

16+
"github.com/showwin/speedtest-go/speedtest/transport"
17+
"gopkg.in/alecthomas/kingpin.v2"
18+
1819
"github.com/showwin/speedtest-go/speedtest"
1920
)
2021

@@ -24,6 +25,7 @@ var (
2425
customURL = kingpin.Flag("custom-url", "Specify the url of the server instead of fetching from speedtest.net.").String()
2526
savingMode = kingpin.Flag("saving-mode", "Test with few resources, though low accuracy (especially > 30Mbps).").Bool()
2627
jsonOutput = kingpin.Flag("json", "Output results in json format.").Bool()
28+
jsonlOutput = kingpin.Flag("jsonl", "Output results in jsonl format (one json object per line).").Bool()
2729
unixOutput = kingpin.Flag("unix", "Output results in unix like format.").Bool()
2830
location = kingpin.Flag("location", "Change the location with a precise coordinate (format: lat,lon).").String()
2931
city = kingpin.Flag("city", "Change the location with a predefined city label.").String()
@@ -58,7 +60,7 @@ func main() {
5860
log.SetOutput(io.Discard)
5961

6062
// start unix output for saving mode by default.
61-
if *savingMode && !*jsonOutput && !*unixOutput {
63+
if *savingMode && !*jsonOutput && !*jsonlOutput && !*unixOutput {
6264
*unixOutput = true
6365
}
6466

@@ -84,7 +86,7 @@ func main() {
8486
}
8587

8688
// 1. retrieving user information
87-
taskManager := InitTaskManager(*jsonOutput, *unixOutput)
89+
taskManager := InitTaskManager(*jsonOutput || *jsonlOutput, *unixOutput)
8890
taskManager.AsyncRun("Retrieving User Information", func(task *Task) {
8991
u, err := speedtestClient.FetchUserInfo()
9092
task.CheckError(err)
@@ -133,7 +135,7 @@ func main() {
133135

134136
// 3. test each selected server with ping, download and upload.
135137
for _, server := range targets {
136-
if !*jsonOutput {
138+
if !*jsonOutput && !*jsonlOutput {
137139
fmt.Println()
138140
}
139141
taskManager.Println("Test Server: " + server.String())
@@ -216,7 +218,7 @@ func main() {
216218
}
217219
packetLossAnalyzerCancel()
218220
blocker.Wait()
219-
if !*jsonOutput {
221+
if !*jsonOutput && !*jsonlOutput {
220222
taskManager.Println(server.PacketLoss.String())
221223
}
222224
taskManager.Reset()
@@ -230,6 +232,14 @@ func main() {
230232
panic(errMarshal)
231233
}
232234
fmt.Print(string(json))
235+
} else if *jsonlOutput {
236+
for _, server := range targets {
237+
json, errMarshal := speedtestClient.JSONL(server)
238+
if errMarshal != nil {
239+
panic(errMarshal)
240+
}
241+
fmt.Println(string(json))
242+
}
233243
}
234244
}
235245

@@ -321,7 +331,7 @@ func parseProto(str string) speedtest.Proto {
321331
}
322332

323333
func AppInfo() {
324-
if !*jsonOutput {
334+
if !*jsonOutput && !*jsonlOutput {
325335
fmt.Println()
326336
fmt.Printf(" speedtest-go v%s (git-%s) @showwin\n", speedtest.Version(), commit)
327337
fmt.Println()

speedtest/output.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ type fullOutput struct {
1212
Servers Servers `json:"servers"`
1313
}
1414

15+
type singleServerOutput struct {
16+
Timestamp outputTime `json:"timestamp"`
17+
UserInfo *User `json:"user_info"`
18+
Server *Server `json:"server"`
19+
}
20+
1521
type outputTime time.Time
1622

1723
func (t outputTime) MarshalJSON() ([]byte, error) {
@@ -28,3 +34,14 @@ func (s *Speedtest) JSON(servers Servers) ([]byte, error) {
2834
},
2935
)
3036
}
37+
38+
// JSONL outputs a single server result in JSON format
39+
func (s *Speedtest) JSONL(server *Server) ([]byte, error) {
40+
return json.Marshal(
41+
singleServerOutput{
42+
Timestamp: outputTime(time.Now()),
43+
UserInfo: s.User,
44+
Server: server,
45+
},
46+
)
47+
}

0 commit comments

Comments
 (0)