diff --git a/speedtest.go b/speedtest.go index f90981a..44935f7 100644 --- a/speedtest.go +++ b/speedtest.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "log" "os" @@ -14,8 +15,14 @@ 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 as json").Bool() ) +type fullOutput struct { + UserInfo *speedtest.User `json:"user_info"` + Servers speedtest.Servers `json:"servers"` +} + func main() { kingpin.Version("1.1.2") kingpin.Parse() @@ -24,7 +31,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) @@ -36,15 +45,40 @@ func main() { targets, err := serverList.FindServer(*serverIds) checkError(err) - startTest(targets, *savingMode) + startTest(targets, *savingMode, *jsonOutput) + + if *jsonOutput { + jsonBytes, err := json.Marshal( + fullOutput{ + 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) @@ -55,7 +89,7 @@ func startTest(servers speedtest.Servers, savingMode bool) { showServerResult(s) } - if len(servers) > 1 { + if !jsonOuput && len(servers) > 1 { showAverageServerResult(servers) } } diff --git a/speedtest/server.go b/speedtest/server.go index a1e6fe1..f938001 100644 --- a/speedtest/server.go +++ b/speedtest/server.go @@ -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