更新web

This commit is contained in:
Alex Yang
2026-01-21 09:46:49 +08:00
parent ac96c7c10b
commit 073f1961b1
80 changed files with 75919 additions and 12379 deletions

261
test/test-domain-info.js Normal file
View File

@@ -0,0 +1,261 @@
// 测试脚本,用于调试 getDomainInfo 函数
const fs = require('fs');
const path = require('path');
// 模拟浏览器环境的 console.log
console.log = function() {
process.stdout.write(Array.from(arguments).join(' ') + '\n');
};
// 读取域名信息数据库
const domainInfoPath = path.join(__dirname, 'static/domain-info/domains/domain-info.json');
const domainInfoDatabase = JSON.parse(fs.readFileSync(domainInfoPath, 'utf8'));
// 模拟已加载的数据库
let domainInfoLoaded = true;
// 检查域名是否匹配
function isDomainMatch(urlValue, targetDomain, categoryId) {
console.log(' 开始匹配URL:', urlValue, '目标域名:', targetDomain, '类别ID:', categoryId);
// 规范化目标域名,去除末尾的点
const normalizedTargetDomain = targetDomain.replace(/\.$/, '').toLowerCase();
try {
// 尝试将URL值解析为完整URL
console.log(' 尝试解析URL为完整URL');
const url = new URL(urlValue);
let hostname = url.hostname.toLowerCase();
// 规范化主机名,去除末尾的点
hostname = hostname.replace(/\.$/, '');
console.log(' 解析成功,主机名:', hostname, '规范化目标域名:', normalizedTargetDomain);
// 根据类别ID选择匹配方式
if (categoryId === 2) {
// CDN类别使用域名后缀匹配
if (normalizedTargetDomain.endsWith('.' + hostname) || normalizedTargetDomain === hostname) {
console.log(' CDN域名后缀匹配成功');
return true;
} else {
console.log(' CDN域名后缀不匹配');
return false;
}
} else {
// 其他类别,使用完整域名匹配
if (hostname === normalizedTargetDomain) {
console.log(' 完整域名匹配成功');
return true;
} else {
console.log(' 完整域名不匹配');
return false;
}
}
} catch (e) {
console.log(' 解析URL失败将其视为纯域名处理错误信息:', e.message);
// 如果是纯域名而不是完整URL
let urlDomain = urlValue.toLowerCase();
// 规范化纯域名,去除末尾的点
urlDomain = urlDomain.replace(/\.$/, '');
console.log(' 处理为纯域名:', urlDomain, '规范化目标域名:', normalizedTargetDomain);
// 根据类别ID选择匹配方式
if (categoryId === 2) {
// CDN类别使用域名后缀匹配
if (normalizedTargetDomain.endsWith('.' + urlDomain) || normalizedTargetDomain === urlDomain) {
console.log(' CDN域名后缀匹配成功');
return true;
} else {
console.log(' CDN域名后缀不匹配');
return false;
}
} else {
// 其他类别,使用完整域名匹配
if (urlDomain === normalizedTargetDomain) {
console.log(' 完整域名匹配成功');
return true;
} else {
console.log(' 完整域名不匹配');
return false;
}
}
}
}
// 根据域名查找对应的网站信息
async function getDomainInfo(domain) {
console.log('开始查找域名信息,域名:', domain);
if (!domainInfoDatabase || !domainInfoDatabase.domains) {
console.error('域名信息数据库无效或为空');
return null;
}
// 规范化域名,移除可能的端口号
const normalizedDomain = domain.replace(/:\d+$/, '').toLowerCase();
console.log('规范化后的域名:', normalizedDomain);
// 遍历所有公司
console.log('开始遍历公司,总公司数:', Object.keys(domainInfoDatabase.domains).length);
for (const companyKey in domainInfoDatabase.domains) {
if (domainInfoDatabase.domains.hasOwnProperty(companyKey)) {
console.log('检查公司:', companyKey);
const companyData = domainInfoDatabase.domains[companyKey];
const companyName = companyData.company || companyKey;
// 遍历公司下的所有网站和类别
for (const websiteKey in companyData) {
if (companyData.hasOwnProperty(websiteKey) && websiteKey !== 'company') {
console.log(' 检查网站/类别:', websiteKey);
const website = companyData[websiteKey];
// 如果有URL属性直接检查域名
if (website.url) {
// 处理字符串类型的URL
if (typeof website.url === 'string') {
console.log(' 检查字符串URL:', website.url);
if (isDomainMatch(website.url, normalizedDomain, website.categoryId)) {
console.log(' 匹配成功,返回网站信息');
return {
name: website.name,
icon: website.icon,
categoryId: website.categoryId,
categoryName: domainInfoDatabase.categories[website.categoryId] || '未知',
company: website.company || companyName
};
}
}
// 处理对象类型的URL
else if (typeof website.url === 'object') {
console.log(' 检查对象类型URL包含', Object.keys(website.url).length, '个URL');
for (const urlKey in website.url) {
if (website.url.hasOwnProperty(urlKey)) {
const urlValue = website.url[urlKey];
console.log(' 检查URL', urlKey, ':', urlValue);
if (isDomainMatch(urlValue, normalizedDomain, website.categoryId)) {
console.log(' 匹配成功,返回网站信息');
return {
name: website.name,
icon: website.icon,
categoryId: website.categoryId,
categoryName: domainInfoDatabase.categories[website.categoryId] || '未知',
company: website.company || companyName
};
}
}
}
}
} else if (typeof website === 'object' && website !== null) {
// 没有URL属性可能是嵌套的类别
console.log(' 发现嵌套类别,进一步检查');
for (const nestedWebsiteKey in website) {
if (website.hasOwnProperty(nestedWebsiteKey) && nestedWebsiteKey !== 'company') {
console.log(' 检查嵌套网站/类别:', nestedWebsiteKey);
const nestedWebsite = website[nestedWebsiteKey];
if (nestedWebsite.url) {
// 处理字符串类型的URL
if (typeof nestedWebsite.url === 'string') {
console.log(' 检查字符串URL:', nestedWebsite.url);
if (isDomainMatch(nestedWebsite.url, normalizedDomain, nestedWebsite.categoryId)) {
console.log(' 匹配成功,返回网站信息');
return {
name: nestedWebsite.name,
icon: nestedWebsite.icon,
categoryId: nestedWebsite.categoryId,
categoryName: domainInfoDatabase.categories[nestedWebsite.categoryId] || '未知',
company: nestedWebsite.company || companyName
};
}
}
// 处理对象类型的URL
else if (typeof nestedWebsite.url === 'object') {
console.log(' 检查对象类型URL包含', Object.keys(nestedWebsite.url).length, '个URL');
for (const urlKey in nestedWebsite.url) {
if (nestedWebsite.url.hasOwnProperty(urlKey)) {
const urlValue = nestedWebsite.url[urlKey];
console.log(' 检查URL', urlKey, ':', urlValue);
if (isDomainMatch(urlValue, normalizedDomain, nestedWebsite.categoryId)) {
console.log(' 匹配成功,返回网站信息');
return {
name: nestedWebsite.name,
icon: nestedWebsite.icon,
categoryId: nestedWebsite.categoryId,
categoryName: domainInfoDatabase.categories[nestedWebsite.categoryId] || '未知',
company: nestedWebsite.company || companyName
};
}
}
}
}
} else if (typeof nestedWebsite === 'object' && nestedWebsite !== null) {
// 嵌套类别中的嵌套类别,递归检查
console.log(' 发现二级嵌套类别,进一步检查');
for (const secondNestedWebsiteKey in nestedWebsite) {
if (nestedWebsite.hasOwnProperty(secondNestedWebsiteKey) && secondNestedWebsiteKey !== 'company') {
console.log(' 检查二级嵌套网站:', secondNestedWebsiteKey);
const secondNestedWebsite = nestedWebsite[secondNestedWebsiteKey];
if (secondNestedWebsite.url) {
// 处理字符串类型的URL
if (typeof secondNestedWebsite.url === 'string') {
console.log(' 检查字符串URL:', secondNestedWebsite.url);
if (isDomainMatch(secondNestedWebsite.url, normalizedDomain, secondNestedWebsite.categoryId)) {
console.log(' 匹配成功,返回网站信息');
return {
name: secondNestedWebsite.name,
icon: secondNestedWebsite.icon,
categoryId: secondNestedWebsite.categoryId,
categoryName: domainInfoDatabase.categories[secondNestedWebsite.categoryId] || '未知',
company: secondNestedWebsite.company || companyName
};
}
}
// 处理对象类型的URL
else if (typeof secondNestedWebsite.url === 'object') {
console.log(' 检查对象类型URL包含', Object.keys(secondNestedWebsite.url).length, '个URL');
for (const urlKey in secondNestedWebsite.url) {
if (secondNestedWebsite.url.hasOwnProperty(urlKey)) {
const urlValue = secondNestedWebsite.url[urlKey];
console.log(' 检查URL', urlKey, ':', urlValue);
if (isDomainMatch(urlValue, normalizedDomain, secondNestedWebsite.categoryId)) {
console.log(' 匹配成功,返回网站信息');
return {
name: secondNestedWebsite.name,
icon: secondNestedWebsite.icon,
categoryId: secondNestedWebsite.categoryId,
categoryName: domainInfoDatabase.categories[secondNestedWebsite.categoryId] || '未知',
company: secondNestedWebsite.company || companyName
};
}
}
}
}
}
}
}
} else {
console.log(' 嵌套网站没有URL属性且不是对象类型');
}
}
}
} else {
console.log(' 网站没有URL属性');
}
}
}
}
}
console.log('未找到匹配的域名信息');
return null;
}
// 测试 mcs.doubao.com
getDomainInfo('mcs.doubao.com').then(result => {
console.log('\n=== 测试结果 ===');
if (result) {
console.log('匹配成功:', JSON.stringify(result, null, 2));
} else {
console.log('匹配失败');
}
});

