sless-primer/POSTGRES/code/stress-writer/stress_writer.py
Naeel 4b04cde84b chore(examples): remove stale examples, keep only POSTGRES
- Deleted: TNAR, demo-event-log, demo-managed-functions, hello-go, hello-node,
  k8s, notes-python, pg-list-python, simple-node, simple-python
- POSTGRES: removed luceUNDnode.tf (commented-out legacy), stress_log_1.txt,
  funcs_list.py; disabled stress_destroy_apply.sh (PG lifecycle stress has
  delete_user bug); added README.md
- examples/README.md: updated to reflect current state (sless_service + sless_job)
2026-03-21 07:49:23 +03:00

40 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 2026-03-19
# stress_writer.py — пишет N строк в terraform_demo_table (по умолчанию 5).
# Проверяет параллельные INSERT'ы и устойчивость соединения с PG при нагрузке.
import os
import psycopg2
import time
_VERSION = "v1"
def run(event):
n = int(event.get("rows", 5))
prefix = event.get("prefix", "stress")
conn = psycopg2.connect(
host=os.environ["PGHOST"],
port=int(os.environ.get("PGPORT", "5432")),
dbname=os.environ["PGDATABASE"],
user=os.environ["PGUSER"],
password=os.environ["PGPASSWORD"],
sslmode=os.environ.get("PGSSLMODE", "require"),
)
inserted = []
try:
with conn.cursor() as cur:
for i in range(n):
title = f"{prefix}-{int(time.time()*1000)}-{i}"
cur.execute(
"INSERT INTO terraform_demo_table (title) VALUES (%s) RETURNING id",
(title,),
)
row = cur.fetchone()
inserted.append({"id": row[0], "title": title})
conn.commit()
finally:
conn.close()
return {"version": _VERSION, "inserted": inserted, "count": len(inserted)}