| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #!/bin/bash
- # Web API 日志生成脚本 - 简易版
- # 通过shop-recycle-web前端快速生成日志
- WEB_URL="${1:-http://192.168.1.203}"
- API_URL="$WEB_URL/api"
- echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
- echo " Web API 日志生成 (Simple Mode)"
- echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
- echo "URL: $WEB_URL"
- echo ""
- STATS_FILE="/tmp/web-log-gen-$$.txt"
- echo "0 0 0" > "$STATS_FILE"
- cleanup() {
- echo ""
- read create payment failed < "$STATS_FILE"
- echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
- echo "已停止 | 创建=$create 支付=$payment 失败=$failed"
- echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
- rm -f "$STATS_FILE"
- exit 0
- }
- trap cleanup SIGINT
- USERS=("alice" "bob" "charlie" "david" "emma" "frank" "grace" "henry")
- DESCRIPTIONS=("回收电子产品" "二手家电处理" "旧家具回收" "废金属回收" "包装材料" "废纸回收" "塑料回收" "玻璃回收")
- METHODS=("CARD" "WECHAT" "ALIPAY")
- # 创建订单
- (
- count=0
- while true; do
- user="${USERS[$((RANDOM % ${#USERS[@]}))]}-$((RANDOM % 10000))"
- price="$((RANDOM % 500 + 10)).$(printf "%02d" $((RANDOM % 100)))"
- desc="${DESCRIPTIONS[$((RANDOM % ${#DESCRIPTIONS[@]}))]}"
-
- resp=$(curl -s -X POST "$API_URL/order/create" \
- -H "Content-Type: application/json" \
- -d "{\"userId\":\"$user\",\"price\":$price,\"description\":\"$desc\"}" 2>/dev/null)
-
- if echo "$resp" | grep -q '"code":0'; then
- order_id=$(echo "$resp" | grep -o '"orderId":"[^"]*' | cut -d':' -f2 | tr -d '"')
- if [ ! -z "$order_id" ]; then
- ((count++))
- echo "$count" > /tmp/web-log-gen-create-$$.txt
- echo "[$(date '+%H:%M:%S')] CREATE #$count"
- sleep 0.5
- continue
- fi
- fi
- failed=$(<"$STATS_FILE" cut -d' ' -f3)
- echo "0 0 $((failed+1))" > "$STATS_FILE"
- sleep 0.5
- done
- ) &
- # 支付订单
- (
- count=0
- while true; do
- sleep 1.2
- order_id=$(<"/tmp/web-log-gen-create-$$.txt" head -1 2>/dev/null)
- if [ ! -z "$order_id" ] && [ "$order_id" != "0" ]; then
- price="$((RANDOM % 500 + 10)).$(printf "%02d" $((RANDOM % 100)))"
- method="${METHODS[$((RANDOM % ${#METHODS[@]}))]}"
-
- resp=$(curl -s -X POST "$API_URL/payment/pay" \
- -H "Content-Type: application/json" \
- -d "{\"orderId\":\"$order_id\",\"amount\":$price,\"paymentMethod\":\"$method\"}" 2>/dev/null)
-
- if echo "$resp" | grep -q '"code":0'; then
- ((count++))
- echo "$count"
- fi
- fi
- sleep 0.8
- done
- ) > /tmp/web-log-gen-payment-$$.txt 2>&1 &
- # 监控
- echo "运行中 (Ctrl+C 停止)..."
- echo ""
- while true; do
- create=$(cat /tmp/web-log-gen-create-$$.txt 2>/dev/null || echo "0")
- payment=$(cat /tmp/web-log-gen-payment-$$.txt 2>/dev/null | tail -1 || echo "0")
- failed=$(<"$STATS_FILE" cut -d' ' -f3)
-
- total=$((create + payment))
- echo -ne "\r创建: $create | 支付: $payment | 失败: $failed | 总计: $total "
-
- sleep 2
- done
|