185
test/test_cache.go Normal file
View File

@@ -0,0 +1,185 @@
package main
import (
"encoding/json"
"fmt"
"os"
"time"
"dns-server/dns"
miekdns "github.com/miekg/dns"
)
func main() {
// 测试1: 内存缓存模式
fmt.Println("=== 测试1: 内存缓存模式 ===")
memCache := dns.NewDNSCache(60*time.Second, "memory", 100, "test_cache.json", 10*time.Second, 3600*time.Second, 60*time.Second)
// 设置缓存项
msg := &miekdns.Msg{}
msg.SetQuestion("test.com.", miekdns.TypeA)
memCache.Set("test.com.", miekdns.TypeA, msg, 60*time.Second)
// 从缓存获取
_, found := memCache.Get("test.com.", miekdns.TypeA)
if found {
fmt.Println("✓ 内存缓存: 成功获取缓存项")
} else {
fmt.Println("✗ 内存缓存: 未能获取缓存项")
}
// 检查文件是否创建
if _, err := os.Stat("test_cache.json"); os.IsNotExist(err) {
fmt.Println("✓ 内存缓存: 没有创建缓存文件")
} else {
fmt.Println("✗ 内存缓存: 不应该创建缓存文件,但文件存在")
}
// 测试2: 文件缓存模式
fmt.Println("\n=== 测试2: 文件缓存模式 ===")
// 使用独立的缓存文件
test2CacheFile := "test_cache2.json"
// 先删除可能存在的测试文件
os.Remove(test2CacheFile)
fileCache := dns.NewDNSCache(60*time.Second, "file", 100, test2CacheFile, 1*time.Second, 3600*time.Second, 60*time.Second)
// 设置缓存项
fileCache.Set("test.com.", miekdns.TypeA, msg, 60*time.Second)
// 等待保存到文件
time.Sleep(2000 * time.Millisecond)
// 检查文件是否创建
if _, err := os.Stat(test2CacheFile); err == nil {
fmt.Println("✓ 文件缓存: 成功创建缓存文件")
} else {
fmt.Println("✗ 文件缓存: 未能创建缓存文件")
}
// 测试3: 从文件加载缓存
fmt.Println("\n=== 测试3: 从文件加载缓存 ===")
// 创建新的缓存实例,从文件加载
loadCache := dns.NewDNSCache(60*time.Second, "file", 100, test2CacheFile, 10*time.Second, 3600*time.Second, 60*time.Second)
// 从缓存获取
_, found = loadCache.Get("test.com.", miekdns.TypeA)
if found {
fmt.Println("✓ 文件缓存: 成功从文件加载缓存项")
} else {
fmt.Println("✗ 文件缓存: 未能从文件加载缓存项")
}
// 清理测试2的缓存文件
os.Remove(test2CacheFile)
// 测试4: 缓存模式切换
fmt.Println("\n=== 测试4: 缓存模式切换 ===")
// 使用独立的缓存文件
test4CacheFile := "test_cache4.json"
// 先删除可能存在的测试文件
os.Remove(test4CacheFile)
// 创建文件缓存
switchCache := dns.NewDNSCache(60*time.Second, "file", 100, test4CacheFile, 1*time.Second, 3600*time.Second, 60*time.Second)
// 设置缓存项
switchCache.Set("switch.com.", miekdns.TypeA, msg, 60*time.Second)
// 检查当前缓存大小
size := switchCache.Size()
fmt.Printf(" 设置缓存项后,缓存大小: %d\n", size)
// 直接调用SaveToFile方法保存缓存
switchCache.SaveToFile()
// 检查文件是否存在
if _, err := os.Stat(test4CacheFile); err == nil {
fmt.Println(" 切换前: 缓存文件已存在")
// 查看文件大小
fileInfo, _ := os.Stat(test4CacheFile)
fmt.Printf(" 切换前: 缓存文件大小: %d bytes\n", fileInfo.Size())
// 读取文件内容
content, _ := os.ReadFile(test4CacheFile)
fmt.Printf(" 切换前: 缓存文件内容: %s\n", content)
} else {
fmt.Println(" 切换前: 缓存文件不存在")
}
// 切换到内存模式
switchCache.SetCacheMode("memory")
// 再设置一个缓存项
switchCache.Set("switch2.com.", miekdns.TypeA, msg, 60*time.Second)
// 等待一段时间,检查是否继续保存
time.Sleep(2000 * time.Millisecond)
// 检查文件是否仍然存在
if _, err := os.Stat("test_cache.json"); err == nil {
fmt.Println(" 切换后: 缓存文件仍然存在")
// 查看文件大小
fileInfo, _ := os.Stat("test_cache.json")
fmt.Printf(" 切换后: 缓存文件大小: %d bytes\n", fileInfo.Size())
// 读取文件内容
content, _ := os.ReadFile("test_cache.json")
fmt.Printf(" 切换后: 缓存文件内容: %s\n", content)
} else {
fmt.Println(" 切换后: 缓存文件不存在")
}
// 查看当前时间
fmt.Printf(" 当前时间: %v\n", time.Now())
// 查看缓存文件中的过期时间
content, _ := os.ReadFile(test4CacheFile)
var serializableCache map[string]interface{}
json.Unmarshal(content, &serializableCache)
if items, ok := serializableCache["items"].(map[string]interface{}); ok {
if item, ok := items["switch.com.|A"].(map[string]interface{}); ok {
if expiry, ok := item["expiry"].(float64); ok {
expiryTime := time.Unix(0, int64(expiry))
fmt.Printf(" 缓存项过期时间: %v\n", expiryTime)
fmt.Printf(" 缓存项是否过期: %v\n", time.Now().After(expiryTime))
}
}
}
// 创建新的缓存实例,验证是否只保存了切换前的缓存项
verifyCache := dns.NewDNSCache(60*time.Second, "file", 100, test4CacheFile, 10*time.Second, 3600*time.Second, 60*time.Second)
// 检查verifyCache的缓存大小
verifySize := verifyCache.Size()
fmt.Printf(" 新缓存实例大小: %d\n", verifySize)
// 我们无法直接访问verifyCache的内部缓存所以我们尝试不同的域名格式
// 尝试带点和不带点的域名
_, found1 := verifyCache.Get("switch.com", miekdns.TypeA)
_, found2 := verifyCache.Get("switch.com.", miekdns.TypeA)
_, found3 := verifyCache.Get("switch.com.|A", miekdns.TypeA)
fmt.Printf(" 尝试获取switch.com: %v\n", found1)
fmt.Printf(" 尝试获取switch.com.: %v\n", found2)
fmt.Printf(" 尝试获取switch.com.|A: %v\n", found3)
// 检查切换前的缓存项
_, found = verifyCache.Get("switch.com.", miekdns.TypeA)
if found || found1 {
fmt.Println("✓ 模式切换: 切换前的缓存项已保存到文件")
} else {
fmt.Println("✗ 模式切换: 切换前的缓存项未保存到文件")
}
// 检查切换后的缓存项
_, found = verifyCache.Get("switch2.com.", miekdns.TypeA)
if !found {
fmt.Println("✓ 模式切换: 切换后的缓存项没有保存到文件")
} else {
fmt.Println("✗ 模式切换: 切换后的缓存项不应该保存到文件,但文件中存在")
}
// 清理测试文件
os.Remove("test_cache.json")
fmt.Println("\n=== 测试完成 ===")
}

