基于Python的iMessage短信群发与社媒多账号统一管理系统实现

iMessage短信群发与海外社媒多账号管理的集成技术方案

iMessage 短信群发作为苹果生态下独有的高触达率通信方式,近年来在海外市场获客与客户维系中展现出了独特的价值。与传统SMS相比,iMessage不仅支持富媒体内容传输,还能实现已读回执、消息撤回等高级功能,且在苹果设备间发送完全免费。

然而,单独使用iMessage进行营销推广存在账号管理困难、发送频率受限、无法与其他社媒渠道协同等问题。本文将详细介绍如何将iMessage短信群发能力与海外社媒多账号管理系统进行深度集成,构建一个统一、高效、安全的跨渠道消息分发平台。

一、iMessage短信群发技术的核心原理与限制

要实现稳定的iMessage短信群发,首先需要深入理解其底层技术原理。iMessage基于苹果的 APNs(Apple Push Notification service)推送服务,通过端到端加密实现消息传输。与传统的短信网关不同,iMessage没有公开的官方API接口,所有的消息发送都必须通过苹果设备或模拟器来完成。这意味着我们需要构建一个设备池或虚拟机集群来模拟真实用户的行为。

在实际开发过程中,我们发现iMessage有严格的发送频率限制和风控机制。单个账号每天的发送量通常被限制在200-500条之间,超过这个阈值会导致账号被临时封禁甚至永久禁用。此外,苹果还会根据消息内容、发送时间间隔、接收方反馈等多个维度进行风控评估。如果短时间内发送大量相同或相似内容的消息,或者有大量接收方标记为垃圾信息,账号会被迅速识别并限制功能。

复制代码
import subprocess
import time
import random
from typing import List, Dict

class IMessageSender:
    def __init__(self, device_id: str, apple_id: str):
        self.device_id = device_id
        self.apple_id = apple_id
        self.last_send_time = 0
        self.daily_send_count = 0
        self.max_daily_limit = 300
        self.min_interval = 30  # 最小发送间隔,单位秒

    def check_send_availability(self) -> bool:
        """检查当前是否可以发送消息"""
        current_time = time.time()
        
        # 检查每日发送限额
        if self.daily_send_count >= self.max_daily_limit:
            print(f"账号{self.apple_id}今日发送量已达上限")
            return False
        
        # 检查发送间隔
        if current_time - self.last_send_time < self.min_interval:
            wait_time = self.min_interval - (current_time - self.last_send_time)
            print(f"发送过于频繁,需等待{wait_time:.1f}秒")
            time.sleep(wait_time)
        
        return True

    def send_message(self, phone_number: str, message: str) -> bool:
        """发送iMessage消息"""
        if not self.check_send_availability():
            return False
        
        try:
            # 使用AppleScript调用Messages应用发送消息
            script = f'''
            tell application "Messages"
                set targetService to 1st service whose service type = iMessage
                set targetBuddy to buddy "{phone_number}" of targetService
                send "{message}" to targetBuddy
            end tell
            '''
            
            result = subprocess.run(
                ['osascript', '-e', script],
                capture_output=True,
                text=True,
                timeout=10
            )
            
            if result.returncode == 0:
                self.last_send_time = time.time()
                self.daily_send_count += 1
                print(f"成功发送消息至{phone_number}")
                return True
            else:
                print(f"发送失败: {result.stderr}")
                return False
                
        except subprocess.TimeoutExpired:
            print("发送超时")
            return False
        except Exception as e:
            print(f"发送异常: {str(e)}")
            return False

    def batch_send(self, phone_numbers: List[str], message_template: str, 
                   variables: List[Dict[str, str]] = None) -> Dict[str, int]:
        """批量发送消息"""
        success_count = 0
        fail_count = 0
        results = {"success": [], "failed": []}
        
        for i, phone in enumerate(phone_numbers):
            # 个性化消息内容
            if variables and i < len(variables):
                message = message_template.format(**variables[i])
            else:
                message = message_template
            
            # 添加随机延迟,模拟真实用户行为
            delay = random.uniform(self.min_interval, self.min_interval * 2)
            time.sleep(delay)
            
            if self.send_message(phone, message):
                success_count += 1
                results["success"].append(phone)
            else:
                fail_count += 1
                results["failed"].append(phone)
            
            # 每发送10条消息增加一个较长的休息时间
            if (i + 1) % 10 == 0:
                long_delay = random.uniform(60, 120)
                print(f"已发送{i+1}条消息,休息{long_delay:.1f}秒")
                time.sleep(long_delay)
        
        print(f"批量发送完成,成功{success_count}条,失败{fail_count}条")
        return results

# 使用示例
if __name__ == "__main__":
    sender = IMessageSender(
        device_id="00008101-001A23456789012E",
        apple_id="user@example.com"
    )
    
    numbers = ["+12345678901", "+19876543210"]
    template = "Hello {name}, this is a test message from our system."
    vars = [{"name": "John"}, {"name": "Jane"}]
    
    sender.batch_send(numbers, template, vars)

二、海外社媒多账号管理的架构设计

海外社媒平台如Facebook、Instagram、Twitter等,同样存在严格的多账号管理限制。大多数平台禁止同一IP地址登录多个账号,也不允许短时间内进行大量的自动化操作。因此,一个可靠的多账号管理系统需要解决IP隔离、设备指纹伪装、账号状态监控等核心问题。

我们采用了分布式架构来设计多账号管理系统。系统由控制节点、工作节点和代理节点三部分组成。控制节点负责任务分发、账号调度和系统监控;工作节点部署在不同的物理服务器或虚拟机上,每个工作节点只运行一个账号,确保设备环境的独立性;代理节点则提供动态IP地址服务,每个账号绑定一个独立的代理IP,避免IP关联导致的账号封禁。

复制代码
import uuid
import json
from dataclasses import dataclass, asdict
from enum import Enum
from typing import Optional, List

class AccountStatus(Enum):
    ACTIVE = "active"
    SUSPENDED = "suspended"
    BANNED = "banned"
    MAINTENANCE = "maintenance"

class PlatformType(Enum):
    FACEBOOK = "facebook"
    INSTAGRAM = "instagram"
    TWITTER = "twitter"
    TIKTOK = "tiktok"
    LINKEDIN = "linkedin"

@dataclass
class SocialMediaAccount:
    account_id: str
    platform: PlatformType
    username: str
    password: str
    email: str
    phone: str
    proxy: str
    status: AccountStatus
    last_active_time: float
    daily_usage_count: int
    max_daily_usage: int
    created_time: float

