LibreSpeed Tracker is a Node.js script designed to run on Qinglong Panel, which performs network speed tests by calling the backend API of target LibreSpeed sites and stores the results in a MySQL database. The script supports sites protected by nginx's basic_auth and can test multiple network performance metrics.
- Multi-server Testing: Test multiple LibreSpeed servers configured in the configuration file
- HTTP Basic Authentication: Access sites that require username and password authentication
- Comprehensive Metrics: Test download speed, upload speed, ping, jitter, and server information
- MySQL Database Storage: Automatically create tables and store test results
- Environment Variable Support: Configure database connections and server settings via environment variables
- Qinglong Panel Integration: Optimized for running on Qinglong Panel with scheduled tasks
- Error Handling: Robust error handling with partial results when tests fail
- Clone or download the project files
- Install dependencies:
npm install
- Configure the settings by copying
config.example.jstoconfig.jsand modifying it, or use environment variables - Run the script:
node index.js
Create a config.js file based on config.example.js:
module.exports = {
// MySQL database configuration
database: {
host: 'localhost',
port: 3306,
database: 'speedtest_results',
username: 'root',
password: 'password',
// Connection pool configuration
pool: {
min: 2,
max: 10,
idle: 30000,
acquire: 60000
}
},
// Server configuration
servers: [
{
name: "LibreSpeed Official Test",
url: "https://librespeed.org/",
username: "",
password: ""
},
{
name: "Local Test Server",
url: "http://localhost/speedtest/",
username: "admin",
password: "password123"
}
],
// Test parameters
test: {
downloadSize: 50, // Download test data size (MB)
uploadSize: 10, // Upload test data size (MB)
smallUploadSize: 1, // Small data packet size for upload test fallback (MB)
pingCount: 10, // Number of ping tests
downloadTimeout: 60, // Download test timeout (seconds)
uploadTimeout: 60, // Upload test timeout (seconds)
pingTimeout: 5 // Ping test timeout (seconds)
}
};Alternatively, you can configure the script using environment variables:
# MySQL database connection
export DB_HOST=localhost
export DB_PORT=3306
export DB_NAME=speedtest_results
export DB_USER=root
export DB_PASSWORD=password
# Connection pool configuration
export DB_POOL_MIN=2
export DB_POOL_MAX=10
export DB_POOL_IDLE=30000
export DB_POOL_ACQUIRE=60000
# Server configuration
export SPEEDTEST_SERVERS='[{"name":"Test Server","url":"https://example.com/","username":"","password":""}]'The script automatically creates the following table in the MySQL database:
CREATE TABLE IF NOT EXISTS speedtest_results (
id INT AUTO_INCREMENT PRIMARY KEY,
server_name VARCHAR(255) NOT NULL,
server_url VARCHAR(255) NOT NULL,
test_timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
download_speed FLOAT,
upload_speed FLOAT,
ping FLOAT,
jitter FLOAT,
server_info JSON
);The script provides the following methods:
testAllServers(): Test all servers configured in the configuration filetestServer(serverConfig): Test a specific serversaveResult(result): Save test results to the databaseinitDatabase(): Initialize database connection and table structuregetHistoryResults(limit): Get recent test resultscloseDatabaseConnection(): Close the database connection
const { testAllServers } = require('./index.js');
// Test all servers and save results to database
testAllServers()
.then(results => {
console.log('All tests completed:', results);
})
.catch(error => {
console.error('Test failed:', error);
});const { testServer, saveResult, initDatabase, getHistoryResults, closeDatabaseConnection } = require('./index.js');
// Initialize database
await initDatabase();
// Test a specific server
const serverConfig = {
name: "Custom Server",
url: "https://example.com/speedtest/",
username: "user",
password: "pass"
};
const result = await testServer(serverConfig);
await saveResult(result);
// Get historical results
const history = await getHistoryResults(10);
console.log('Recent test results:', history);
// Close database connection when done
await closeDatabaseConnection();- Upload all project files to the Qinglong Panel script directory
- Install dependencies in the Qinglong Panel script management:
npm install axios mysql2 - Configure environment variables in the Qinglong Panel environment variable management
- Create a scheduled task with the command:
node index.js
DB_HOST: Database host addressDB_PORT: Database portDB_NAME: Database nameDB_USER: Database usernameDB_PASSWORD: Database passwordSPEEDTEST_SERVERS: JSON formatted server configuration array
axios: HTTP client for API requests and basic authenticationmysql2: MySQL database client
MIT
The project includes a comprehensive test suite to ensure functionality and reliability:
To run all tests, execute the following commands:
# Install dependencies (if not already installed)
npm install
# Run database connection tests
node test/test-db-connection.js
# Run server connection tests
node test/test-server-connection.js
# Run logger tests
node test/test-logger.js
# Run history function tests
node test/test-history.js
# Run result verification tests
node test/verify-results.js- test/test-db-connection.js: Tests database connection, verifies database creation, and checks table structure.
- test/test-server-connection.js: Tests connections to LibreSpeed servers, verifies API endpoints, and checks basic authentication.
- test/test-logger.js: Tests the logging functionality, including info, error, and warning messages.
- test/test-history.js: Tests the history retrieval functionality, including filtering and sorting of results.
- test/test-utils.js: Provides utility functions for database testing, including setup and teardown.
- test/verify-results.js: Verifies the accuracy and completeness of test results.
Before running tests, ensure:
- MySQL server is running and accessible
- Database credentials are correctly configured in
config.example.js(copied toconfig.js) - All dependencies are installed (
npm install)
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
LibreSpeed Tracker 是一个 Node.js 脚本,设计用于在青龙面板上运行,通过调用目标 LibreSpeed 站点的后端 API 进行网络速度测试,并将结果存储到 MySQL 数据库中。该脚本支持受 nginx basic_auth 保护的站点,并能够测试多项网络性能指标。
- 多服务器测试:测试配置文件中的多个 LibreSpeed 服务器
- HTTP 基本认证:访问需要用户名和密码认证的站点
- 全面指标:测试下载速度、上传速度、延迟、抖动和服务器信息
- MySQL 数据库存储:自动创建表并存储测试结果
- 环境变量支持:通过环境变量配置数据库连接和服务器设置
- 青龙面板集成:优化为在青龙面板上运行,支持定时任务
- 错误处理:强大的错误处理机制,测试失败时返回部分结果
- 克隆或下载项目文件
- 安装依赖:
npm install
- 通过复制
config.example.js为config.js并修改它来配置设置,或使用环境变量 - 运行脚本:
node index.js
基于 config.example.js 创建一个 config.js 文件:
module.exports = {
// MySQL数据库配置
database: {
host: 'localhost',
port: 3306,
database: 'speedtest_results',
username: 'root',
password: 'password',
// 连接池配置
pool: {
min: 2,
max: 10,
idle: 30000,
acquire: 60000
}
},
// 服务器配置
servers: [
{
name: "LibreSpeed官方测试",
url: "https://librespeed.org/",
username: "",
password: ""
},
{
name: "本地测试服务器",
url: "http://localhost/speedtest/",
username: "admin",
password: "password123"
}
],
// 测试参数
test: {
downloadSize: 50, // 下载测试数据包大小(MB)
uploadSize: 10, // 上传测试数据包大小(MB)
smallUploadSize: 1, // 上传测试失败时使用的小数据包大小(MB)
pingCount: 10, // ping测试次数
downloadTimeout: 60, // 下载测试超时时间(秒)
uploadTimeout: 60, // 上传测试超时时间(秒)
pingTimeout: 5 // ping测试超时时间(秒)
}
};或者,您可以使用环境变量配置脚本:
# MySQL数据库连接
export DB_HOST=localhost
export DB_PORT=3306
export DB_NAME=speedtest_results
export DB_USER=root
export DB_PASSWORD=password
# 连接池配置
export DB_POOL_MIN=2
export DB_POOL_MAX=10
export DB_POOL_IDLE=30000
export DB_POOL_ACQUIRE=60000
# 服务器配置
export SPEEDTEST_SERVERS='[{"name":"测试服务器","url":"https://example.com/","username":"","password":""}]'脚本会自动在 MySQL 数据库中创建以下表:
CREATE TABLE IF NOT EXISTS speedtest_results (
id INT AUTO_INCREMENT PRIMARY KEY,
server_name VARCHAR(255) NOT NULL,
server_url VARCHAR(255) NOT NULL,
test_timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
download_speed FLOAT,
upload_speed FLOAT,
ping FLOAT,
jitter FLOAT,
server_info JSON
);脚本提供以下方法:
testAllServers(): 测试配置文件中的所有服务器testServer(serverConfig): 测试指定的服务器saveResult(result): 将测试结果保存到数据库initDatabase(): 初始化数据库连接和表结构getHistoryResults(limit): 获取最近的测试结果closeDatabaseConnection(): 关闭数据库连接
const { testAllServers } = require('./index.js');
// 测试所有服务器并将结果保存到数据库
testAllServers()
.then(results => {
console.log('所有测试完成:', results);
})
.catch(error => {
console.error('测试失败:', error);
});const { testServer, saveResult, initDatabase, getHistoryResults, closeDatabaseConnection } = require('./index.js');
// 初始化数据库
await initDatabase();
// 测试特定服务器
const serverConfig = {
name: "自定义服务器",
url: "https://example.com/speedtest/",
username: "user",
password: "pass"
};
const result = await testServer(serverConfig);
await saveResult(result);
// 获取历史结果
const history = await getHistoryResults(10);
console.log('最近的测试结果:', history);
// 完成后关闭数据库连接
await closeDatabaseConnection();- 将所有项目文件上传到青龙面板的脚本目录
- 在青龙面板的脚本管理中安装依赖:
npm install axios mysql2 - 在青龙面板的环境变量管理中配置环境变量
- 创建定时任务,命令为:
node index.js
DB_HOST: 数据库主机地址DB_PORT: 数据库端口DB_NAME: 数据库名称DB_USER: 数据库用户名DB_PASSWORD: 数据库密码SPEEDTEST_SERVERS: JSON 格式的服务器配置数组
axios: 用于 API 请求和基本认证的 HTTP 客户端mysql2: MySQL 数据库客户端
MIT
项目包含全面的测试套件,以确保功能的可靠性和稳定性:
要运行所有测试,请执行以下命令:
# 安装依赖(如果尚未安装)
npm install
# 运行数据库连接测试
node test/test-db-connection.js
# 运行服务器连接测试
node test/test-server-connection.js
# 运行日志功能测试
node test/test-logger.js
# 运行历史记录功能测试
node test/test-history.js
# 运行结果验证测试
node test/verify-results.js- test/test-db-connection.js: 测试数据库连接,验证数据库创建和检查表结构。
- test/test-server-connection.js: 测试与LibreSpeed服务器的连接,验证API端点和基本认证。
- test/test-logger.js: 测试日志记录功能,包括信息、错误和警告消息。
- test/test-history.js: 测试历史记录检索功能,包括结果的筛选和排序。
- test/test-utils.js: 提供数据库测试的实用工具函数,包括设置和清理。
- test/verify-results.js: 验证测试结果的准确性和完整性。
运行测试前,请确保:
- MySQL服务器正在运行且可访问
- 数据库凭据在
config.example.js(复制为config.js)中正确配置 - 所有依赖已安装(
npm install)
- Fork 仓库
- 创建功能分支
- 进行更改
- 提交拉取请求