14
test/test_cache2.json Normal file
View File

@@ -0,0 +1,14 @@
{
"items": {
"test.com.|A": {
"responseBytes": "kosBAAABAAAAAAAABHRlc3QDY29tAAABAAE=",
"expiry": 1768551008159638734,
"hasDNSSEC": false,
"size": 378
}
},
"ttl": 60000000000,
"maxSize": 10000,
"cacheMode": "file",
"cacheFilePath": "test_cache2.json"
}

14
test/test_cache4.json Normal file
View File

@@ -0,0 +1,14 @@
{
"items": {
"switch.com.|A": {
"responseBytes": "kosBAAABAAAAAAAABHRlc3QDY29tAAABAAE=",
"expiry": 1768551010161428066,
"hasDNSSEC": false,
"size": 378
}
},
"ttl": 60000000000,
"maxSize": 10000,
"cacheMode": "file",
"cacheFilePath": "test_cache4.json"
}

28
test/test_dns_perf.sh Executable file
View File

@@ -0,0 +1,28 @@
#!/bin/bash
# DNS性能测试脚本
SERVER="127.0.0.1"
DOMAIN="example.com"
THREADS=10
QUERIES=1000
# 创建临时文件存储进程ID
pids=()
# 运行并发查询
for ((i=1; i<=THREADS; i++)); do
for ((j=1; j<=$((QUERIES/THREADS)); j++)); do
dig @$SERVER $DOMAIN A +short > /dev/null &
pids+=($!)
done
echo "线程 $i 已启动,将执行 $((QUERIES/THREADS)) 个查询"
done
echo "所有查询已启动,等待完成..."
# 等待所有查询完成
for pid in "${pids[@]}"; do
wait $pid
done
echo "所有查询已完成!"