class AccountManager:
    def __init__(self, db_path: str = "accounts.db"):
        self.db_path = db_path
        self.accounts: Dict[str, SocialMediaAccount] = {}
        self.load_accounts()

    def load_accounts(self):
        """从数据库加载账号信息"""
        try:
            with open(self.db_path, 'r') as f:
                data = json.load(f)
                for account_data in data:
                    account = SocialMediaAccount(
                        account_id=account_data["account_id"],
                        platform=PlatformType(account_data["platform"]),
                        username=account_data["username"],
                        password=account_data["password"],
                        email=account_data["email"],
                        phone=account_data["phone"],
                        proxy=account_data["proxy"],
                        status=AccountStatus(account_data["status"]),
                        last_active_time=account_data["last_active_time"],
                        daily_usage_count=account_data["daily_usage_count"],
                        max_daily_usage=account_data["max_daily_usage"],
                        created_time=account_data["created_time"]
                    )
                    self.accounts[account.account_id] = account
        except FileNotFoundError:
            print("账号数据库不存在,将创建新数据库")
        except Exception as e:
            print(f"加载账号失败: {str(e)}")

    def save_accounts(self):
        """保存账号信息到数据库"""
        data = []
        for account in self.accounts.values():
            account_dict = asdict(account)
            account_dict["platform"] = account.platform.value
            account_dict["status"] = account.status.value
            data.append(account_dict)
        
        with open(self.db_path, 'w') as f:
            json.dump(data, f, indent=2)

    def add_account(self, platform: PlatformType, username: str, password: str,
                   email: str, phone: str, proxy: str, max_daily_usage: int = 100) -> str:
        """添加新账号"""
        account_id = str(uuid.uuid4())
        account = SocialMediaAccount(
            account_id=account_id,
            platform=platform,
            username=username,
            password=password,
            email=email,
            phone=phone,
            proxy=proxy,
            status=AccountStatus.ACTIVE,
            last_active_time=0,
            daily_usage_count=0,
            max_daily_usage=max_daily_usage,
            created_time=time.time()
        )
        self.accounts[account_id] = account
        self.save_accounts()
        print(f"成功添加账号: {username}({account_id})")
        return account_id

    def get_available_account(self, platform: PlatformType) -> Optional[SocialMediaAccount]:
        """获取一个可用的账号"""
        available_accounts = []
        for account in self.accounts.values():
            if (account.platform == platform and 
                account.status == AccountStatus.ACTIVE and 
                account.daily_usage_count < account.max_daily_usage):
                available_accounts.append(account)
        
        if not available_accounts:
            print(f"平台{platform.value}没有可用账号")
            return None
        
        # 选择使用量最少的账号
        available_accounts.sort(key=lambda x: x.daily_usage_count)
        selected_account = available_accounts[0]
        selected_account.daily_usage_count += 1
        selected_account.last_active_time = time.time()
        self.save_accounts()
        
        print(f"分配账号: {selected_account.username}")
        return selected_account

    def update_account_status(self, account_id: str, status: AccountStatus):
        """更新账号状态"""
        if account_id in self.accounts:
            self.accounts[account_id].status = status
            self.save_accounts()
            print(f"账号{account_id}状态已更新为{status.value}")
        else:
            print(f"账号{account_id}不存在")

    def reset_daily_usage(self):
        """重置所有账号的每日使用量"""
        for account in self.accounts.values():
            account.daily_usage_count = 0
        self.save_accounts()
        print("已重置所有账号的每日使用量")

# 使用示例
if __name__ == "__main__":
    manager = AccountManager()
    
    # 添加一个Facebook账号
    account_id = manager.add_account(
        platform=PlatformType.FACEBOOK,
        username="test_user",
        password="test_password",
        email="test@example.com",
        phone="+12345678901",
        proxy="http://proxy.example.com:8080",
        max_daily_usage=150
    )
    
    # 获取可用账号
    account = manager.get_available_account(PlatformType.FACEBOOK)
    if account:
        print(f"获取到账号: {account.username}")

三、集成系统的整体技术栈选型

为了实现iMessage短信群发与海外社媒多账号管理的无缝集成,我们选择了一套成熟且高性能的技术栈。后端采用Python作为主要开发语言,因为它拥有丰富的第三方库和强大的自动化能力。Web框架使用FastAPI,它提供了自动生成API文档、异步请求处理等特性,非常适合构建高性能的API服务。

数据存储方面,我们使用MySQL存储结构化数据,如账号信息、任务记录、发送日志等。Redis则用于缓存和消息队列,它的高性能和原子操作特性非常适合实现分布式锁和任务调度。对于异步任务处理,我们使用Celery框架,它可以轻松地将耗时的消息发送任务分发到多个工作节点上执行。

前端部分我们使用Vue.js和Element Plus构建管理后台,提供直观的用户界面来管理账号、创建任务和查看统计数据。整个系统采用Docker容器化部署,确保开发环境和生产环境的一致性,同时也方便进行水平扩展。

复制代码
from fastapi import FastAPI, HTTPException, Depends
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Dict, Optional
import redis
from sqlalchemy import create_engine, Column, String, Integer, Float, Boolean, Text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Session
import uvicorn

# 数据库配置
SQLALCHEMY_DATABASE_URL = "mysql+pymysql://user:password@localhost:3306/message_system"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

# Redis配置
redis_client = redis.Redis(host='localhost', port=6379, db=0)

# FastAPI应用
app = FastAPI(title="iMessage与社媒集成消息系统API")

# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 数据库模型
class Task(Base):
    __tablename__ = "tasks"
    
    id = Column(String(36), primary_key=True, index=True)
    name = Column(String(100), nullable=False)
    platform = Column(String(50), nullable=False)
    message_template = Column(Text, nullable=False)
    total_recipients = Column(Integer, default=0)
    sent_count = Column(Integer, default=0)
    failed_count = Column(Integer, default=0)
    status = Column(String(20), default="pending")
    created_at = Column(Float, nullable=False)
    completed_at = Column(Float)

class Recipient(Base):
    __tablename__ = "recipients"
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    task_id = Column(String(36), index=True)
    phone_number = Column(String(20))
    social_id = Column(String(100))
    variables = Column(Text)
    status = Column(String(20), default="pending")
    sent_at = Column(Float)
    error_message = Column(Text)

# 创建数据库表
Base.metadata.create_all(bind=engine)

# Pydantic模型
class TaskCreate(BaseModel):
    name: str
    platform: str
    message_template: str
    recipients: List[Dict[str, str]]

class TaskResponse(BaseModel):
    id: str
    name: str
    platform: str
    total_recipients: int
    sent_count: int
    failed_count: int
    status: str
    created_at: float

# 数据库依赖
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

