Files
postgrest/nix/tools/generate_targets_rsa.py
T
Michal KleczekandGitHub 69072116be test: Use RSA 4096 algorithm for JWT load test (#4118)
Until now we had a load test with 50k unique JWTs signed with symmetric key.
This commit adds a new load test with 10k JWTs signed with RSA 4096.

Existing -k jwt parameter was changed to -k jwt-hs-50k.
New test is run with -k jwt-rsa-10k parameter.

Additionally a new parameter --jwtcache=off was added to turn off JWT caching in the above load tests.
2025-06-05 07:44:05 -05:00

69 lines
1.9 KiB
Python

# generates a file to be used by the vegeta load testing tool
# It runs TOTAL_TARGETS amount of requests with
# TOTAL_JWTS amount of JWTs randomly spread among them
import time
import argparse
import sys
import random
import jwt
import jwcrypto.jwk as jwk
URL = "http://postgrest"
TOTAL_JWTS = 1000
TOTAL_TARGETS = 50000 # tuned by hand to reduce result variance
key = jwk.JWK.generate(kty="RSA", size=4096)
private_key = jwt.algorithms.RSAAlgorithm.from_jwk(key.export_private())
public_key = key.export_public()
def generate_jwt() -> str:
"""Generate an RS256 JWT"""
now = int(time.time())
payload = {
"sub": f"user_{random.getrandbits(32)}",
"iat": now,
"role": "postgrest_test_author",
}
return jwt.encode(payload, private_key, "RS256")
def main():
parser = argparse.ArgumentParser(
description="Generate Vegeta targets with unique JWTs"
)
parser.add_argument(
"output",
help="Path to write the generated targets file",
)
parser.add_argument("jwk", help="Path to write the generated JWK")
args = parser.parse_args()
tokens = [generate_jwt() for _ in range(TOTAL_JWTS)]
lines = []
start_time = time.time()
for i in range(TOTAL_TARGETS):
token = random.choice(tokens)
lines.append(f"OPTIONS {URL}/authors_only")
lines.append(f"Authorization: Bearer {token}")
lines.append("") # blank line to separate requests
try:
with open(args.jwk, "w") as jwk, open(args.output, "w") as f:
jwk.write(public_key)
f.write("\n".join(lines))
except IOError as e:
print(f"Error writing to {args.output}: {e}", file=sys.stderr)
sys.exit(1)
elapsed = time.time() - start_time
print(f"Created {TOTAL_TARGETS} targets with unique JWTs", end=" ")
print(f"in {args.output} ({elapsed:.2f}s)")
if __name__ == "__main__":
main()