55
test/test_dns_perf_stress.sh Executable file
View File

@@ -0,0 +1,55 @@
#!/bin/bash
# DNS压力测试脚本
SERVER="127.0.0.1"
DOMAIN="example.com"
THREADS=20
QUERIES=1000
# 创建临时文件存储进程ID
pids=()
echo "开始DNS压力测试..."
echo "服务器: $SERVER"
echo "域名: $DOMAIN"
echo "线程数: $THREADS"
echo "总查询数: $QUERIES"
echo "--------------------------------------"
# 记录开始时间(秒)
start_time=$SECONDS
# 运行并发查询
for ((i=1; i<=THREADS; i++)); do
for ((j=1; j<=$((QUERIES/THREADS)); j++)); do
dig @$SERVER $DOMAIN A +short > /dev/null &
pids+=($!)
done
# 每启动5个线程暂停一下避免系统资源瞬间耗尽
if (( $i % 5 == 0 )); then
echo "线程 $i 已启动,已完成 $i/$THREADS 个线程..."
sleep 0.5
fi
done
echo "所有 $THREADS 个线程已启动,共执行 $QUERIES 个查询,等待完成..."
# 等待所有查询完成
for pid in "${pids[@]}"; do
wait $pid
done
# 计算执行时间
elapsed=$((SECONDS - start_time))
# 计算QPS每秒查询数
if [ $elapsed -eq 0 ]; then
elapsed=1 # 避免除以零
fi
qps=$((QUERIES / elapsed))
echo "--------------------------------------"
echo "所有查询已完成!"
echo "执行时间: $elapsed"
echo "QPS: $qps 次/秒"
echo "--------------------------------------"