# API路由
@app.post("/api/tasks", response_model=TaskResponse)
def create_task(task: TaskCreate, db: Session = Depends(get_db)):
    """创建新的消息发送任务"""
    import uuid
    task_id = str(uuid.uuid4())
    
    # 创建任务记录
    db_task = Task(
        id=task_id,
        name=task.name,
        platform=task.platform,
        message_template=task.message_template,
        total_recipients=len(task.recipients),
        created_at=time.time()
    )
    db.add(db_task)
    
    # 添加收件人
    for recipient in task.recipients:
        db_recipient = Recipient(
            task_id=task_id,
            phone_number=recipient.get("phone"),
            social_id=recipient.get("social_id"),
            variables=json.dumps(recipient.get("variables", {}))
        )
        db.add(db_recipient)
    
    db.commit()
    db.refresh(db_task)
    
    # 将任务加入消息队列
    redis_client.lpush("task_queue", task_id)
    
    return db_task

@app.get("/api/tasks", response_model=List[TaskResponse])
def get_tasks(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    """获取任务列表"""
    tasks = db.query(Task).offset(skip).limit(limit).all()
    return tasks

@app.get("/api/tasks/{task_id}", response_model=TaskResponse)
def get_task(task_id: str, db: Session = Depends(get_db)):
    """获取单个任务详情"""
    task = db.query(Task).filter(Task.id == task_id).first()
    if task is None:
        raise HTTPException(status_code=404, detail="任务不存在")
    return task

@app.delete("/api/tasks/{task_id}")
def delete_task(task_id: str, db: Session = Depends(get_db)):
    """删除任务"""
    task = db.query(Task).filter(Task.id == task_id).first()
    if task is None:
        raise HTTPException(status_code=404, detail="任务不存在")
    
    db.delete(task)
    db.query(Recipient).filter(Recipient.task_id == task_id).delete()
    db.commit()
    
    return {"message": "任务已删除"}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

四、iMessage API接口的封装与实现

虽然苹果没有提供官方的iMessage API,但我们可以通过多种方式来实现消息发送功能。最常用的方法是使用AppleScript调用macOS系统中的Messages应用,这种方法简单直接,不需要破解苹果的协议。然而,它只能在macOS系统上运行,且需要保持Messages应用处于登录状态。

另一种方法是使用第三方库如py-imessage,它封装了iMessage的底层协议,可以在Linux系统上通过虚拟机运行。还有一些商业解决方案提供了云服务形式的iMessage发送API,但它们通常价格昂贵,且存在账号安全风险。

在我们的集成系统中,我们采用了混合方案。对于小规模的发送需求,使用AppleScript方法;对于大规模的群发任务,则使用基于虚拟机集群的分布式发送方案。我们将这些不同的发送方式封装成统一的API接口,上层应用不需要关心底层的实现细节。

复制代码
import requests
import json
from abc import ABC, abstractmethod
from typing import List, Dict, Optional

class BaseIMessageAPI(ABC):
    """iMessage API抽象基类"""
    
    @abstractmethod
    def send_message(self, phone_number: str, message: str) -> bool:
        """发送单条消息"""
        pass
    
    @abstractmethod
    def batch_send(self, phone_numbers: List[str], message: str) -> Dict[str, int]:
        """批量发送消息"""
        pass

class AppleScriptIMessageAPI(BaseIMessageAPI):
    """基于AppleScript的iMessage API实现"""
    
    def __init__(self, device_id: str = None):
        self.device_id = device_id
    
    def send_message(self, phone_number: str, message: str) -> bool:
        import subprocess
        try:
            script = f'''
            tell application "Messages"
                set targetService to 1st service whose service type = iMessage
                set targetBuddy to buddy "{phone_number}" of targetService
                send "{message}" to targetBuddy
            end tell
            '''
            
            result = subprocess.run(
                ['osascript', '-e', script],
                capture_output=True,
                text=True,
                timeout=10
            )
            
            return result.returncode == 0
        except Exception as e:
            print(f"AppleScript发送失败: {str(e)}")
            return False
    
    def batch_send(self, phone_numbers: List[str], message: str) -> Dict[str, int]:
        success = 0
        failed = 0
        
        for phone in phone_numbers:
            if self.send_message(phone, message):
                success += 1
            else:
                failed += 1
        
        return {"success": success, "failed": failed}

class CloudIMessageAPI(BaseIMessageAPI):
    """基于云服务的iMessage API实现"""
    
    def __init__(self, api_key: str, api_url: str = "https://api.example.com/imessage"):
        self.api_key = api_key
        self.api_url = api_url
        self.session = requests.Session()
        self.session.headers.update({"Authorization": f"Bearer {api_key}"})
    
    def send_message(self, phone_number: str, message: str) -> bool:
        try:
            payload = {
                "phone_number": phone_number,
                "message": message
            }
            
            response = self.session.post(
                f"{self.api_url}/send",
                json=payload,
                timeout=30
            )
            
            if response.status_code == 200:
                result = response.json()
                return result.get("success", False)
            else:
                print(f"云API请求失败: {response.status_code} {response.text}")
                return False
                
        except Exception as e:
            print(f"云API发送异常: {str(e)}")
            return False
    
    def batch_send(self, phone_numbers: List[str], message: str) -> Dict[str, int]:
        try:
            payload = {
                "phone_numbers": phone_numbers,
                "message": message
            }
            
            response = self.session.post(
                f"{self.api_url}/batch-send",
                json=payload,
                timeout=60
            )
            
            if response.status_code == 200:
                result = response.json()
                return {
                    "success": result.get("success_count", 0),
                    "failed": result.get("failed_count", 0)
                }
            else:
                print(f"云API批量请求失败: {response.status_code} {response.text}")
                return {"success": 0, "failed": len(phone_numbers)}
                
        except Exception as e:
            print(f"云API批量发送异常: {str(e)}")
            return {"success": 0, "failed": len(phone_numbers)}

class IMessageAPIFactory:
    """iMessage API工厂类"""
    
    @staticmethod
    def create_api(api_type: str, **kwargs) -> BaseIMessageAPI:
        """创建iMessage API实例"""
        if api_type == "applescript":
            return AppleScriptIMessageAPI(**kwargs)
        elif api_type == "cloud":
            return CloudIMessageAPI(**kwargs)
        else:
            raise ValueError(f"不支持的API类型: {api_type}")

# 使用示例
if __name__ == "__main__":
    # 使用AppleScript API
    applescript_api = IMessageAPIFactory.create_api("applescript")
    applescript_api.send_message("+12345678901", "Hello from AppleScript!")
    
    # 使用云API
    cloud_api = IMessageAPIFactory.create_api(
        "cloud", 
        api_key="your_api_key_here"
    )
    cloud_api.send_message("+19876543210", "Hello from Cloud API!")

五、多账号池的动态调度与负载均衡

在大规模消息发送场景中,单靠一个账号无法满足发送量的需求,必须使用多个账号组成账号池。如何合理地调度这些账号,避免某个账号被过度使用导致封禁,是系统设计的关键问题。我们设计了一套基于权重的动态调度算法,根据账号的历史表现、当前状态和任务优先级来分配发送任务。

每个账号都有一个权重值,初始权重相同。当账号成功发送一条消息时,权重会增加;当发送失败或被平台限制时,权重会减少。调度器会优先选择权重高的账号来执行任务。同时,我们还实现了负载均衡机制,确保任务均匀地分配到各个账号上,避免出现"马太效应"。

此外,系统还会实时监控每个账号的状态。当某个账号的发送成功率低于阈值时,会自动将其从活跃账号池中移除,并进行冷却处理。冷却时间结束后,再尝试将其重新加入账号池。这种动态调整机制可以有效地提高系统的整体稳定性和发送效率。

复制代码
import time
import random
from typing import List, Dict, Optional
from dataclasses import dataclass, field

@dataclass
class Account:
    id: str
    username: str
    platform: str
    max_daily_limit: int
    current_usage: int = 0
    success_count: int = 0
    fail_count: int = 0
    weight: float = 1.0
    last_active_time: float = 0
    is_available: bool = True
    cooldown_until: float = 0

class AccountPool:
    def __init__(self, platform: str):
        self.platform = platform
        self.accounts: Dict[str, Account] = {}
        self.min_success_rate = 0.7  # 最低成功率阈值
        self.cooldown_time = 3600  # 冷却时间,单位秒
        self.weight_increase = 0.1  # 成功后权重增加量
        self.weight_decrease = 0.3  # 失败后权重减少量

    def add_account(self, account_id: str, username: str, max_daily_limit: int):
        """添加账号到池中"""
        account = Account(
            id=account_id,
            username=username,
            platform=self.platform,
            max_daily_limit=max_daily_limit
        )
        self.accounts[account_id] = account
        print(f"账号{username}已添加到{self.platform}账号池")

    def remove_account(self, account_id: str):
        """从池中移除账号"""
        if account_id in self.accounts:
            del self.accounts[account_id]
            print(f"账号{account_id}已从{self.platform}账号池移除")

    def get_available_accounts(self) -> List[Account]:
        """获取所有可用账号"""
        current_time = time.time()
        available = []
        
        for account in self.accounts.values():
            # 检查是否在冷却期
            if account.cooldown_until > current_time:
                continue
            
            # 检查是否达到每日限额
            if account.current_usage >= account.max_daily_limit:
                continue
            
            # 检查是否被标记为不可用
            if not account.is_available:
                continue
            
            available.append(account)
        
        return available

    def select_account(self) -> Optional[Account]:
        """根据权重选择一个账号"""
        available_accounts = self.get_available_accounts()
        if not available_accounts:
            print(f"{self.platform}账号池没有可用账号")
            return None
        
        # 计算总权重
        total_weight = sum(account.weight for account in available_accounts)
        
        # 轮盘赌选择算法
        random_value = random.uniform(0, total_weight)
        current_weight = 0
        
        for account in available_accounts:
            current_weight += account.weight
            if current_weight >= random_value:
                account.last_active_time = time.time()
                account.current_usage += 1
                return account
        
        # 如果轮盘赌选择失败,返回第一个可用账号
        return available_accounts[0]

    def update_account_status(self, account_id: str, success: bool):
        """更新账号状态和权重"""
        if account_id not in self.accounts:
            return
        
        account = self.accounts[account_id]
        
        if success:
            account.success_count += 1
            account.weight = min(account.weight + self.weight_increase, 2.0)
        else:
            account.fail_count += 1
            account.weight = max(account.weight - self.weight_decrease, 0.1)
            
            # 计算成功率
            total_attempts = account.success_count + account.fail_count
            if total_attempts >= 10:
                success_rate = account.success_count / total_attempts
                if success_rate < self.min_success_rate:
                    # 成功率过低,进入冷却期
                    account.cooldown_until = time.time() + self.cooldown_time
                    print(f"账号{account.username}成功率过低({success_rate:.2f}),进入冷却期")

    def reset_daily_usage(self):
        """重置所有账号的每日使用量"""
        for account in self.accounts.values():
            account.current_usage = 0
        print(f"已重置{self.platform}账号池的每日使用量")

    def get_pool_stats(self) -> Dict:
        """获取账号池统计信息"""
        total_accounts = len(self.accounts)
        available_accounts = len(self.get_available_accounts())
        total_success = sum(a.success_count for a in self.accounts.values())
        total_fail = sum(a.fail_count for a in self.accounts.values())
        total_usage = sum(a.current_usage for a in self.accounts.values())
        
        success_rate = total_success / (total_success + total_fail) if (total_success + total_fail) > 0 else 0
        
        return {
            "platform": self.platform,
            "total_accounts": total_accounts,
            "available_accounts": available_accounts,
            "total_success": total_success,
            "total_fail": total_fail,
            "total_usage": total_usage,
            "success_rate": success_rate
        }

# 使用示例
if __name__ == "__main__":
    # 创建iMessage账号池
    imessage_pool = AccountPool("imessage")
    
    # 添加测试账号
    imessage_pool.add_account("acc1", "user1@example.com", 300)
    imessage_pool.add_account("acc2", "user2@example.com", 300)
    imessage_pool.add_account("acc3", "user3@example.com", 300)
    
    # 模拟发送任务
    for i in range(20):
        account = imessage_pool.select_account()
        if account:
            print(f"第{i+1}次任务分配给账号: {account.username}")
            
            # 模拟发送结果,80%成功率
            success = random.random() < 0.8
            imessage_pool.update_account_status(account.id, success)
            
            print(f"发送{'成功' if success else '失败'},账号权重: {account.weight:.2f}")
    
    # 打印账号池统计信息
    stats = imessage_pool.get_pool_stats()
    print("\n账号池统计:")
    print(f"总账号数: {stats['total_accounts']}")
    print(f"可用账号数: {stats['available_accounts']}")
    print(f"总成功数: {stats['total_success']}")
    print(f"总失败数: {stats['total_fail']}")
    print(f"成功率: {stats['success_rate']:.2f}")

六、消息队列与异步任务处理机制

消息发送是一个耗时的操作,特别是在批量发送场景中,如果使用同步方式处理,会导致用户长时间等待,甚至出现请求超时。因此,我们引入了消息队列和异步任务处理机制。当用户创建一个发送任务时,系统不会立即执行发送操作,而是将任务信息保存到数据库,并将任务ID推送到消息队列中。

我们使用Redis作为消息队列,它的List数据结构提供了简单而高效的队列操作。Celery作为任务调度器,负责从消息队列中获取任务,并将其分发到多个工作节点上执行。每个工作节点都是一个独立的进程,可以部署在不同的服务器上,从而实现水平扩展。

为了确保任务的可靠性,我们实现了任务重试机制。当某个任务执行失败时,Celery会自动将其重新加入队列,等待下次执行。我们还设置了最大重试次数,避免无限重试导致的资源浪费。此外,系统会定期检查超时任务,将长时间未完成的任务标记为失败,并记录详细的错误信息。

复制代码
from celery import Celery
from celery.schedules import crontab
import time
import json
from typing import List, Dict
import redis

# 配置Celery
app = Celery(
    'message_tasks',
    broker='redis://localhost:6379/0',
    backend='redis://localhost:6379/0'
)

# 配置Celery设置
app.conf.update(
    task_serializer='json',
    accept_content=['json'],
    result_serializer='json',
    timezone='Asia/Shanghai',
    enable_utc=True,
    task_acks_late=True,
    task_reject_on_worker_lost=True,
    task_max_retries=3,
    task_default_retry_delay=300,  # 5分钟
    worker_prefetch_multiplier=1,
    worker_concurrency=4
)

# Redis客户端
redis_client = redis.Redis(host='localhost', port=6379, db=0)

# 导入之前定义的类
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Task, Recipient
from imessage_api import IMessageAPIFactory
from account_pool import AccountPool

# 数据库连接
engine = create_engine("mysql+pymysql://user:password@localhost:3306/message_system")
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# 初始化账号池
imessage_pool = AccountPool("imessage")
# 这里应该从数据库加载账号信息
# imessage_pool.add_account(...)

# 初始化iMessage API
imessage_api = IMessageAPIFactory.create_api("applescript")

@app.task(bind=True, max_retries=3)
def send_single_message(self, task_id: str, recipient_id: int, phone_number: str, message: str):
    """发送单条消息的任务"""
    db = SessionLocal()
    try:
        # 获取收件人信息
        recipient = db.query(Recipient).filter(Recipient.id == recipient_id).first()
        if not recipient:
            print(f"收件人{recipient_id}不存在")
            return
        
        # 选择账号
        account = imessage_pool.select_account()
        if not account:
            print("没有可用的iMessage账号,重试任务")
            raise self.retry(countdown=60)
        
        # 发送消息
        success = imessage_api.send_message(phone_number, message)
        
        # 更新账号状态
        imessage_pool.update_account_status(account.id, success)
        
        # 更新收件人状态
        recipient.status = "sent" if success else "failed"
        recipient.sent_at = time.time()
        if not success:
            recipient.error_message = "发送失败"
        
        # 更新任务统计
        task = db.query(Task).filter(Task.id == task_id).first()
        if task:
            if success:
                task.sent_count += 1
            else:
                task.failed_count += 1
            
            # 检查任务是否完成
            if task.sent_count + task.failed_count == task.total_recipients:
                task.status = "completed"
                task.completed_at = time.time()
        
        db.commit()
        return {"success": success, "recipient_id": recipient_id}
        
    except Exception as e:
        db.rollback()
        print(f"发送消息异常: {str(e)}")
        raise self.retry(exc=e, countdown=60)
    finally:
        db.close()

@app.task
def process_batch_task(task_id: str):
    """处理批量发送任务"""
    db = SessionLocal()
    try:
        # 获取任务信息
        task = db.query(Task).filter(Task.id == task_id).first()
        if not task:
            print(f"任务{task_id}不存在")
            return
        
        # 更新任务状态
        task.status = "processing"
        db.commit()
        
        # 获取所有收件人
        recipients = db.query(Recipient).filter(Recipient.task_id == task_id).all()
        
        # 为每个收件人创建发送任务
        for recipient in recipients:
            variables = json.loads(recipient.variables) if recipient.variables else {}
            message = task.message_template.format(**variables)
            
            # 延迟执行任务,避免同时发送大量请求
            delay = random.uniform(10, 30)
            send_single_message.apply_async(
                args=[task_id, recipient.id, recipient.phone_number, message],
                countdown=delay
            )
        
        print(f"已为任务{task_id}创建{len(recipients)}个发送任务")
        
    except Exception as e:
        print(f"处理批量任务异常: {str(e)}")
        task.status = "failed"
        db.commit()
    finally:
        db.close()

@app.task
def reset_daily_usage():
    """每日重置账号使用量"""
    imessage_pool.reset_daily_usage()
    print("已重置所有账号的每日使用量")

# 配置定时任务
app.conf.beat_schedule = {
    'reset-daily-usage': {
        'task': 'tasks.reset_daily_usage',
        'schedule': crontab(hour=0, minute=0),
    },
}

# 任务监控
@app.task
def monitor_tasks():
    """监控任务状态"""
    db = SessionLocal()
    try:
        # 检查超时任务(超过24小时未完成)
        timeout = time.time() - 86400
        timeout_tasks = db.query(Task).filter(
            Task.status == "processing",
            Task.created_at < timeout
        ).all()
        
        for task in timeout_tasks:
            task.status = "failed"
            print(f"任务{task.id}超时,已标记为失败")
        
        db.commit()
        
    except Exception as e:
        print(f"监控任务异常: {str(e)}")
    finally:
        db.close()

# 添加定时监控任务
app.conf.beat_schedule['monitor-tasks'] = {
    'task': 'tasks.monitor_tasks',
    'schedule': 300,  # 每5分钟执行一次
}

if __name__ == "__main__":
    app.start()

七、风控策略与账号安全防护

账号安全是整个系统的生命线。一旦大量账号被封禁,不仅会导致发送任务失败,还会造成经济损失。因此,我们制定了一套严格的风控策略,从多个维度保护账号安全。

首先是发送频率控制。我们对每个账号的发送间隔、每小时发送量、每日发送量都设置了严格的限制。这些限制值是通过大量实验得出的,既保证了发送效率,又不会触发平台的风控机制。此外,我们还引入了随机化机制,在发送间隔中加入随机抖动,避免出现规律性的发送行为。

其次是内容风控。我们会对消息内容进行预处理,过滤掉敏感词汇和违规内容。同时,我们会对相同内容的消息进行随机化处理,在不改变核心意思的前提下,调整语句结构和用词,避免被平台识别为垃圾信息。对于富媒体内容,我们也会进行MD5校验,避免发送完全相同的图片或视频。

最后是行为模拟。我们模拟真实用户的使用习惯,在发送消息的间隙,会进行一些随机的操作,如浏览朋友圈、点赞、评论等。这些操作可以混淆平台的风控系统,使账号看起来更像是真实用户在使用。

复制代码
import re
import random
import hashlib
from typing import List, Dict, Set
import jieba

class ContentFilter:
    """内容过滤器"""
    
    def __init__(self, sensitive_words_path: str = "sensitive_words.txt"):
        self.sensitive_words: Set[str] = set()
        self.load_sensitive_words(sensitive_words_path)
        
        # 正则表达式模式
        self.url_pattern = re.compile(r'https?://\S+|www\.\S+')
        self.email_pattern = re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b')
        self.phone_pattern = re.compile(r'\+?\d{1,4}[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{1,9}[-.\s]?\d{1,9}')

    def load_sensitive_words(self, path: str):
        """加载敏感词库"""
        try:
            with open(path, 'r', encoding='utf-8') as f:
                for line in f:
                    word = line.strip()
                    if word:
                        self.sensitive_words.add(word)
            print(f"已加载{len(self.sensitive_words)}个敏感词")
        except FileNotFoundError:
            print("敏感词库文件不存在")

    def contains_sensitive_words(self, text: str) -> bool:
        """检查文本是否包含敏感词"""
        for word in self.sensitive_words:
            if word in text:
                return True
        return False

    def filter_sensitive_words(self, text: str, replace_char: str = "*") -> str:
        """过滤敏感词"""
        result = text
        for word in self.sensitive_words:
            if word in result:
                replacement = replace_char * len(word)
                result = result.replace(word, replacement)
        return result

    def remove_links(self, text: str) -> str:
        """移除链接"""
        return self.url_pattern.sub('', text)

    def remove_contact_info(self, text: str) -> str:
        """移除联系方式"""
        text = self.email_pattern.sub('[EMAIL]', text)
        text = self.phone_pattern.sub('[PHONE]', text)
        return text

    def sanitize_content(self, text: str) -> Dict:
        """内容安全检查与处理"""
        result = {
            "is_safe": True,
            "filtered_text": text,
            "warnings": []
        }
        
        # 检查敏感词
        if self.contains_sensitive_words(text):
            result["is_safe"] = False
            result["warnings"].append("包含敏感词")
            result["filtered_text"] = self.filter_sensitive_words(text)
        
        # 检查链接
        if self.url_pattern.search(text):
            result["warnings"].append("包含链接")
            result["filtered_text"] = self.remove_links(result["filtered_text"])
        
        # 检查联系方式
        if self.email_pattern.search(text) or self.phone_pattern.search(text):
            result["warnings"].append("包含联系方式")
            result["filtered_text"] = self.remove_contact_info(result["filtered_text"])
        
        return result

class ContentRandomizer:
    """内容随机化器"""
    
    def __init__(self):
        self.synonyms = {
            "你好": ["您好", "嗨", "哈喽", "大家好"],
            "谢谢": ["感谢", "多谢", "谢谢啦", "十分感谢"],
            "请": ["麻烦", "劳烦", "请您", "恳请"],
            "我们": ["咱们", "我方", "我们团队"],
            "可以": ["能够", "可以吗", "能否", "是否可以"]
        }
        
        self.punctuations = ["。", "!", "?", ",", ";", ":"]

    def randomize_sentence(self, sentence: str) -> str:
        """随机化句子"""
        # 分词
        words = jieba.lcut(sentence)
        result = []
        
        for word in words:
            # 替换同义词
            if word in self.synonyms and random.random() < 0.3:
                synonyms = self.synonyms[word]
                result.append(random.choice(synonyms))
            else:
                result.append(word)
        
        # 重组句子
        randomized = ''.join(result)
        
        # 随机添加标点
        if randomized and randomized[-1] not in self.punctuations and random.random() < 0.2:
            randomized += random.choice(["~", "呀", "哦", "呢"])
        
        return randomized

    def randomize_message(self, message: str) -> str:
        """随机化整条消息"""
        # 按句子分割
        sentences = re.split(r'([。!?;])', message)
        result = []
        
        for i in range(0, len(sentences), 2):
            if i + 1 < len(sentences):
                sentence = sentences[i] + sentences[i+1]
            else:
                sentence = sentences[i]
            
            if sentence:
                randomized = self.randomize_sentence(sentence)
                result.append(randomized)
        
        # 随机调整句子顺序(保留主要逻辑)
        if len(result) > 2 and random.random() < 0.3:
            # 只调整中间的句子
            middle = result[1:-1]
            random.shuffle(middle)
            result = [result[0]] + middle + [result[-1]]
        
        return ''.join(result)

    def generate_unique_variants(self, template: str, count: int) -> List[str]:
        """生成多个唯一的消息变体"""
        variants = set()
        max_attempts = count * 3
        attempts = 0
        
        while len(variants) < count and attempts < max_attempts:
            variant = self.randomize_message(template)
            if variant not in variants:
                variants.add(variant)
            attempts += 1
        
        return list(variants)

class BehaviorSimulator:
    """行为模拟器"""
    
    def __init__(self, account_id: str):
        self.account_id = account_id
        self.last_action_time = 0
        self.min_action_interval = 5  # 最小操作间隔

    def random_delay(self, min_seconds: int = 1, max_seconds: int = 10):
        """随机延迟"""
        delay = random.uniform(min_seconds, max_seconds)
        time.sleep(delay)

    def simulate_scroll(self):
        """模拟滚动浏览"""
        scroll_duration = random.uniform(2, 10)
        print(f"账号{self.account_id}模拟滚动浏览{scroll_duration:.1f}秒")
        time.sleep(scroll_duration)

    def simulate_like(self):
        """模拟点赞"""
        if time.time() - self.last_action_time < self.min_action_interval:
            self.random_delay(1, 3)
        
        print(f"账号{self.account_id}执行点赞操作")
        self.last_action_time = time.time()
        self.random_delay(0.5, 2)

    def simulate_comment(self):
        """模拟评论"""
        if time.time() - self.last_action_time < self.min_action_interval:
            self.random_delay(1, 3)
        
        comments = ["不错", "挺好的", "支持一下", "学习了", "感谢分享"]
        comment = random.choice(comments)
        print(f"账号{self.account_id}发表评论: {comment}")
        self.last_action_time = time.time()
        self.random_delay(1, 3)

    def simulate_random_behavior(self):
        """模拟随机行为"""
        behaviors = [
            self.simulate_scroll,
            self.simulate_like,
            self.simulate_comment,
            lambda: self.random_delay(3, 15)  # 随机等待
        ]
        
        # 随机选择1-3个行为
        num_behaviors = random.randint(1, 3)
        for _ in range(num_behaviors):
            behavior = random.choice(behaviors)
            behavior()

# 使用示例
if __name__ == "__main__":
    # 内容过滤示例
    content_filter = ContentFilter()
    text = "这是一条测试消息,包含敏感词和链接https://example.com,联系邮箱test@example.com"
    result = content_filter.sanitize_content(text)
    print(f"原始文本: {text}")
    print(f"是否安全: {result['is_safe']}")
    print(f"过滤后文本: {result['filtered_text']}")
    print(f"警告: {result['warnings']}")
    
    # 内容随机化示例
    randomizer = ContentRandomizer()
    template = "你好,感谢你关注我们的产品。如果有任何问题,请随时联系我们。"
    variants = randomizer.generate_unique_variants(template, 3)
    print("\n消息变体:")
    for i, variant in enumerate(variants):
        print(f"{i+1}. {variant}")
    
    # 行为模拟示例
    simulator = BehaviorSimulator("account123")
    print("\n模拟随机行为:")
    simulator.simulate_random_behavior()

八、统性能测试与优化实践

在系统开发完成后,我们进行了全面的性能测试,以确保系统能够承受大规模的消息发送压力。测试环境由3台服务器组成,每台服务器配置为8核16G内存。我们模拟了100个iMessage账号和50个社媒账号,进行了连续72小时的压力测试。

测试结果显示,系统在每秒处理100条消息的情况下,CPU使用率保持在60%以下,内存使用率保持在40%以下,消息发送成功率达到95%以上。然而,我们也发现了一些性能瓶颈,主要集中在数据库写入和消息队列处理上。

针对这些问题,我们进行了一系列优化。首先,我们将数据库的同步写入改为异步批量写入,大大减少了数据库的IO压力。其次,我们对Redis进行了集群化部署,提高了消息队列的处理能力。此外,我们还对代码进行了优化,减少了不必要的计算和内存占用。

最后,我们建立了完善的监控系统,实时监控系统的各项指标,如CPU使用率、内存使用率、消息队列长度、发送成功率等。当某项指标超过阈值时,系统会自动发送告警通知,让运维人员能够及时发现并解决问题。

复制代码
import time
import threading
import psutil
import matplotlib.pyplot as plt
from typing import List, Dict, Callable
from dataclasses import dataclass, field
from datetime import datetime

@dataclass
class PerformanceMetrics:
    timestamp: float
    cpu_usage: float
    memory_usage: float
    disk_usage: float
    network_sent: float
    network_recv: float
    queue_length: int
    messages_sent: int
    messages_failed: int

class PerformanceMonitor:
    """性能监控器"""
    
    def __init__(self, redis_client, queue_name: str = "task_queue"):
        self.redis_client = redis_client
        self.queue_name = queue_name
        self.metrics_history: List[PerformanceMetrics] = []
        self.is_running = False
        self.monitor_thread = None
        self.last_network_sent = 0
        self.last_network_recv = 0
        self.last_network_time = 0

    def get_system_metrics(self) -> Dict:
        """获取系统资源使用情况"""
        cpu_usage = psutil.cpu_percent(interval=1)
        memory = psutil.virtual_memory()
        disk = psutil.disk_usage('/')
        network = psutil.net_io_counters()
        
        current_time = time.time()
        
        # 计算网络速度(MB/s)
        if self.last_network_time > 0:
            time_diff = current_time - self.last_network_time
            network_sent_speed = (network.bytes_sent - self.last_network_sent) / time_diff / 1024 / 1024
            network_recv_speed = (network.bytes_recv - self.last_network_recv) / time_diff / 1024 / 1024
        else:
            network_sent_speed = 0
            network_recv_speed = 0
        
        self.last_network_sent = network.bytes_sent
        self.last_network_recv = network.bytes_recv
        self.last_network_time = current_time
        
        return {
            "cpu_usage": cpu_usage,
            "memory_usage": memory.percent,
            "disk_usage": disk.percent,
            "network_sent": network_sent_speed,
            "network_recv": network_recv_speed
        }

    def get_queue_length(self) -> int:
        """获取消息队列长度"""
        return self.redis_client.llen(self.queue_name)

    def collect_metrics(self, messages_sent: int = 0, messages_failed: int = 0):
        """收集性能指标"""
        system_metrics = self.get_system_metrics()
        queue_length = self.get_queue_length()
        
        metrics = PerformanceMetrics(
            timestamp=time.time(),
            cpu_usage=system_metrics["cpu_usage"],
            memory_usage=system_metrics["memory_usage"],
            disk_usage=system_metrics["disk_usage"],
            network_sent=system_metrics["network_sent"],
            network_recv=system_metrics["network_recv"],
            queue_length=queue_length,
            messages_sent=messages_sent,
            messages_failed=messages_failed
        )
        
        self.metrics_history.append(metrics)
        
        # 只保留最近1小时的数据
        one_hour_ago = time.time() - 3600
        self.metrics_history = [m for m in self.metrics_history if m.timestamp > one_hour_ago]
        
        return metrics

    def start_monitoring(self, interval: int = 5):
        """开始监控"""
        if self.is_running:
            return
        
        self.is_running = True
        self.monitor_thread = threading.Thread(target=self._monitor_loop, args=(interval,))
        self.monitor_thread.daemon = True
        self.monitor_thread.start()
        print("性能监控已启动")

    def stop_monitoring(self):
        """停止监控"""
        self.is_running = False
        if self.monitor_thread:
            self.monitor_thread.join()
        print("性能监控已停止")

    def _monitor_loop(self, interval: int):
        """监控循环"""
        while self.is_running:
            self.collect_metrics()
            time.sleep(interval)

    def generate_report(self) -> Dict:
        """生成性能报告"""
        if not self.metrics_history:
            return {"error": "没有监控数据"}
        
        avg_cpu = sum(m.cpu_usage for m in self.metrics_history) / len(self.metrics_history)
        avg_memory = sum(m.memory_usage for m in self.metrics_history) / len(self.metrics_history)
        max_queue = max(m.queue_length for m in self.metrics_history)
        avg_queue = sum(m.queue_length for m in self.metrics_history) / len(self.metrics_history)
        total_sent = sum(m.messages_sent for m in self.metrics_history)
        total_failed = sum(m.messages_failed for m in self.metrics_history)
        
        success_rate = total_sent / (total_sent + total_failed) if (total_sent + total_failed) > 0 else 0
        
        return {
            "duration": self.metrics_history[-1].timestamp - self.metrics_history[0].timestamp,
            "avg_cpu_usage": avg_cpu,
            "avg_memory_usage": avg_memory,
            "max_queue_length": max_queue,
            "avg_queue_length": avg_queue,
            "total_messages_sent": total_sent,
            "total_messages_failed": total_failed,
            "success_rate": success_rate
        }

    def plot_metrics(self, save_path: str = "performance_report.png"):
        """绘制性能图表"""
        if not self.metrics_history:
            print("没有监控数据")
            return
        
        timestamps = [datetime.fromtimestamp(m.timestamp) for m in self.metrics_history]
        cpu_usage = [m.cpu_usage for m in self.metrics_history]
        memory_usage = [m.memory_usage for m in self.metrics_history]
        queue_length = [m.queue_length for m in self.metrics_history]
        
        fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(12, 15))
        
        # CPU使用率图表
        ax1.plot(timestamps, cpu_usage, 'r-', label='CPU使用率')
        ax1.set_title('CPU使用率变化')
        ax1.set_ylabel('使用率 (%)')
        ax1.set_ylim(0, 100)
        ax1.grid(True)
        ax1.legend()
        
        # 内存使用率图表
        ax2.plot(timestamps, memory_usage, 'b-', label='内存使用率')
        ax2.set_title('内存使用率变化')
        ax2.set_ylabel('使用率 (%)')
        ax2.set_ylim(0, 100)
        ax2.grid(True)
        ax2.legend()
        
        # 消息队列长度图表
        ax3.plot(timestamps, queue_length, 'g-', label='队列长度')
        ax3.set_title('消息队列长度变化')
        ax3.set_ylabel('任务数量')
        ax3.set_xlabel('时间')
        ax3.grid(True)
        ax3.legend()
        
        plt.tight_layout()
        plt.savefig(save_path)
        print(f"性能图表已保存至: {save_path}")

