Skip to content

Instantly share code, notes, and snippets.

View sofianhamiti's full-sized avatar

Sofian Hamiti sofianhamiti

View GitHub Profile
try:
logging.info('LOAD TESTING THE API')
stats = run_load_test(host=api.api_endpoint)
# get response time percentiles
response_time_percentile = stats['requests']['POST_/']['response_time_percentiles'][95]
logging.info(f'REPONSE TIME PERCENTILES: {response_time_percentile}')
logging.info(f'LAMBDA MEMORY: {args.lambda_memory}')
# we create this aggregate score to optimize both latency and lambda memory allocation (cost)
import os
import json
import logging
import argparse
import invokust
from load_test.api_user import ApiUser
from stack.api_gateway import ApiGateway
from stack.lambda_function import LambdaFunction
logging.basicConfig(level=logging.INFO)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import boto3
class LambdaFunction:
def __init__(self, name, container, model_s3_uri, memory, role, region):
self.name = name
self.container = container
self.model_s3_uri = model_s3_uri
self.memory = memory
self.role = role
import invokust
from load_test.api_user import ApiUser
def run_load_test(host):
settings = invokust.create_settings(
classes=[ApiUser],
host=host,
num_users=1000,
spawn_rate=100,
run_time='1m'
from locust.contrib.fasthttp import FastHttpUser
from locust import between, task
class ApiUser(FastHttpUser):
wait_time = between(1, 3)
@task()
def predict_lambda(self):
payload = {
FROM public.ecr.aws/lambda/python:3.8
# Install dependencies
COPY requirements.txt /tmp/
RUN pip3 install -r /tmp/requirements.txt --no-cache
# Copy inference code
COPY predict.py utils.py ${LAMBDA_TASK_ROOT}/
import os
import json
import xgboost
import pandas as pd
import pickle as pkl
from utils import extract_model
# download model file from S3 into /tmp folder
extract_model(os.environ['MODEL_S3_URI'], '/tmp')
# LOAD MODEL
version: 0.2
phases:
install:
runtime-versions:
python: 3.8
commands:
- npm install -g [email protected]
- pip install --upgrade --force-reinstall botocore boto3 awscli
import os
import json
import boto3
import logging
import argparse
import requests
logger = logging.getLogger(__name__)
api_client = boto3.client('apigatewayv2')