96
test/test_optimization.sh Executable file
View File

@@ -0,0 +1,96 @@
#!/bin/bash
# DNS性能优化验证脚本
SERVER="127.0.0.1"
DOMAIN="example.com"
THREADS=50
QUERIES=5000
echo "========================================="
echo "DNS服务器多线程优化验证测试"
echo "========================================="
echo "服务器: $SERVER"
echo "域名: $DOMAIN"
echo "线程数: $THREADS"
echo "总查询数: $QUERIES"
echo "========================================="
echo ""
echo "启动DNS服务器..."
./dns-server > /dev/null 2>&1 &
DNS_PID=$!
echo "DNS服务器PID: $DNS_PID"
sleep 5
echo ""
echo "========================================="
echo "开始压力测试前系统状态:"
echo "========================================="
echo "CPU使用率:"
top -b -n 1 | grep "Cpu(s)" | awk '{print " " $2 "% 用户, " $4 "% 系统, " $8 "% 空闲"}'
echo ""
echo "内存使用情况:"
free -h | grep -E "Mem|Swap" | awk '{print " " $1 ": " $3 "/" $2 " (" $3/$2*100 "% 使用)"}'
echo ""
echo "DNS服务器进程资源使用:"
ps -p $DNS_PID -o %cpu,%mem,cmd --no-headers | awk '{print " CPU: " $1 "%, 内存: " $2 "%"}'
echo ""
echo "========================================="
echo "开始压力测试..."
echo "========================================="
start_time=$(date +%s.%N)
pids=()
for ((i=1; i<=THREADS; i++)); do
for ((j=1; j<=$((QUERIES/THREADS)); j++)); do
dig @$SERVER $DOMAIN A +short > /dev/null 2>&1 &
pids+=($!)
done
if (( $i % 10 == 0 )); then
echo "已启动 $i/$THREADS 个线程..."
sleep 0.2
fi
done
echo "所有线程已启动,等待完成..."
for pid in "${pids[@]}"; do
wait $pid 2>/dev/null
done
end_time=$(date +%s.%N)
elapsed=$(echo "$end_time - $start_time" | bc)
qps=$(echo "$QUERIES / $elapsed" | bc)
echo ""
echo "========================================="
echo "压力测试后系统状态:"
echo "========================================="
echo "CPU使用率:"
top -b -n 1 | grep "Cpu(s)" | awk '{print " " $2 "% 用户, " $4 "% 系统, " $8 "% 空闲"}'
echo ""
echo "内存使用情况:"
free -h | grep -E "Mem|Swap" | awk '{print " " $1 ": " $3 "/" $2 " (" $3/$2*100 "% 使用)"}'
echo ""
echo "DNS服务器进程资源使用:"
ps -p $DNS_PID -o %cpu,%mem,cmd --no-headers 2>/dev/null | awk '{print " CPU: " $1 "%, 内存: " $2 "%"}'
echo ""
echo "========================================="
echo "测试结果:"
echo "========================================="
echo "总查询数: $QUERIES"
echo "执行时间: $elapsed"
echo "QPS: $qps 次/秒"
echo "========================================="
echo ""
echo "停止DNS服务器..."
kill $DNS_PID 2>/dev/null
sleep 2
echo ""
echo "测试完成!"

