web-log-generator-simple.sh 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/bin/bash
  2. # Web API 日志生成脚本 - 简易版
  3. # 通过shop-recycle-web前端快速生成日志
  4. WEB_URL="${1:-http://192.168.1.203}"
  5. API_URL="$WEB_URL/api"
  6. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  7. echo " Web API 日志生成 (Simple Mode)"
  8. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  9. echo "URL: $WEB_URL"
  10. echo ""
  11. STATS_FILE="/tmp/web-log-gen-$$.txt"
  12. echo "0 0 0" > "$STATS_FILE"
  13. cleanup() {
  14. echo ""
  15. read create payment failed < "$STATS_FILE"
  16. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  17. echo "已停止 | 创建=$create 支付=$payment 失败=$failed"
  18. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  19. rm -f "$STATS_FILE"
  20. exit 0
  21. }
  22. trap cleanup SIGINT
  23. USERS=("alice" "bob" "charlie" "david" "emma" "frank" "grace" "henry")
  24. DESCRIPTIONS=("回收电子产品" "二手家电处理" "旧家具回收" "废金属回收" "包装材料" "废纸回收" "塑料回收" "玻璃回收")
  25. METHODS=("CARD" "WECHAT" "ALIPAY")
  26. # 创建订单
  27. (
  28. count=0
  29. while true; do
  30. user="${USERS[$((RANDOM % ${#USERS[@]}))]}-$((RANDOM % 10000))"
  31. price="$((RANDOM % 500 + 10)).$(printf "%02d" $((RANDOM % 100)))"
  32. desc="${DESCRIPTIONS[$((RANDOM % ${#DESCRIPTIONS[@]}))]}"
  33. resp=$(curl -s -X POST "$API_URL/order/create" \
  34. -H "Content-Type: application/json" \
  35. -d "{\"userId\":\"$user\",\"price\":$price,\"description\":\"$desc\"}" 2>/dev/null)
  36. if echo "$resp" | grep -q '"code":0'; then
  37. order_id=$(echo "$resp" | grep -o '"orderId":"[^"]*' | cut -d':' -f2 | tr -d '"')
  38. if [ ! -z "$order_id" ]; then
  39. ((count++))
  40. echo "$count" > /tmp/web-log-gen-create-$$.txt
  41. echo "[$(date '+%H:%M:%S')] CREATE #$count"
  42. sleep 0.5
  43. continue
  44. fi
  45. fi
  46. failed=$(<"$STATS_FILE" cut -d' ' -f3)
  47. echo "0 0 $((failed+1))" > "$STATS_FILE"
  48. sleep 0.5
  49. done
  50. ) &
  51. # 支付订单
  52. (
  53. count=0
  54. while true; do
  55. sleep 1.2
  56. order_id=$(<"/tmp/web-log-gen-create-$$.txt" head -1 2>/dev/null)
  57. if [ ! -z "$order_id" ] && [ "$order_id" != "0" ]; then
  58. price="$((RANDOM % 500 + 10)).$(printf "%02d" $((RANDOM % 100)))"
  59. method="${METHODS[$((RANDOM % ${#METHODS[@]}))]}"
  60. resp=$(curl -s -X POST "$API_URL/payment/pay" \
  61. -H "Content-Type: application/json" \
  62. -d "{\"orderId\":\"$order_id\",\"amount\":$price,\"paymentMethod\":\"$method\"}" 2>/dev/null)
  63. if echo "$resp" | grep -q '"code":0'; then
  64. ((count++))
  65. echo "$count"
  66. fi
  67. fi
  68. sleep 0.8
  69. done
  70. ) > /tmp/web-log-gen-payment-$$.txt 2>&1 &
  71. # 监控
  72. echo "运行中 (Ctrl+C 停止)..."
  73. echo ""
  74. while true; do
  75. create=$(cat /tmp/web-log-gen-create-$$.txt 2>/dev/null || echo "0")
  76. payment=$(cat /tmp/web-log-gen-payment-$$.txt 2>/dev/null | tail -1 || echo "0")
  77. failed=$(<"$STATS_FILE" cut -d' ' -f3)
  78. total=$((create + payment))
  79. echo -ne "\r创建: $create | 支付: $payment | 失败: $failed | 总计: $total "
  80. sleep 2
  81. done