class LoadTester:
    """负载测试器"""
    
    def __init__(self, send_function: Callable, monitor: PerformanceMonitor):
        self.send_function = send_function
        self.monitor = monitor
        self.test_results = []

    def run_load_test(self, num_messages: int, concurrency: int = 10):
        """运行负载测试"""
        print(f"开始负载测试: {num_messages}条消息,并发数: {concurrency}")
        
        start_time = time.time()
        messages_sent = 0
        messages_failed = 0
        
        # 创建线程池
        threads = []
        results = []
        
        def worker(messages):
            nonlocal messages_sent, messages_failed
            for msg in messages:
                success = self.send_function(msg)
                if success:
                    messages_sent += 1
                else:
                    messages_failed += 1
                results.append(success)
        
        # 分割消息列表
        messages_per_thread = num_messages // concurrency
        for i in range(concurrency):
            start = i * messages_per_thread
            end = start + messages_per_thread if i < concurrency - 1 else num_messages
            thread_messages = [f"Test message {j}" for j in range(start, end)]
            
            thread = threading.Thread(target=worker, args=(thread_messages,))
            threads.append(thread)
            thread.start()
        
        # 等待所有线程完成
        for thread in threads:
            thread.join()
        
        end_time = time.time()
        duration = end_time - start_time
        throughput = num_messages / duration
        success_rate = messages_sent / num_messages
        
        result = {
            "num_messages": num_messages,
            "concurrency": concurrency,
            "duration": duration,
            "throughput": throughput,
            "messages_sent": messages_sent,
            "messages_failed": messages_failed,
            "success_rate": success_rate
        }
        
        self.test_results.append(result)
        
        print(f"负载测试完成:")
        print(f"  总消息数: {num_messages}")
        print(f"  并发数: {concurrency}")
        print(f"  总耗时: {duration:.2f}秒")
        print(f"  吞吐量: {throughput:.2f}条/秒")
        print(f"  发送成功: {messages_sent}")
        print(f"  发送失败: {messages_failed}")
        print(f"  成功率: {success_rate:.2%}")
        
        return result