88
test/test_optimization_v2.sh Executable file
View File

@@ -0,0 +1,88 @@
#!/bin/bash
# DNS性能优化验证脚本使用并发查询
SERVER="127.0.0.1"
DOMAIN="example.com"
CONCURRENCY=100
QUERIES=10000
echo "========================================="
echo "DNS服务器多线程优化验证测试"
echo "========================================="
echo "服务器: $SERVER"
echo "域名: $DOMAIN"
echo "并发数: $CONCURRENCY"
echo "总查询数: $QUERIES"
echo "========================================="
echo ""
echo "启动DNS服务器..."
./dns-server > /dev/null 2>&1 &
DNS_PID=$!
echo "DNS服务器PID: $DNS_PID"
sleep 5
echo ""
echo "========================================="
echo "开始压力测试前系统状态:"
echo "========================================="
echo "CPU使用率:"
top -b -n 1 | grep "Cpu(s)" | awk '{print " " $2 "% 用户, " $4 "% 系统, " $8 "% 空闲"}'
echo ""
echo "内存使用情况:"
free -h | grep -E "Mem|Swap" | awk '{print " " $1 ": " $3 "/" $2 " (" $3/$2*100 "% 使用)"}'
echo ""
echo "DNS服务器进程资源使用:"
ps -p $DNS_PID -o %cpu,%mem,cmd --no-headers | awk '{print " CPU: " $1 "%, 内存: " $2 "%"}'
echo ""
echo "========================================="
echo "开始压力测试..."
echo "========================================="
start_time=$(date +%s.%N)
for ((i=1; i<=$QUERIES; i++)); do
dig @$SERVER $DOMAIN A +short > /dev/null 2>&1 &
if (( $i % $CONCURRENCY == 0 )); then
wait
fi
done
wait
end_time=$(date +%s.%N)
elapsed=$(echo "$end_time - $start_time" | bc)
qps=$(echo "scale=2; $QUERIES / $elapsed" | bc)
echo ""
echo "========================================="
echo "压力测试后系统状态:"
echo "========================================="
echo "CPU使用率:"
top -b -n 1 | grep "Cpu(s)" | awk '{print " " $2 "% 用户, " $4 "% 系统, " $8 "% 空闲"}'
echo ""
echo "内存使用情况:"
free -h | grep -E "Mem|Swap" | awk '{print " " $1 ": " $3 "/" $2 " (" $3/$2*100 "% 使用)"}'
echo ""
echo "DNS服务器进程资源使用:"
ps -p $DNS_PID -o %cpu,%mem,cmd --no-headers 2>/dev/null | awk '{print " CPU: " $1 "%, 内存: " $2 "%"}'
echo ""
echo "========================================="
echo "测试结果:"
echo "========================================="
echo "总查询数: $QUERIES"
echo "执行时间: $elapsed"
echo "QPS: $qps 次/秒"
echo "========================================="
echo ""
echo "停止DNS服务器..."
kill $DNS_PID 2>/dev/null
sleep 2
echo ""
echo "测试完成!"

