Skip to content

Commit

Permalink
Fix benchmark tf module with python function (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahsivjar committed Oct 2, 2023
1 parent 421496b commit de44cb8
Show file tree
Hide file tree
Showing 18 changed files with 893 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ dist/
*arn-file.md

tf/*.zip

build
2 changes: 1 addition & 1 deletion apmproxy/apmserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ func (c *Client) FlushAPMData(ctx context.Context) {
}

// Flush lambda data
c.logger.Debug("Flush in progress - Processing lambda data")
for {
select {
case apmData := <-c.LambdaDataChannel:
c.logger.Debug("Flush in progress - Processing lambda data")
if err := c.forwardLambdaData(ctx, apmData); err != nil {
c.logger.Errorf("Error sending to APM server, skipping: %v", err)
}
Expand Down
32 changes: 27 additions & 5 deletions testing/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ MACHINE_TYPE?=t2.medium
STACK_VERSION?=latest
LOAD_DURATION?=10
LOAD_ARRIVAL_RATE?=50
LAMBDA_RUNTIME?=python3.8
LAMBDA_RUNTIME?=python3.9
LAMBDA_TIMEOUT?=15
LAMBDA_MEMORY_SIZE?=128
CUSTOM_LAMBDA_EXTENSION_ARN?=
LAMBDA_RUNTIME_NORM=$(subst '.','_',$(LAMBDA_RUNTIME))

# TODO: @lahsivjar Add automation for terraform fmt and docs
LAMBDA_APM_AWS_EXTENSION_PATH=$(shell echo $(wildcard ../dist/*-linux-amd64.zip) | cut -d' ' -f1)
TERRAFORM_DOCS=../build/terraform-docs
TERRAFORM_DIRS=$(shell find tf-modules -type d -mindepth 1 -maxdepth 1)

build/$(LAMBDA_RUNTIME_NORM).zip:
@mkdir -p build
Expand All @@ -19,7 +20,7 @@ build/$(LAMBDA_RUNTIME_NORM).zip:
.PHONY: clean
clean:
@rm -rf build/
@rm -rf functions/python3.8/package/
@rm -rf functions/python3.9/package/
@rm -rf functions/go1.x/main

.PHONY: bench
Expand All @@ -30,10 +31,29 @@ bench: build/$(LAMBDA_RUNTIME_NORM).zip --run-tf
destroy: TF_CMD=destroy
destroy: --run-tf

.PHONY: fmt
fmt:
@terraform fmt -write $(TERRAFORM_DIRS)

.PHONY: lint
lint:
@terraform fmt -write=false $(TERRAFORM_DIRS)

.PHONY: terraform-init
terraform-init:
@cd benchmarking && terraform init

$(TERRAFORM_DOCS): ../tools/go.mod
@go build -o $@ -modfile=$< github.com/terraform-docs/terraform-docs

.PHONY: docs
docs: $(TERRAFORM_DOCS) $(addsuffix /README.md,$(TERRAFORM_DIRS))

%/README.md: force
$(TERRAFORM_DOCS) markdown --hide-empty --header-from header.md --output-file=README.md --output-mode replace $(subst README.md,,$@)

force: ;

.PHONY: --run-tf
--run-tf:
@cd benchmarking && terraform $(TF_CMD) -auto-approve \
Expand All @@ -46,4 +66,6 @@ terraform-init:
-var 'lambda_timeout=$(LAMBDA_TIMEOUT)' \
-var 'lambda_memory_size=$(LAMBDA_MEMORY_SIZE)' \
-var 'stack_version=$(STACK_VERSION)' \
-var 'custom_lambda_extension_arn=$(CUSTOM_LAMBDA_EXTENSION_ARN)'
-var 'custom_lambda_extension_arn=$(CUSTOM_LAMBDA_EXTENSION_ARN)' \
-var 'lambda_apm_aws_extension_path=../$(LAMBDA_APM_AWS_EXTENSION_PATH)'

4 changes: 2 additions & 2 deletions testing/benchmarking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ No resources.
| <a name="input_elasticsearch_zone_count"></a> [elasticsearch\_zone\_count](#input\_elasticsearch\_zone\_count) | Optional Elasticsearch zone count | `number` | `2` | no |
| <a name="input_ess_region"></a> [ess\_region](#input\_ess\_region) | Optional ESS region where the deployment will be created. Defaults to gcp-us-west2 | `string` | `"gcp-us-west2"` | no |
| <a name="input_lambda_memory_size"></a> [lambda\_memory\_size](#input\_lambda\_memory\_size) | Amount of memory (in MB) the lambda function can use | `number` | `128` | no |
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | The language-specific lambda runtime | `string` | `"python3.8"` | no |
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | The language-specific lambda runtime | `string` | `"python3.9"` | no |
| <a name="input_lambda_timeout"></a> [lambda\_timeout](#input\_lambda\_timeout) | Timeout of the lambda function in seconds | `number` | `15` | no |
| <a name="input_load_arrival_rate"></a> [load\_arrival\_rate](#input\_load\_arrival\_rate) | Rate(per second) at which the virtual users are generated | `number` | `50` | no |
| <a name="input_load_duration"></a> [load\_duration](#input\_load\_duration) | Duration over which to generate new virtual users | `number` | `10` | no |
Expand All @@ -46,4 +46,4 @@ No resources.
## Outputs

No outputs.
<!-- END_TF_DOCS -->
<!-- END_TF_DOCS -->
26 changes: 19 additions & 7 deletions testing/benchmarking/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
version = "~> 5.18.1"
}
null = {
source = "hashicorp/null"
Expand All @@ -21,9 +21,19 @@ locals {
name_from_runtime = replace(var.lambda_runtime, ".", "_")
lambda_function_zip = "../build/${local.name_from_runtime}.zip"
lambda_function_name = "${var.resource_prefix}_${local.name_from_runtime}_apm_aws_lambda"
runtimeToHandler = {
"python3.8" = "main.handler"
"go1.x" = "main"
runtimeVars = {
"python3.9" = {
"handler" = "main.handler"
"layers" = ["arn:aws:lambda:${var.aws_region}:267093732750:layer:elastic-apm-python-ver-6-18-0:1"]
"envvars" = {
"AWS_LAMBDA_EXEC_WRAPPER" = "/opt/python/bin/elasticapm-lambda"
}
}
"go1.x" = {
"handler" = "main"
"layers" = []
"envvars" = {}
}
}
}

Expand Down Expand Up @@ -65,14 +75,16 @@ module "lambda_deployment" {

resource_prefix = var.resource_prefix

apm_aws_extension_path = "../../bin/extension.zip"
apm_aws_extension_path = var.lambda_apm_aws_extension_path

lambda_runtime = var.lambda_runtime
lambda_memory_size = var.lambda_memory_size
lambda_function_zip = local.lambda_function_zip
lambda_function_name = local.lambda_function_name
lambda_handler = local.runtimeToHandler[var.lambda_runtime]
lambda_invoke_path = local.load_req_path
lambda_memory_size = var.lambda_memory_size
additional_lambda_layers = local.runtimeVars[var.lambda_runtime]["layers"]
lambda_handler = local.runtimeVars[var.lambda_runtime]["handler"]
environment_variables = local.runtimeVars[var.lambda_runtime]["envvars"]
custom_lambda_extension_arn = var.custom_lambda_extension_arn

apm_server_url = module.ec_deployment.apm_url
Expand Down
7 changes: 6 additions & 1 deletion testing/benchmarking/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ variable "load_arrival_rate" {
variable "lambda_runtime" {
type = string
description = "The language-specific lambda runtime"
default = "python3.8"
default = "python3.9"
}

variable "lambda_timeout" {
Expand All @@ -45,6 +45,11 @@ variable "lambda_memory_size" {
default = 128
}

variable "lambda_apm_aws_extension_path" {
type = string
description = "Extension path where apm-aws-lambda extension zip is created"
}

variable "custom_lambda_extension_arn" {
type = string
description = "Specific lambda extension to use, will use the latest build if not specified"
Expand Down
1 change: 0 additions & 1 deletion testing/functions/python3.8/requirements.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@

set -e

pip install -t ./package -r requirements.txt
(cd package && zip -r ../../../build/python3_8.zip .)
zip -g ../../build/python3_8.zip main.py
zip -g ../../build/python3_9.zip main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import json
from elasticapm import capture_serverless

coldstart = "true"
@capture_serverless()
def handler(event, context):
global coldstart
print("Example function log", context.aws_request_id)
Expand Down
26 changes: 5 additions & 21 deletions testing/tf-modules/artillery_deployment/README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
<!-- BEGIN_TF_DOCS -->
## Terraform module for deploying artillery

The module is used to deploy and run [artillery](https://www.artillery.io/) in
cloud for benchmarking lambda functions.

<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 4.0 |
| <a name="requirement_null"></a> [null](#requirement\_null) | >=3.1.1 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.37.0 |
| <a name="provider_null"></a> [null](#provider\_null) | 3.2.0 |
| <a name="provider_tls"></a> [tls](#provider\_tls) | 4.0.4 |

## Modules

No modules.
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
| <a name="provider_null"></a> [null](#provider\_null) | n/a |
| <a name="provider_tls"></a> [tls](#provider\_tls) | n/a |

## Resources

Expand All @@ -43,15 +32,10 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | AWS region to deploy lambda function | `string` | `"us-west-2"` | no |
| <a name="input_load_arrival_rate"></a> [load\_arrival\_rate](#input\_load\_arrival\_rate) | Rate(per second) at which the virtual users are generated | `number` | `50` | no |
| <a name="input_load_base_url"></a> [load\_base\_url](#input\_load\_base\_url) | Base URL for load generation | `string` | n/a | yes |
| <a name="input_load_duration"></a> [load\_duration](#input\_load\_duration) | Duration over which to generate new virtual users | `number` | `10` | no |
| <a name="input_load_req_path"></a> [load\_req\_path](#input\_load\_req\_path) | Request path for load generation | `string` | n/a | yes |
| <a name="input_machine_type"></a> [machine\_type](#input\_machine\_type) | Machine type for artillery nodes | `string` | `"t2.medium"` | no |
| <a name="input_resource_prefix"></a> [resource\_prefix](#input\_resource\_prefix) | Prefix to add to all created resource | `string` | n/a | yes |

## Outputs

No outputs.
<!-- END_TF_DOCS -->
<!-- END_TF_DOCS -->
5 changes: 5 additions & 0 deletions testing/tf-modules/artillery_deployment/header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Terraform module for deploying artillery

The module is used to deploy and run [artillery](https://www.artillery.io/) in
cloud for benchmarking lambda functions.

22 changes: 9 additions & 13 deletions testing/tf-modules/lambda_deployment/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
<!-- BEGIN_TF_DOCS -->
## Terraform module to deploy Lambda functions on cloud

The module is used to deploy lambda functions for supported runtimes on cloud
for the purpose of benchmarking. The lambda function is configured to be
triggered with an no auth API gateway endpoint.


<!-- BEGIN_TF_DOCS -->
## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |

## Modules

No modules.

## Resources

| Name | Type |
Expand All @@ -28,7 +19,9 @@ No modules.
| [aws_apigatewayv2_integration.trigger](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_integration) | resource |
| [aws_apigatewayv2_route.trigger](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_route) | resource |
| [aws_apigatewayv2_stage.trigger](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_stage) | resource |
| [aws_cloudwatch_log_group.cw_log_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
| [aws_iam_role.iam_for_lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy_attachment.cw](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_lambda_function.test_fn](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource |
| [aws_lambda_layer_version.extn_layer](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_layer_version) | resource |
| [aws_lambda_permission.trigger](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
Expand All @@ -37,15 +30,18 @@ No modules.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_additional_lambda_layers"></a> [additional\_lambda\_layers](#input\_additional\_lambda\_layers) | Additional lambda layer ARNs to add to the lambda function | `list(string)` | `[]` | no |
| <a name="input_apm_aws_extension_path"></a> [apm\_aws\_extension\_path](#input\_apm\_aws\_extension\_path) | Path to the zip file containing extension code | `string` | n/a | yes |
| <a name="input_apm_secret_token"></a> [apm\_secret\_token](#input\_apm\_secret\_token) | Secret token for auth against the given server URL | `string` | n/a | yes |
| <a name="input_apm_server_url"></a> [apm\_server\_url](#input\_apm\_server\_url) | APM Server URL for sending the generated load | `string` | n/a | yes |
| <a name="input_build_dir"></a> [build\_dir](#input\_build\_dir) | Prefix to add to all created resource | `string` | n/a | yes |
| <a name="input_custom_lambda_extension_arn"></a> [custom\_lambda\_extension\_arn](#input\_custom\_lambda\_extension\_arn) | Specific lambda extension to use, will use the latest build if not specified | `string` | `""` | no |
| <a name="input_environment_variables"></a> [environment\_variables](#input\_environment\_variables) | Additional environment variables to add to the lambda function | `map(string)` | `{}` | no |
| <a name="input_lambda_function_name"></a> [lambda\_function\_name](#input\_lambda\_function\_name) | The name of the lambda function | `string` | n/a | yes |
| <a name="input_lambda_function_zip"></a> [lambda\_function\_zip](#input\_lambda\_function\_zip) | Path to the zip package containing the lambda function to deploy | `string` | n/a | yes |
| <a name="input_lambda_handler"></a> [lambda\_handler](#input\_lambda\_handler) | Entrypoint for the lambda function | `string` | `"main.handler"` | no |
| <a name="input_lambda_invoke_path"></a> [lambda\_invoke\_path](#input\_lambda\_invoke\_path) | Request path to invoke the test lambda function | `string` | `"/test"` | no |
| <a name="input_lambda_memory_size"></a> [lambda\_memory\_size](#input\_lambda\_memory\_size) | Amount of memory (in MB) the lambda function can use | `number` | `128` | no |
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | The language-specific lambda runtime | `string` | `"python3.8"` | no |
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | The language-specific lambda runtime | `string` | `"python3.9"` | no |
| <a name="input_lambda_timeout"></a> [lambda\_timeout](#input\_lambda\_timeout) | Timeout of the lambda function in seconds | `number` | `15` | no |
| <a name="input_resource_prefix"></a> [resource\_prefix](#input\_resource\_prefix) | Prefix to add to all created resource | `string` | n/a | yes |

Expand All @@ -55,4 +51,4 @@ No modules.
|------|-------------|
| <a name="output_base_url"></a> [base\_url](#output\_base\_url) | Base URL for triggering the test lambda function |
| <a name="output_trigger_url"></a> [trigger\_url](#output\_trigger\_url) | URL for triggering the test lambda function |
<!-- END_TF_DOCS -->
<!-- END_TF_DOCS -->
6 changes: 6 additions & 0 deletions testing/tf-modules/lambda_deployment/header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Terraform module to deploy Lambda functions on cloud

The module is used to deploy lambda functions for supported runtimes on cloud
for the purpose of benchmarking. The lambda function is configured to be
triggered with an no auth API gateway endpoint.

21 changes: 13 additions & 8 deletions testing/tf-modules/lambda_deployment/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,26 @@ resource "aws_lambda_function" "test_fn" {
handler = var.lambda_handler
runtime = var.lambda_runtime
source_code_hash = filebase64sha256(var.lambda_function_zip)
layers = [var.custom_lambda_extension_arn == "" ? aws_lambda_layer_version.extn_layer[0].arn : var.custom_lambda_extension_arn]
timeout = var.lambda_timeout
memory_size = var.lambda_memory_size
timeout = var.lambda_timeout
memory_size = var.lambda_memory_size

depends_on = [
aws_cloudwatch_log_group.cw_log_group,
]
layers = concat(
[var.custom_lambda_extension_arn == "" ? aws_lambda_layer_version.extn_layer[0].arn : var.custom_lambda_extension_arn],
var.additional_lambda_layers,
)

environment {
variables = {
variables = merge({
ELASTIC_APM_LAMBDA_APM_SERVER = var.apm_server_url
ELASTIC_APM_SECRET_TOKEN = var.apm_secret_token
ELASTIC_APM_LAMBDA_CAPTURE_LOGS = "true"
}
ELASTIC_APM_SEND_STRATEGY = "background"
}, var.environment_variables)
}

depends_on = [
aws_cloudwatch_log_group.cw_log_group,
]
}

resource "aws_apigatewayv2_api" "trigger" {
Expand Down
14 changes: 13 additions & 1 deletion testing/tf-modules/lambda_deployment/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ variable "lambda_function_name" {
variable "lambda_runtime" {
type = string
description = "The language-specific lambda runtime"
default = "python3.8"
default = "python3.9"
}

variable "lambda_handler" {
Expand Down Expand Up @@ -54,6 +54,18 @@ variable "custom_lambda_extension_arn" {
default = ""
}

variable "additional_lambda_layers" {
type = list(string)
description = "Additional lambda layer ARNs to add to the lambda function"
default = []
}

variable "environment_variables" {
type = map(string)
description = "Additional environment variables to add to the lambda function"
default = {}
}

variable "apm_server_url" {
type = string
description = "APM Server URL for sending the generated load"
Expand Down
Loading

0 comments on commit de44cb8

Please sign in to comment.