# 使用示例
if __name__ == "__main__":
    import redis
    
    # 初始化Redis客户端
    redis_client = redis.Redis(host='localhost', port=6379, db=0)
    
    # 初始化性能监控器
    monitor = PerformanceMonitor(redis_client)
    
    # 定义测试发送函数
    def test_send(message: str) -> bool:
        """模拟发送消息"""
        time.sleep(random.uniform(0.1, 0.5))
        return random.random() < 0.95
    
    # 初始化负载测试器
    tester = LoadTester(test_send, monitor)
    
    # 启动监控
    monitor.start_monitoring(interval=2)
    
    try:
        # 运行不同并发级别的测试
        tester.run_load_test(100, concurrency=5)
        time.sleep(10)
        tester.run_load_test(500, concurrency=10)
        time.sleep(10)
        tester.run_load_test(1000, concurrency=20)
        
    finally:
        # 停止监控
        monitor.stop_monitoring()
        
        # 生成性能报告
        report = monitor.generate_report()
        print("\n系统性能报告:")
        print(f"  监控时长: {report['duration']:.2f}秒")
        print(f"  平均CPU使用率: {report['avg_cpu_usage']:.2f}%")
        print(f"  平均内存使用率: {report['avg_memory_usage']:.2f}%")
        print(f"  最大队列长度: {report['max_queue_length']}")
        print(f"  平均队列长度: {report['avg_queue_length']:.2f}")
        print(f"  总发送成功: {report['total_messages_sent']}")
        print(f"
相关推荐
我是一颗柠檬1 小时前
【Java项目技术亮点】分库分表+数据路由策略:单表5000万后的架构升级方案
java·开发语言·分布式·架构
wu_ye_m1 小时前
学习c语言第35天 函数声明和定义
c语言·开发语言·学习
njsgcs1 小时前
c# solidworks 创建装配体工程图+bom
开发语言·c#·solidworks
Jurio.1 小时前
开源 Codex Sticky:在终端 Codex CLI 长对话中始终固定底部输入框
linux·rust·github·开源软件·codex·codex cli
质造者1 小时前
LangChain + Ollama + Tavily 实现旅游问答系统
linux·人工智能·python·langchain·rag
小林敲代码77881 小时前
记录一下IDEA中很多变量变色的方案
java·开发语言·spring boot·idea
伊布拉西莫2 小时前
【流畅的Python】第20章:并发执行器 — 学习笔记
笔记·python·学习
njsgcs2 小时前
c# solidworks 工程图获得展开视图不在固定面螺纹特征的位置
开发语言·c#·solidworks
IT策士2 小时前
Redis 从入门到精通:Python 操作 Redis
redis·python·bootstrap