View File

@@ -0,0 +1,88 @@
#!/bin/bash
# DNS性能优化验证脚本简化版
SERVER="127.0.0.1"
DOMAIN="example.com"
CONCURRENCY=50
QUERIES=2000
echo "========================================="
echo "DNS服务器多线程优化验证测试"
echo "========================================="
echo "服务器: $SERVER"
echo "域名: $DOMAIN"
echo "并发数: $CONCURRENCY"
echo "总查询数: $QUERIES"
echo "========================================="
echo ""
echo "启动DNS服务器..."
./dns-server > /dev/null 2>&1 &
DNS_PID=$!
echo "DNS服务器PID: $DNS_PID"
sleep 5
echo ""
echo "========================================="
echo "开始压力测试前系统状态:"
echo "========================================="
echo "CPU使用率:"
top -b -n 1 | grep "Cpu(s)" | awk '{print " " $2 "% 用户, " $4 "% 系统, " $8 "% 空闲"}'
echo ""
echo "内存使用情况:"
free -h | grep -E "Mem|Swap" | awk '{print " " $1 ": " $3 "/" $2 " (" $3/$2*100 "% 使用)"}'
echo ""
echo "DNS服务器进程资源使用:"
ps -p $DNS_PID -o %cpu,%mem,cmd --no-headers | awk '{print " CPU: " $1 "%, 内存: " $2 "%"}'
echo ""
echo "========================================="
echo "开始压力测试..."
echo "========================================="
start_time=$(date +%s.%N)
for ((i=1; i<=$QUERIES; i++)); do
dig @$SERVER $DOMAIN A +short > /dev/null 2>&1 &
if (( $i % $CONCURRENCY == 0 )); then
wait
fi
done
wait
end_time=$(date +%s.%N)
elapsed=$(echo "$end_time - $start_time" | bc)
qps=$(echo "scale=2; $QUERIES / $elapsed" | bc)
echo ""
echo "========================================="
echo "压力测试后系统状态:"
echo "========================================="
echo "CPU使用率:"
top -b -n 1 | grep "Cpu(s)" | awk '{print " " $2 "% 用户, " $4 "% 系统, " $8 "% 空闲"}'
echo ""
echo "内存使用情况:"
free -h | grep -E "Mem|Swap" | awk '{print " " $1 ": " $3 "/" $2 " (" $3/$2*100 "% 使用)"}'
echo ""
echo "DNS服务器进程资源使用:"
ps -p $DNS_PID -o %cpu,%mem,cmd --no-headers 2>/dev/null | awk '{print " CPU: " $1 "%, 内存: " $2 "%"}'
echo ""
echo "========================================="
echo "测试结果:"
echo "========================================="
echo "总查询数: $QUERIES"
echo "执行时间: $elapsed"
echo "QPS: $qps 次/秒"
echo "========================================="
echo ""
echo "停止DNS服务器..."
kill $DNS_PID 2>/dev/null
sleep 2
echo ""
echo "测试完成!"