Skip to content

Commit

Permalink
Merge pull request #38 from mumia/add_json_output
Browse files Browse the repository at this point in the history
Add ability to output results in json format
  • Loading branch information
showwin authored Apr 19, 2021
2 parents 628dedc + e0cc228 commit 786bbd6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Flags:
-l, --list Show available speedtest.net servers.
-s, --server=SERVER ... Select server id to speedtest.
--saving-mode Using less memory (≒10MB), though low accuracy (especially > 30Mbps).
--json Output results as json
--version Show application version.
```

Expand Down
52 changes: 47 additions & 5 deletions speedtest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"log"
"os"
Expand All @@ -14,8 +15,16 @@ var (
showList = kingpin.Flag("list", "Show available speedtest.net servers.").Short('l').Bool()
serverIds = kingpin.Flag("server", "Select server id to speedtest.").Short('s').Ints()
savingMode = kingpin.Flag("saving-mode", "Using less memory (≒10MB), though low accuracy (especially > 30Mbps).").Bool()
jsonOutput = kingpin.Flag("json", "Output results in json format").Bool()
)

type fullOutput struct {
Timestamp outputTime `json:"timestamp"`
UserInfo *speedtest.User `json:"user_info"`
Servers speedtest.Servers `json:"servers"`
}
type outputTime time.Time

func main() {
kingpin.Version("1.1.2")
kingpin.Parse()
Expand All @@ -24,7 +33,9 @@ func main() {
if err != nil {
fmt.Println("Warning: Cannot fetch user information. http://www.speedtest.net/speedtest-config.php is temporarily unavailable.")
}
showUser(user)
if !*jsonOutput {
showUser(user)
}

serverList, err := speedtest.FetchServerList(user)
checkError(err)
Expand All @@ -36,15 +47,41 @@ func main() {
targets, err := serverList.FindServer(*serverIds)
checkError(err)

startTest(targets, *savingMode)
startTest(targets, *savingMode, *jsonOutput)

if *jsonOutput {
jsonBytes, err := json.Marshal(
fullOutput{
Timestamp: outputTime(time.Now()),
UserInfo: user,
Servers: targets,
},
)
checkError(err)

fmt.Println(string(jsonBytes))
}
}

func startTest(servers speedtest.Servers, savingMode bool) {
func startTest(servers speedtest.Servers, savingMode bool, jsonOuput bool) {
for _, s := range servers {
showServer(s)
if !jsonOuput {
showServer(s)
}

err := s.PingTest()
checkError(err)

if jsonOuput {
err := s.DownloadTest(savingMode)
checkError(err)

err = s.UploadTest(savingMode)
checkError(err)

continue
}

showLatencyResult(s)

err = testDownload(s, savingMode)
Expand All @@ -55,7 +92,7 @@ func startTest(servers speedtest.Servers, savingMode bool) {
showServerResult(s)
}

if len(servers) > 1 {
if !jsonOuput && len(servers) > 1 {
showAverageServerResult(servers)
}
}
Expand Down Expand Up @@ -150,3 +187,8 @@ func checkError(err error) {
os.Exit(1)
}
}

func (t outputTime) MarshalJSON() ([]byte, error) {
stamp := fmt.Sprintf("\"%s\"", time.Time(t).Format("2006-01-02 15:04:05.000"))
return []byte(stamp), nil
}
28 changes: 14 additions & 14 deletions speedtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ package speedtest
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"math"
"net/http"
"sort"
"strconv"
"errors"
"time"
)

// Server information
type Server struct {
URL string `xml:"url,attr"`
Lat string `xml:"lat,attr"`
Lon string `xml:"lon,attr"`
Name string `xml:"name,attr"`
Country string `xml:"country,attr"`
Sponsor string `xml:"sponsor,attr"`
ID string `xml:"id,attr"`
URL2 string `xml:"url2,attr"`
Host string `xml:"host,attr"`
Distance float64
Latency time.Duration
DLSpeed float64
ULSpeed float64
URL string `xml:"url,attr" json:"url"`
Lat string `xml:"lat,attr" json:"lat"`
Lon string `xml:"lon,attr" json:"lon"`
Name string `xml:"name,attr" json:"name"`
Country string `xml:"country,attr" json:"country"`
Sponsor string `xml:"sponsor,attr" json:"sponsor"`
ID string `xml:"id,attr" json:"id"`
URL2 string `xml:"url2,attr" json:"url_2"`
Host string `xml:"host,attr" json:"host"`
Distance float64 `json:"distance"`
Latency time.Duration `json:"latency"`
DLSpeed float64 `json:"dl_speed"`
ULSpeed float64 `json:"ul_speed"`
}

// ServerList list of Server
Expand Down

0 comments on commit 786bbd6

Please sign in to comment.