├── LICENSE ├── README.md ├── cloudformation ├── index.md └── jmeter_server.json └── jmeter └── test-plans └── basicHttpTest.jmx /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 ConcurrenyLabs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Export JMeter test results to CloudWatch Logs. 2 | 3 | Code from the following blog post: 4 | https://www.concurrencylabs.com/blog/publish-jmeter-test-results-to-cloudwatch-logs/ 5 | 6 | 7 | This repo contains the following: 8 | 9 | * CloudFormation template for publishing JMeter test results to CloudWatch Logs 10 | * Sample JMeter test plan to quickly validate that the stack was created properly and JMeter 11 | test results are ultimately available as CloudWatch metrics. 12 | 13 | 14 | ### jmeter_server.json 15 | This template performs the following tasks: 16 | * Launches an EC2 instance with JMeter and the Flexible File Writer plugin installed. 17 | * Installs the CloudWatch Logs agent and it creates metric filters for converting log entries 18 | into CloudWatch metrics. 19 | * The EC2 instance is launched with an EC2 Instance Profile, which gives permissions to the 20 | the CloudWatch Logs agent for publishing log data to CLoudWatch Logs. 21 | * This template also downloads a sample JMeter test plan (included in this repo) that sends HTTP requests 22 | to an S3 endpoint. This is only intended for validation purposes. 23 | 24 | JMeter is installed in the following directory: 25 | 26 | ``` 27 | /home/ec2-user/jmeter/apache-jmeter-2.13 28 | ``` 29 | 30 | The JMeter test plan is downloaded to the following location: 31 | 32 | ``` 33 | /home/ec2-user/jmeter/test-plans/basicHttpTest.jmx 34 | ``` 35 | 36 | The CloudWatch Logs agent expects the JMeter test result log in the following location: 37 | 38 | ``` 39 | /home/ec2-user/jmeter/test-results/results.log 40 | ``` 41 | 42 | To start the JMeter test just SSH to the EC2 instance launched by this template and execute: 43 | 44 | ``` 45 | nohup /home/ec2-user/jmeter/apache-jmeter-2.13/bin/jmeter -n -t /home/ec2-user/jmeter/test-plans/basicHttpTest.jmx & 46 | ``` 47 | 48 | Once you run the JMeter test, you should be able to see test results flowing into CloudWatch Logs 49 | almost immediately. 50 | 51 | To stop the test, run the following command from the JMeter EC2 instance: 52 | 53 | ``` 54 | /home/ec2-user/jmeter/apache-jmeter-2.13/bin/shutdown.sh 55 | ``` 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /cloudformation/index.md: -------------------------------------------------------------------------------- 1 | CloudFormation template for launching an EC2 instance with JMeter and the CloudWatch Logs 2 | agent installed in it. This template configures the instance so it publishes JMeter test results to 3 | CloudWatch Logs. This template also creates metric filter that convert log entries into 4 | CloudWatch metrics. 5 | 6 | -------------------------------------------------------------------------------- /cloudformation/jmeter_server.json: -------------------------------------------------------------------------------- 1 | { 2 | "AWSTemplateFormatVersion": "2010-09-09", 3 | "Description": "AWS CloudFormation Template for launching a JMeter server with the Flexible File Writer plugin and the CloudWatch Logs Agent installed in it. There is no need to configure the CloudWatch Logs agent with your own credentials due to this template creating an IAM role and an EC2 instance profile", 4 | "Parameters": { 5 | "MetricsNamespace" : { 6 | "Type" : "String", 7 | "Default" : "jmeter", 8 | "Description" : "Name of the namespace for your CloudWatch metrics. Your metrics will be organized according to this namespace, for example: namespace/metric1, namespace/metric2, etc." 9 | }, 10 | "KeyName": { 11 | "Description": "Name of an existing EC2 KeyPair to enable SSH access to the instances", 12 | "Type": "AWS::EC2::KeyPair::KeyName", 13 | "ConstraintDescription": "must be the name of an existing EC2 KeyPair." 14 | } 15 | }, 16 | "Mappings": { 17 | "RegionMap": { 18 | "us-east-1": { 19 | "AMI": "ami-af7359c5" 20 | } 21 | } 22 | }, 23 | "Resources": { 24 | "LogRole": { 25 | "Type": "AWS::IAM::Role", 26 | "Properties": { 27 | "AssumeRolePolicyDocument": { 28 | "Version": "2012-10-17", 29 | "Statement": [{ 30 | "Effect": "Allow", 31 | "Principal": { 32 | "Service": [ 33 | "ec2.amazonaws.com" 34 | ] 35 | }, 36 | "Action": [ 37 | "sts:AssumeRole" 38 | ] 39 | }] 40 | }, 41 | "Path": "/", 42 | "Policies": [{ 43 | "PolicyName": "LogRolePolicy", 44 | "PolicyDocument": { 45 | "Version": "2012-10-17", 46 | "Statement": [{ 47 | "Effect": "Allow", 48 | "Action": [ 49 | "logs:Create*", 50 | "logs:PutLogEvents" 51 | ], 52 | "Resource": [ 53 | "arn:aws:logs:*:*:*" 54 | ] 55 | }] 56 | } 57 | }] 58 | } 59 | }, 60 | "LogRoleInstanceProfile": { 61 | "Type": "AWS::IAM::InstanceProfile", 62 | "Properties": { 63 | "Path": "/", 64 | "Roles": [{ 65 | "Ref": "LogRole" 66 | }] 67 | } 68 | }, 69 | "JMeterServerSecurityGroup": { 70 | "Type": "AWS::EC2::SecurityGroup", 71 | "Properties": { 72 | "GroupDescription": "Enable HTTP access via port 80 and SSH access via port 22", 73 | "SecurityGroupIngress": [{ 74 | "IpProtocol": "tcp", 75 | "FromPort": "80", 76 | "ToPort": "80", 77 | "CidrIp": "0.0.0.0/0" 78 | }, { 79 | "IpProtocol": "tcp", 80 | "FromPort": "22", 81 | "ToPort": "22", 82 | "CidrIp": "0.0.0.0/0" 83 | }] 84 | } 85 | }, 86 | "JMeterLogGroup": { 87 | "Type": "AWS::Logs::LogGroup", 88 | "Properties": { 89 | "RetentionInDays": 7 90 | } 91 | }, 92 | "JMeterServer": { 93 | "Type": "AWS::EC2::Instance", 94 | "Metadata": { 95 | "Comment": "Install JMeter 2.13", 96 | "AWS::CloudFormation::Init": { 97 | "configSets" : { 98 | "install_all" : [ "install_cfn", "install_logs" ] 99 | }, 100 | 101 | "install_cfn" : { 102 | "files" : { 103 | "/etc/cfn/cfn-hup.conf" : { 104 | "content" : { "Fn::Join" : ["", [ 105 | "[main]\n", 106 | "stack=", { "Ref" : "AWS::StackId" }, "\n", 107 | "region=", { "Ref" : "AWS::Region" }, "\n" 108 | ]]}, 109 | "mode" : "000400", 110 | "owner" : "root", 111 | "group" : "root" 112 | }, 113 | 114 | "/etc/cfn/hooks.d/cfn-auto-reloader.conf" : { 115 | "content": { "Fn::Join" : ["", [ 116 | "[cfn-auto-reloader-hook]\n", 117 | "triggers=post.update\n", 118 | "path=Resources.JMeterServer.Metadata.AWS::CloudFormation::Init\n", 119 | "action=/opt/aws/bin/cfn-init -v ", 120 | " --stack ", { "Ref" : "AWS::StackName" }, 121 | " --resource JMeterServer ", 122 | " --configsets install_all ", 123 | " --region ", { "Ref" : "AWS::Region" }, "\n", 124 | "runas=root\n" 125 | ]]} 126 | } 127 | }, 128 | "services" : { 129 | "sysvinit" : { 130 | "cfn-hup" : { "enabled" : "true", "ensureRunning" : "true", 131 | "files" : ["/etc/cfn/cfn-hup.conf", "/etc/cfn/hooks.d/cfn-auto-reloader.conf"]} 132 | } 133 | } 134 | }, 135 | 136 | "install_logs": { 137 | "packages" : { 138 | "yum" : { 139 | "awslogs" : [] 140 | } 141 | }, 142 | "files": { 143 | "/etc/awslogs/awslogs.conf": { 144 | "content": { 145 | "Fn::Join": [ 146 | "", [ 147 | "[general]\n", 148 | "state_file= /var/awslogs/agent-state\n", 149 | "[results.log]\n", 150 | "file = /home/ec2-user/jmeter/test-results/results.log\n", 151 | "log_group_name = ", { 152 | "Ref": "JMeterLogGroup" 153 | }, "\n", 154 | "log_stream_name = jmeter-server\n", 155 | "datetime_format = %Y/%m/%d %H:%M:%S.%f %Z" 156 | ] 157 | ] 158 | }, 159 | "mode": "000400", 160 | "owner": "root", 161 | "group": "root" 162 | }, 163 | "/etc/awslogs/awscli.conf": { 164 | "content": { "Fn::Join": [ "", [ 165 | "[plugins]\n", 166 | "cwlogs = cwlogs\n", 167 | "[default]\n", 168 | "region = ", { "Ref" : "AWS::Region" }, "\n" 169 | ] ] }, 170 | "mode": "000444", 171 | "owner": "root", 172 | "group": "root" 173 | } 174 | }, 175 | "commands" : { 176 | "01_create_state_directory" : { 177 | "command" : "mkdir -p /var/awslogs/state" 178 | } 179 | }, 180 | "services" : { 181 | "sysvinit" : { 182 | "awslogs" : { "enabled" : "true", "ensureRunning" : "true", 183 | "files" : [ "/etc/awslogs/awslogs.conf" ] } 184 | } 185 | } 186 | } 187 | } 188 | }, 189 | "Properties": { 190 | "ImageId": {"Fn::FindInMap": ["RegionMap", {"Ref": "AWS::Region"},"AMI"]}, 191 | "KeyName": { 192 | "Ref": "KeyName" 193 | }, 194 | "InstanceType": "t2.nano", 195 | "SecurityGroups": [{ 196 | "Ref": "JMeterServerSecurityGroup" 197 | }], 198 | "Tags": [ 199 | { 200 | "Value": "jmeter-server", 201 | "Key": "Name" 202 | } 203 | ], 204 | "IamInstanceProfile": { 205 | "Ref": "LogRoleInstanceProfile" 206 | }, 207 | "UserData": { 208 | "Fn::Base64": { 209 | "Fn::Join": [ 210 | "", [ 211 | "#!/bin/bash -xe\n", 212 | "yum update -y aws-cfn-bootstrap\n", 213 | 214 | "/opt/aws/bin/cfn-init -v ", 215 | " --stack ", { "Ref" : "AWS::StackName" }, 216 | " --resource JMeterServer ", 217 | " --configsets install_all ", 218 | " --region ", { "Ref" : "AWS::Region" }, "\n", 219 | 220 | "# Get the sample JMeter test plan\n", 221 | "wget https://raw.githubusercontent.com/ConcurrenyLabs/jmeter-cw-logs/master/jmeter/test-plans/basicHttpTest.jmx -O /home/ec2-user/jmeter/test-plans/basicHttpTest.jmx\n", 222 | "chown ec2-user:ec2-user /home/ec2-user/jmeter/test-plans/basicHttpTest.jmx\n", 223 | 224 | "/opt/aws/bin/cfn-signal -e $? ", 225 | " --stack ", { "Ref" : "AWS::StackName" }, 226 | " --resource JMeterServer ", 227 | " --region ", { "Ref" : "AWS::Region" }, "\n" 228 | ] 229 | ] 230 | } 231 | } 232 | }, 233 | "CreationPolicy" : { 234 | "ResourceSignal" : { 235 | "Timeout" : "PT15M" 236 | } 237 | } 238 | }, 239 | "Requests":{ 240 | "Type": "AWS::Logs::MetricFilter", 241 | "Properties": { 242 | "FilterPattern": "[]", 243 | "LogGroupName": { "Ref" : "JMeterLogGroup" }, 244 | "MetricTransformations": [ 245 | { 246 | "MetricValue": "1", 247 | "MetricNamespace": {"Ref":"MetricsNamespace"}, 248 | "MetricName": "AllRequests" 249 | } 250 | ] 251 | } 252 | }, 253 | "HTTP200Responses":{ 254 | "Type": "AWS::Logs::MetricFilter", 255 | "Properties": { 256 | "FilterPattern": "[...,responseCode=200,responseMessage,isSuccsessful]", 257 | "LogGroupName": { "Ref" : "JMeterLogGroup" }, 258 | "MetricTransformations": [ 259 | { 260 | "MetricValue": "1", 261 | "MetricNamespace": {"Ref":"MetricsNamespace"}, 262 | "MetricName": "AllHTTP_200" 263 | } 264 | ] 265 | } 266 | }, 267 | "HTTP400Responses":{ 268 | "Type": "AWS::Logs::MetricFilter", 269 | "Properties": { 270 | "FilterPattern": "[...,responseCode=400,responseMessage,isSuccsessful]", 271 | "LogGroupName": { "Ref" : "JMeterLogGroup" }, 272 | "MetricTransformations": [ 273 | { 274 | "MetricValue": "1", 275 | "MetricNamespace": {"Ref":"MetricsNamespace"}, 276 | "MetricName": "AllHTTP_400" 277 | } 278 | ] 279 | } 280 | }, 281 | "HTTP500Responses":{ 282 | "Type": "AWS::Logs::MetricFilter", 283 | "Properties": { 284 | "FilterPattern": "[...,responseCode=500,responseMessage,isSuccsessful]", 285 | "LogGroupName": { "Ref" : "JMeterLogGroup" }, 286 | "MetricTransformations": [ 287 | { 288 | "MetricValue": "1", 289 | "MetricNamespace": {"Ref":"MetricsNamespace"}, 290 | "MetricName": "AllHTTP_500" 291 | } 292 | ] 293 | } 294 | }, 295 | "ResponseTimeMs":{ 296 | "Type": "AWS::Logs::MetricFilter", 297 | "Properties": { 298 | "FilterPattern": "[timeStamp,sampleLabel,threadName,responseTime,...]", 299 | "LogGroupName": { "Ref" : "JMeterLogGroup" }, 300 | "MetricTransformations": [ 301 | { 302 | "MetricValue": "$responseTime", 303 | "MetricNamespace": {"Ref":"MetricsNamespace"}, 304 | "MetricName": "AllResponseTime_ms" 305 | } 306 | ] 307 | } 308 | }, 309 | "ConnectTimeMs":{ 310 | "Type": "AWS::Logs::MetricFilter", 311 | "Properties": { 312 | "FilterPattern": "[timeStamp,sampleLabel,threadName,responseTime,connectTime,...]", 313 | "LogGroupName": { "Ref" : "JMeterLogGroup" }, 314 | "MetricTransformations": [ 315 | { 316 | "MetricValue": "$connectTime", 317 | "MetricNamespace": {"Ref":"MetricsNamespace"}, 318 | "MetricName": "AllConnectTime_ms" 319 | } 320 | ] 321 | } 322 | }, 323 | "LatencyMs":{ 324 | "Type": "AWS::Logs::MetricFilter", 325 | "Properties": { 326 | "FilterPattern": "[timeStamp,sampleLabel,threadName,responseTime,connectTime,latency,...]", 327 | "LogGroupName": { "Ref" : "JMeterLogGroup" }, 328 | "MetricTransformations": [ 329 | { 330 | "MetricValue": "$latency", 331 | "MetricNamespace": {"Ref":"MetricsNamespace"}, 332 | "MetricName": "AllLatency_ms" 333 | } 334 | ] 335 | } 336 | }, 337 | "SentBytes":{ 338 | "Type": "AWS::Logs::MetricFilter", 339 | "Properties": { 340 | "FilterPattern": "[timeStamp,sampleLabel,threadName,responseTime,connectTime,latency,sentBytes,...]", 341 | "LogGroupName": { "Ref" : "JMeterLogGroup" }, 342 | "MetricTransformations": [ 343 | { 344 | "MetricValue": "$sentBytes", 345 | "MetricNamespace": {"Ref":"MetricsNamespace"}, 346 | "MetricName": "AllSentBytes" 347 | } 348 | ] 349 | } 350 | }, 351 | "ReceivedBytes":{ 352 | "Type": "AWS::Logs::MetricFilter", 353 | "Properties": { 354 | "FilterPattern": "[timeStamp,sampleLabel,threadName,responseTime,connectTime,latency,sentBytes,receivedBytes,...]", 355 | "LogGroupName": { "Ref" : "JMeterLogGroup" }, 356 | "MetricTransformations": [ 357 | { 358 | "MetricValue": "$receivedBytes", 359 | "MetricNamespace": {"Ref":"MetricsNamespace"}, 360 | "MetricName": "AllReceivedBytes" 361 | } 362 | ] 363 | } 364 | } 365 | }, 366 | "Outputs": { 367 | "JMeterServerInstanceId": { 368 | "Description": "The instance ID of the JMeter server", 369 | "Value": { 370 | "Ref": "JMeterServer" 371 | } 372 | }, 373 | "PublicIP": { 374 | "Description": "Public IP address of the JMeter server", 375 | "Value": { 376 | "Fn::GetAtt": [ 377 | "JMeterServer", 378 | "PublicIp" 379 | ] 380 | } 381 | }, 382 | "CloudWatchLogGroupName": { 383 | "Description": "The name of the CloudWatch log group", 384 | "Value": { 385 | "Ref": "JMeterLogGroup" 386 | } 387 | } 388 | } 389 | } 390 | -------------------------------------------------------------------------------- /jmeter/test-plans/basicHttpTest.jmx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | false 7 | false 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | continue 16 | 17 | false 18 | -1 19 | 20 | 10 21 | 10 22 | 1419826797000 23 | 1419826797000 24 | false 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | concurrencylabs-dummyendpoint.s3-website-us-east-1.amazonaws.com/ 34 | 35 | 36 | 37 | 38 | 39 | / 40 | GET 41 | false 42 | false 43 | true 44 | false 45 | false 46 | 47 | 48 | 49 | 50 | 5000 51 | 5000 52 | This timer adds a random think time of 5-10 seconds between requests. 53 | 54 | 55 | 56 | 57 | /home/ec2-user/jmeter/test-results/results.log 58 | sampleLabel| [|threadName|] [|responseTime|] [|connectTime|] [|latency|] [|sentBytes|] [|receivedBytes|] [|responseCode|] [|responseMessage|] [|isSuccsessful|]\n 59 | true 60 | timeStamp sampleLabel threadName responseTime connectTime latency sentBytes receivedBytes responseCode responseMessage isSuccsessful\n 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | --------------------------------------------------------------------------------