test-redis-only.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const Redis = require('ioredis');
  2. require('dotenv').config();
  3. // 测试结果收集器
  4. const testResults = {
  5. redis: {
  6. status: 'pending',
  7. responseTime: null,
  8. error: null,
  9. details: {}
  10. }
  11. };
  12. // 颜色输出函数
  13. const colors = {
  14. green: '\x1b[32m',
  15. red: '\x1b[31m',
  16. yellow: '\x1b[33m',
  17. blue: '\x1b[34m',
  18. reset: '\x1b[0m'
  19. };
  20. function log(message, color = colors.reset) {
  21. console.log(`${color}${message}${colors.reset}`);
  22. }
  23. // 测试Redis连接
  24. async function testRedisConnection() {
  25. log('\n===== 测试Redis连接 =====', colors.blue);
  26. const startTime = Date.now();
  27. try {
  28. // 从环境变量获取Redis连接配置
  29. const redisHost = process.env.REDIS_HOST || 'localhost';
  30. const redisPort = process.env.REDIS_PORT || 6379;
  31. const redisUrl = process.env.REDIS_URL || `redis://${redisHost}:${redisPort}`;
  32. log(`连接地址: ${redisUrl}`, colors.yellow);
  33. const redis = new Redis(redisUrl);
  34. // 测试连接
  35. const pong = await redis.ping();
  36. const responseTime = Date.now() - startTime;
  37. // 测试基本操作
  38. const testKey = 'test_connection_' + Date.now();
  39. await redis.set(testKey, 'test_value', 'EX', 10);
  40. const getValue = await redis.get(testKey);
  41. await redis.del(testKey);
  42. // 获取Redis信息
  43. const info = await redis.info('server');
  44. testResults.redis = {
  45. status: 'success',
  46. responseTime,
  47. error: null,
  48. details: {
  49. pong,
  50. testResult: getValue === 'test_value',
  51. redisVersion: info.match(/redis_version:(.+)/)?.[1] || 'unknown'
  52. }
  53. };
  54. log(`✅ Redis连接成功!`, colors.green);
  55. log(` 响应时间: ${responseTime}ms`, colors.green);
  56. log(` PING响应: ${pong}`, colors.green);
  57. log(` 测试读写: ${getValue === 'test_value' ? '成功' : '失败'}`, colors.green);
  58. log(` Redis版本: ${info.match(/redis_version:(.+)/)?.[1] || 'unknown'}`, colors.green);
  59. await redis.quit();
  60. } catch (error) {
  61. const responseTime = Date.now() - startTime;
  62. testResults.redis = {
  63. status: 'failed',
  64. responseTime,
  65. error: error.message,
  66. details: {
  67. errorCode: error.code,
  68. errorName: error.name
  69. }
  70. };
  71. log(`❌ Redis连接失败!`, colors.red);
  72. log(` 错误: ${error.message}`, colors.red);
  73. log(` 响应时间: ${responseTime}ms`, colors.red);
  74. }
  75. }
  76. // 生成测试报告
  77. function generateReport() {
  78. log('\n\n===== Redis连接测试报告 =====', colors.blue);
  79. log(`测试时间: ${new Date().toISOString()}`, colors.yellow);
  80. log('========================', colors.blue);
  81. // Redis报告
  82. log('\n【Redis】', colors.blue);
  83. if (testResults.redis.status === 'success') {
  84. log(`状态: ✅ 成功`, colors.green);
  85. log(`响应时间: ${testResults.redis.responseTime}ms`, colors.green);
  86. log(`PING响应: ${testResults.redis.details.pong}`, colors.green);
  87. log(`测试读写: ${testResults.redis.details.testResult ? '成功' : '失败'}`, colors.green);
  88. log(`版本: ${testResults.redis.details.redisVersion}`, colors.green);
  89. } else {
  90. log(`状态: ❌ 失败`, colors.red);
  91. log(`错误: ${testResults.redis.error}`, colors.red);
  92. }
  93. log('========================', colors.blue);
  94. // 保存报告到文件
  95. const reportData = {
  96. timestamp: new Date().toISOString(),
  97. results: testResults,
  98. overallStatus: testResults.redis.status === 'success' ? 'success' : 'failed'
  99. };
  100. const fs = require('fs');
  101. fs.writeFileSync(
  102. './redis-test-report.json',
  103. JSON.stringify(reportData, null, 2)
  104. );
  105. log('\n详细报告已保存到: redis-test-report.json', colors.yellow);
  106. return testResults.redis.status === 'success';
  107. }
  108. // 主函数
  109. async function runRedisTest() {
  110. log('开始执行Redis连接测试...', colors.blue);
  111. await testRedisConnection();
  112. const success = generateReport();
  113. process.exit(success ? 0 : 1);
  114. }
  115. // 运行测试
  116. runRedisTest().catch(error => {
  117. log(`\n测试执行过程中发生错误: ${error.message}`, colors.red);
  118. console.error(error);
  119. process.exit(1);
  120. });