Running Ad-hoc Network Speed Tests through the API

NetBeez’s BeezKeeper API in v.13.0 introduces a new set of endpoints designed to execute network speed tests. With these API calls, you can seamlessly integrate on-demand network speed tests into third-party dashboards.

How to Use the API

1. Setting Up the API Token and Base URL

First, set up the API token and base URL on your application.

import requests

API_TOKEN = "your_api_token_here"
BASE_URL = f"https://{HOSTNAME}"
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}

2. Create a New Multiagent Network Speed Test

Make a POST request to /multiagent_nb_test_runs/ad_hoc to initiate the test.

test_type_id = 7  # ID for network speed test
agent_ids = [1, 2]  # IDs of the agents performing the test

payload = {
	"data": {
		"type": "multiagent_nb_test_run",
		"attributes": {
			"speedtest_type": 2,
			"target": ""
		},
		"relationships": {
			"agents": {
				"data": [
					{
						"id": 279,
						"type": "agent"
					},
					{
						"id": 280,
						"type": "agent"
					}
				]
			},
			"test_type": {
				"data": {
					"id": 7,
					"type": "test_type"
				}
			}
		}
	}
}

response = requests.post(
    f"{BASE_URL}/multiagent_nb_test_runs/ad_hoc",
    headers=HEADERS,
    json=payload
)

# Record the test_run_id from the response
test_run_id = response.json()["data"]["id"]

3. Poll for Test Results

After creating the test, continuously poll for its results.

import time

while True:
    response = requests.get(
        f"{BASE_URL}/multiagent_nb_test_runs?filter[multiagent_nb_test_runs]={test_run_id}&include=results",
        headers=HEADERS
    )

    data = response.json()

    if data["data"][0]["attributes"]["state"] == "completed":
        break

    time.sleep(5)  # Adjust sleep time as needed

# Extract results
results = data["included"]

Putting it All Together

Below is a complete Python script that combines all the steps. This script initializes the API token, triggers a network speed test, and polls for the results.

import requests
import time

def run_speed_test():
    # Initialize API Token and Base URL
    API_TOKEN = "your_api_token_here"
    BASE_URL = f"https://{HOSTNAME}"
    HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
    
    # Create a New Multiagent Network Speed Test
    test_type_id = 7  # ID for network speed test
    agent_ids = [279,280]  # IDs of the agents performing the test
    
    payload = {
		"data": {
			"type": "multiagent_nb_test_run",
			"attributes": {
				"speedtest_type": 2,
				"target": ""
			},
			"relationships": {
				"agents": {
					"data": [{"id": agent_id, "type":"agent"} for agent_id in agent_ids]
				},
				"test_type": {
					"data": {
						"id": 7,
						"type": "test_type"
					}
				}
			}
		}
	}

    response = requests.post(
        f"{BASE_URL}/multiagent_nb_test_runs/ad_hoc",
        headers=HEADERS,
        json=payload
    )
    
    test_run_id = response.json()["id"]
    
    # Poll for Test Results
    while True:
        response = requests.get(
            f"{BASE_URL}/multiagent_nb_test_runs?filter[multiagent_nb_test_runs]={test_run_id}&include=results",
            headers=HEADERS
        )
        
        data = response.json()
        
        if data["data"][0]["attributes"]["state"] == "completed":
            break
        
        time.sleep(5)  # Adjust sleep time as needed
    
    results = data["results"]
    return results

# Run the speed test and print the results
if __name__ == "__main__":
    test_results = run_speed_test()
    print("Test Results:", test_results)

This script can be integrated into your application, and it allows you to trigger network speed tests programmatically. Simply copy, paste, and run this code, and you’ll get your network speed test results directly in your Python environment. We also created a convenient CoLab Notebook for you to use.

Summary

We’ve walked through how to set up and run ad-hoc network speed tests through the new BeezKeeper API endpoints. This makes it easy to integrate these actions into any third-party dashboards for on-demand execution.

If you have any questions or need further clarification, feel free to ask… and stay tuned for the updated documentation which we will be publishing very soon at https://api.netbeez.net.

A few questions:

  1. Is the ‘test_type’ always 7 for a network speed test?
  2. What is ‘speedtest_type’?

Thanks!

Hi Will! Thank you for the question!

Here are the answers:

  1. Yes, network speed tests always have a test_type_id of 7.
  2. speedtest_type is a number. Choose 2 for an NDT test and 3 for a fast.com test.

Let me know if you have any other questions!