Files
dashboard-leads/backend/medir_tiempos.py
2026-08-18 11:48:08 -05:00

71 lines
2.4 KiB
Python

# medir_tiempos.py
# Mide cuánto tarda CADA parte que carga el dashboard de Leads al iniciar.
# Simula lo mismo que hace el backend en su precarga, pero cronometrando cada paso.
# Ejecutar en backend/: python medir_tiempos.py
import time
import services as S
def cron(nombre, fn):
t0 = time.perf_counter()
ok = "OK"
extra = ""
try:
r = fn()
try:
if isinstance(r, (list, dict)):
extra = f"({len(r)} items)"
except Exception:
pass
except Exception as e:
ok = "ERROR"
extra = str(e)[:60]
dt = time.perf_counter() - t0
print(f" {nombre:38} {dt:7.2f}s {ok} {extra}")
return dt
periodos = S._periodos_actual_anterior()
a0, m0 = periodos[0]
print("\n" + "="*70)
print(" MEDICIÓN DE TIEMPOS DE CARGA — Dashboard LEADS")
print("="*70)
print("\n[1] INSUMOS CRUDOS (consultas base, se cargan 1 vez)")
tot1 = 0
tot1 += cron("Leads (Chatwoot/PostgreSQL)", S._leads_crudos)
tot1 += cron("Cursos (SQL Server)", S._cursos_crudos)
tot1 += cron("Matriculas (SQL Server)", S._matriculas_crudas)
tot1 += cron("Leads asignados/respuesta", S._leads_asignados_crudos)
tot1 += cron("Cartera (Supabase)", S._cartera_rows_cache)
tot1 += cron("Plantillas", S._plantillas_crudas)
print(f"\n --> Subtotal insumos: {tot1:.2f}s")
print(f"\n[2] APARTADO LEADS (periodo {a0}-{m0}, filtros TODOS)")
tot2 = 0
tot2 += cron("leads_dashboard", lambda: S.leads_dashboard(a0, m0, "TODOS", "TODOS", "TODOS"))
tot2 += cron("leads_filtros", S.opciones_filtros)
print(f"\n --> Subtotal Leads: {tot2:.2f}s")
print(f"\n[3] APARTADO VENDEDORES (periodo {a0}-{m0})")
tot3 = 0
tot3 += cron("vendedores_dashboard", lambda: S.vendedores_dashboard(a0, m0, "TODOS"))
print(f"\n --> Subtotal Vendedores: {tot3:.2f}s")
print(f"\n[4] APARTADO OTROS GENERAL (periodo {a0}-{m0}) — el mas pesado")
tot4 = 0
tot4 += cron("otros_general", lambda: S.otros_general(a0, m0, "TODOS"))
print(f"\n --> Subtotal Otros General: {tot4:.2f}s")
print(f"\n[5] APARTADO ROAS (periodo {a0}-{m0})")
tot5 = 0
tot5 += cron("roas_dashboard", lambda: S.roas_dashboard(a0, m0, "TODOS"))
print(f"\n --> Subtotal ROAS: {tot5:.2f}s")
total = tot1 + tot2 + tot3 + tot4 + tot5
print("\n" + "="*70)
print(f" TIEMPO TOTAL (todo en frio, 1a vez): {total:.2f}s")
print(" Nota: en el arranque real, [1]+[2] van en un hilo y [4] en otro,")
print(" asi que corren EN PARALELO. El tiempo real es ~ el mas lento de los dos.")
print("="*70 + "\n")