| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- const Redis = require('ioredis');
- require('dotenv').config();
- // 测试结果收集器
- const testResults = {
- redis: {
- status: 'pending',
- responseTime: null,
- error: null,
- details: {}
- }
- };
- // 颜色输出函数
- const colors = {
- green: '\x1b[32m',
- red: '\x1b[31m',
- yellow: '\x1b[33m',
- blue: '\x1b[34m',
- reset: '\x1b[0m'
- };
- function log(message, color = colors.reset) {
- console.log(`${color}${message}${colors.reset}`);
- }
- // 测试Redis连接
- async function testRedisConnection() {
- log('\n===== 测试Redis连接 =====', colors.blue);
- const startTime = Date.now();
-
- try {
- // 从环境变量获取Redis连接配置
- const redisHost = process.env.REDIS_HOST || 'localhost';
- const redisPort = process.env.REDIS_PORT || 6379;
- const redisUrl = process.env.REDIS_URL || `redis://${redisHost}:${redisPort}`;
-
- log(`连接地址: ${redisUrl}`, colors.yellow);
-
- const redis = new Redis(redisUrl);
-
- // 测试连接
- const pong = await redis.ping();
- const responseTime = Date.now() - startTime;
-
- // 测试基本操作
- const testKey = 'test_connection_' + Date.now();
- await redis.set(testKey, 'test_value', 'EX', 10);
- const getValue = await redis.get(testKey);
- await redis.del(testKey);
-
- // 获取Redis信息
- const info = await redis.info('server');
-
- testResults.redis = {
- status: 'success',
- responseTime,
- error: null,
- details: {
- pong,
- testResult: getValue === 'test_value',
- redisVersion: info.match(/redis_version:(.+)/)?.[1] || 'unknown'
- }
- };
-
- log(`✅ Redis连接成功!`, colors.green);
- log(` 响应时间: ${responseTime}ms`, colors.green);
- log(` PING响应: ${pong}`, colors.green);
- log(` 测试读写: ${getValue === 'test_value' ? '成功' : '失败'}`, colors.green);
- log(` Redis版本: ${info.match(/redis_version:(.+)/)?.[1] || 'unknown'}`, colors.green);
-
- await redis.quit();
- } catch (error) {
- const responseTime = Date.now() - startTime;
- testResults.redis = {
- status: 'failed',
- responseTime,
- error: error.message,
- details: {
- errorCode: error.code,
- errorName: error.name
- }
- };
-
- log(`❌ Redis连接失败!`, colors.red);
- log(` 错误: ${error.message}`, colors.red);
- log(` 响应时间: ${responseTime}ms`, colors.red);
- }
- }
- // 生成测试报告
- function generateReport() {
- log('\n\n===== Redis连接测试报告 =====', colors.blue);
- log(`测试时间: ${new Date().toISOString()}`, colors.yellow);
- log('========================', colors.blue);
-
- // Redis报告
- log('\n【Redis】', colors.blue);
- if (testResults.redis.status === 'success') {
- log(`状态: ✅ 成功`, colors.green);
- log(`响应时间: ${testResults.redis.responseTime}ms`, colors.green);
- log(`PING响应: ${testResults.redis.details.pong}`, colors.green);
- log(`测试读写: ${testResults.redis.details.testResult ? '成功' : '失败'}`, colors.green);
- log(`版本: ${testResults.redis.details.redisVersion}`, colors.green);
- } else {
- log(`状态: ❌ 失败`, colors.red);
- log(`错误: ${testResults.redis.error}`, colors.red);
- }
-
- log('========================', colors.blue);
-
- // 保存报告到文件
- const reportData = {
- timestamp: new Date().toISOString(),
- results: testResults,
- overallStatus: testResults.redis.status === 'success' ? 'success' : 'failed'
- };
-
- const fs = require('fs');
- fs.writeFileSync(
- './redis-test-report.json',
- JSON.stringify(reportData, null, 2)
- );
-
- log('\n详细报告已保存到: redis-test-report.json', colors.yellow);
-
- return testResults.redis.status === 'success';
- }
- // 主函数
- async function runRedisTest() {
- log('开始执行Redis连接测试...', colors.blue);
-
- await testRedisConnection();
-
- const success = generateReport();
-
- process.exit(success ? 0 : 1);
- }
- // 运行测试
- runRedisTest().catch(error => {
- log(`\n测试执行过程中发生错误: ${error.message}`, colors.red);
- console.error(error);
- process.exit(1);
- });
|