Beispiel #1
1
func runTest(c *cli.Context) {
	// create our server object and load initial config
	var testServer sthttp.Server
	sthttp.CONFIG = sthttp.GetConfig()

	// if we are *not* running a report then say hello to everyone
	if !debug.REPORT {
		fmt.Printf("github.com/zpeters/speedtest -- unofficial cli for speedtest.net\n")
	}

	// if we are in debug mode print outa an environment report
	if debug.DEBUG {
		print.EnvironmentReport(c)
	}

	// get all possible servers
	if debug.DEBUG {
		log.Printf("Getting all servers for our test list")
	}
	var allServers []sthttp.Server
	if c.String("mini") == "" {
		allServers = sthttp.GetServers()
	}

	// if a mini speedtest installation was specified, use that...
	if c.String("mini") != "" {
		//construct testserver object manually
		u, err := url.Parse(c.String("mini"))

		if err != nil {
			log.Fatalf("Speedtest mini server URL is not a valid URL: %s", err)
		}

		if debug.DEBUG {
			log.Printf("Using Mini Server '%s'", c.String("mini"))
		}
		testServer.URL = c.String("mini")
		if !strings.HasSuffix(c.String("mini"), "/") {
			testServer.URL += "/"
		}
		testServer.URL += "speedtest/upload.php"
		testServer.Name = u.Host
		testServer.Sponsor = "speedtest-mini"
		testServer.ID = "0"

		testServer.Latency = sthttp.GetLatency(testServer)

		// if they specified a specific speedtest.net server, test against that...
	} else if c.String("server") != "" {
		if debug.DEBUG {
			log.Printf("Server '%s' specified, getting info...", c.String("server"))
		}
		// find server and load latency report
		testServer = tests.FindServer(c.String("server"), allServers)
		// load latency
		testServer.Latency = sthttp.GetLatency(testServer)

		if !debug.REPORT {
			fmt.Printf("Server: %s - %s (%s)\n", testServer.ID, testServer.Name, testServer.Sponsor)
		}

		// ...otherwise get a list of all servers sorted by distance...
	} else {
		if debug.DEBUG {
			log.Printf("Getting closest servers...")
		}
		closestServers := sthttp.GetClosestServers(allServers)
		if debug.DEBUG {
			log.Printf("Getting the fastests of our closest servers...")
		}
		// ... and get the fastests NUMCLOSEST ones
		testServer = sthttp.GetFastestServer(closestServers)
	}

	// if ping only then just output latency results and exit nicely...
	if c.Bool("ping") {
		if c.Bool("report") {
			if settings.ALGOTYPE == "max" {
				fmt.Printf("%3.2f (Lowest)\n", testServer.Latency)
			} else {
				fmt.Printf("%3.2f (Avg)\n", testServer.Latency)
			}
		} else {
			if settings.ALGOTYPE == "max" {
				fmt.Printf("Ping (Lowest): %3.2f ms\n", testServer.Latency)
			} else {
				fmt.Printf("Ping (Avg): %3.2f ms\n", testServer.Latency)
			}
		}
		os.Exit(0)
		// ...otherwise run our full test
	} else {

		var dmbps float64
		var umbps float64

		// get our upload and/or download speeds
		if c.Bool("downloadonly") {
			dmbps = tests.DownloadTest(testServer)
		} else if c.Bool("uploadonly") {
			umbps = tests.UploadTest(testServer)
		} else {
			dmbps = tests.DownloadTest(testServer)
			umbps = tests.UploadTest(testServer)
		}

		if !debug.REPORT {
			if settings.ALGOTYPE == "max" {
				fmt.Printf("Ping (Lowest): %3.2f ms | Download (Max): %3.2f Mbps | Upload (Max): %3.2f Mbps\n", testServer.Latency, dmbps, umbps)
			} else {
				fmt.Printf("Ping (Avg): %3.2f ms | Download (Avg): %3.2f Mbps | Upload (Avg): %3.2f Mbps\n", testServer.Latency, dmbps, umbps)
			}
		} else {
			//print.ServerReport(testServer)
			dkbps := dmbps * 1000
			ukbps := umbps * 1000

			fmt.Printf("%s%s%s%s%s(%s,%s)%s", time.Now().Format("2006-01-02 15:04:05 -0700"), settings.REPORTCHAR, testServer.ID, settings.REPORTCHAR, testServer.Sponsor, testServer.Name, testServer.Country, settings.REPORTCHAR)
			fmt.Printf("%3.2f%s%d%s%d\n", testServer.Latency, settings.REPORTCHAR, int(dkbps), settings.REPORTCHAR, int(ukbps))
		}
	}

}
Beispiel #2
0
// ListServers prints a list of all "global" servers
func ListServers() {
	if debug.DEBUG {
		fmt.Printf("Loading config from speedtest.net\n")
	}
	sthttp.CONFIG = sthttp.GetConfig()
	if debug.DEBUG {
		fmt.Printf("\n")
	}

	if debug.DEBUG {
		fmt.Printf("Getting servers list...")
	}
	allServers := sthttp.GetServers()
	if debug.DEBUG {
		fmt.Printf("(%d) found\n", len(allServers))
	}
	for s := range allServers {
		server := allServers[s]
		print.Server(server)
	}
}