├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── project.clj └── src └── clojider ├── aws.clj ├── core.clj ├── examples.clj ├── lambda.clj └── rc.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | config.edn 13 | tmp/ 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: clojure 2 | script: lein compile 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # clojider 2 | 3 | [![Build Status](https://travis-ci.org/mhjort/clojider.svg?branch=master)](https://travis-ci.org/mhjort/clojider) 4 | 5 | ![alt text](https://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Atlas_November_2005.jpg/220px-Atlas_November_2005.jpg "Large Hadron Collider") 6 | 7 | Run clj-gatling load tests on your local machine or by utilizing AWS Lambda technology. 8 | 9 | Note! This is an experimental alpha stage tool/library. 10 | 11 | ## Features 12 | 13 | * Test your system using realistic scenarios. Not just one url. 14 | * Write test scenarios in Clojure. No special DSL. 15 | * Run your tests either from local machine or using multiple nodes using AWS Lambda technology. 16 | 17 | ## Installation 18 | 19 | ### Basic setup 20 | 21 | Create new Clojure project & add the following to your `project.clj` `:dependencies`: 22 | 23 | ```clojure 24 | [clojider "0.5.1"] 25 | ``` 26 | 27 | Add this setting to your `project.clj` 28 | 29 | ```clojure 30 | :main clojider.core 31 | ``` 32 | 33 | ### AWS Lambda setup 34 | 35 | Note! Clojider has to setup one S3 bucket, IAM role & policy and Lambda function using your AWS credentials. S3 bucket name you have to configure, other resources will be auto-named. 36 | The credentials are read from standard environment variables or configuration file. See details from [here](http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/set-up-creds.html). 37 | 38 | Add this setting to your `project.clj`. This is required because report generation uses Gatling report generation [module](https://github.com/gatling/gatling-highcharts) which is Scala code. 39 | Report generation happens in local machine and this setting prevents Scala code to be included in to Jar file that is deployed to AWS Lambda environment. 40 | 41 | ```clojure 42 | :uberjar-exclusions [#"scala.*"] 43 | ``` 44 | 45 | Deploy your project to AWS Lambda. 46 | Note! Lambda is available in these regions: eu-west-1, us-east-1, us-west-2 and ap-northeast-1. 47 | 48 | ```sh 49 | lein uberjar 50 | lein run install -r -b -f target/ 51 | ``` 52 | 53 | ## Writing tests 54 | 55 | You can find few simple examples [here](https://github.com/mhjort/clojider/blob/master/src/clojider/examples.clj) 56 | which you run locally in a following way. 57 | 58 | ```sh 59 | lein run load-local -c 5 -d 10 -b -s clojider.examples/ping-simulation 60 | ``` 61 | or 62 | 63 | ```sh 64 | lein run load-local -c 5 -d 10 -b -s clojider.examples/metrics-simulation 65 | ``` 66 | 67 | See [clj-gatling](https://github.com/mhjort/clj-gatling) on how to define test scenarios. 68 | 69 | ## Running tests 70 | 71 | ### Locally 72 | 73 | ```sh 74 | lein run load-local -c -d -s 75 | ``` 76 | 77 | ### Using AWS Lambda 78 | 79 | ```sh 80 | lein run load-lambda -r -b -c -d -s 81 | ``` 82 | 83 | And when you have updated your simulation (the scenario code), you have to update latest code to Lambda via 84 | 85 | ```sh 86 | lein uberjar 87 | lein run update -r -b -f target/ 88 | ``` 89 | 90 | ### Optional parameters 91 | 92 | * `-t` or `--timeout`` specifies request timeout in milliseconds. By default it is 5000 ms. 93 | 94 | ## Uninstall 95 | 96 | This will uninstall all created AWS resources (S3 bucket, role, policy and Lambda function). 97 | The cost of keeping these available in your account for the next load testing run is almost zero. 98 | Lambda pricing is totally based on the usage and S3 bucket contains only smallish old result files. 99 | However, I still wanted to have an option to destroy everything when you don't need the tool anymore. 100 | 101 | ```sh 102 | lein run uninstall -r -b 103 | ``` 104 | 105 | 106 | 107 | ## Contribute 108 | 109 | Use [GitHub issues](https://github.com/mhjort/clojider/issues) and [Pull Requests](https://github.com/mhjort/clojider/pulls). 110 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject clojider "0.5.1" 2 | :description "AWS Lambda powered, distributed load testing tool for Clojure" 3 | :url "https://github.com/mhjort/clojider" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.10.3"] 7 | [cheshire "5.5.0"] 8 | [clj-gatling "0.16.1" :exclusions [clojider-gatling-highcharts-reporter]] 9 | [clj-time "0.15.2"] ;clj-gatling does not include clj-time anymore 10 | [clojider-gatling-highcharts-s3-reporter "0.2.0"] 11 | [uswitch/lambada "0.1.2"] 12 | [org.clojure/tools.cli "0.3.7"] 13 | [com.amazonaws/aws-java-sdk-iam "1.10.50"] 14 | [com.amazonaws/aws-java-sdk-lambda "1.10.50"] 15 | [com.amazonaws/aws-java-sdk-core "1.10.50"] 16 | [com.amazonaws/aws-java-sdk-s3 "1.10.50"]] 17 | :uberjar-exclusions [#"scala.*"] 18 | :aot [clojider.core clojider.lambda] 19 | :main clojider.core) 20 | -------------------------------------------------------------------------------- /src/clojider/aws.clj: -------------------------------------------------------------------------------- 1 | (ns clojider.aws 2 | (:require [cheshire.core :refer [generate-string]]) 3 | (:import [com.amazonaws.services.identitymanagement AmazonIdentityManagementClient] 4 | [com.amazonaws.services.identitymanagement.model AttachRolePolicyRequest 5 | CreatePolicyRequest 6 | CreateRoleRequest 7 | DeleteRoleRequest 8 | DeletePolicyRequest 9 | ListRolePoliciesRequest 10 | DetachRolePolicyRequest] 11 | [com.amazonaws.services.lambda AWSLambdaClient] 12 | [com.amazonaws.services.lambda.model CreateFunctionRequest 13 | DeleteFunctionRequest 14 | UpdateFunctionCodeRequest 15 | FunctionCode] 16 | [com.amazonaws.auth DefaultAWSCredentialsProviderChain] 17 | [com.amazonaws.services.s3 AmazonS3Client] 18 | [com.amazonaws.regions Regions] 19 | [java.io File])) 20 | 21 | (def aws-credentials 22 | (delay (.getCredentials (DefaultAWSCredentialsProviderChain.)))) 23 | 24 | (defonce s3-client 25 | (delay (AmazonS3Client. @aws-credentials))) 26 | 27 | (defn- create-results-bucket [bucket-name region] 28 | (if (.doesBucketExist @s3-client bucket-name) 29 | (println bucket-name "already exists. Skipping creation.") 30 | (do (println "Creating bucket" bucket-name "for the results.") 31 | (if (= "us-east-1" region) 32 | (.createBucket @s3-client bucket-name) 33 | (.createBucket @s3-client bucket-name region))))) 34 | 35 | (defn- store-jar-to-bucket [bucket-name jar-path] 36 | (println "Uploading code to S3 from" jar-path) 37 | (.putObject @s3-client 38 | bucket-name 39 | "clojider.jar" 40 | (File. jar-path))) 41 | 42 | (defn- delete-results-bucket [bucket-name] 43 | (.deleteBucket @s3-client bucket-name)) 44 | 45 | (defn- delete-all-objects-from-bucket [bucket-name] 46 | (println "Deleting all objects from bucket" bucket-name) 47 | (let [object-keys (map #(.getKey %) 48 | (.getObjectSummaries (.listObjects @s3-client bucket-name)))] 49 | (doseq [object-key object-keys] 50 | (println "Deleting" object-key) 51 | (.deleteObject @s3-client bucket-name object-key)))) 52 | 53 | (def role 54 | {:Version "2012-10-17" 55 | :Statement {:Effect "Allow" 56 | :Principal {:Service "lambda.amazonaws.com"} 57 | :Action "sts:AssumeRole"}}) 58 | 59 | (defn policy [bucket-name] 60 | {:Version "2012-10-17" 61 | :Statement [{:Effect "Allow" 62 | :Action ["s3:PutObject"] 63 | :Resource (str "arn:aws:s3:::" bucket-name "/*")} 64 | {:Effect "Allow" 65 | :Action ["logs:CreateLogGroup" 66 | "logs:CreateLogStream" 67 | "logs:PutLogEvents"] 68 | :Resource ["arn:aws:logs:*:*:*"]}]}) 69 | 70 | (defn create-role-and-policy [role-name policy-name bucket-name] 71 | (println "Creating role" role-name "with policy" policy-name) 72 | (let [client (AmazonIdentityManagementClient. @aws-credentials) 73 | role (.createRole client (-> (CreateRoleRequest.) 74 | (.withRoleName role-name) 75 | (.withAssumeRolePolicyDocument (generate-string role))))] 76 | (let [policy-result (.createPolicy client (-> (CreatePolicyRequest.) 77 | (.withPolicyName policy-name) 78 | (.withPolicyDocument (generate-string (policy bucket-name)))))] 79 | (.attachRolePolicy client (-> (AttachRolePolicyRequest.) 80 | (.withPolicyArn (-> policy-result .getPolicy .getArn)) 81 | (.withRoleName role-name)))) 82 | (-> role .getRole .getArn))) 83 | 84 | (defn delete-role-and-policy [role-name policy-name] 85 | (println "Deleting role" role-name "with policy" policy-name) 86 | (let [client (AmazonIdentityManagementClient. @aws-credentials) 87 | policy-arn (.getArn (first (filter #(= policy-name (.getPolicyName %)) 88 | (.getPolicies (.listPolicies client)))))] 89 | (.detachRolePolicy client (-> (DetachRolePolicyRequest.) 90 | (.withPolicyArn policy-arn) 91 | (.withRoleName role-name))) 92 | (.deletePolicy client (-> (DeletePolicyRequest.) 93 | (.withPolicyArn policy-arn))) 94 | (.deleteRole client (-> (DeleteRoleRequest.) 95 | (.withRoleName role-name))))) 96 | 97 | (defn- create-lambda-client [region] 98 | (-> (AWSLambdaClient. @aws-credentials) 99 | (.withRegion (Regions/fromName region)))) 100 | 101 | (defn delete-lambda-fn [lambda-name region] 102 | (println "Deleting Lambda function" lambda-name "from region") 103 | (let [client (create-lambda-client region)] 104 | (.deleteFunction client (-> (DeleteFunctionRequest.) 105 | (.withFunctionName lambda-name))))) 106 | 107 | (defn- update-lambda-fn [lambda-name bucket-name region] 108 | (println "Updating Lambda function" lambda-name "in region") 109 | (let [client (create-lambda-client region)] 110 | (.updateFunctionCode client (-> (UpdateFunctionCodeRequest.) 111 | (.withFunctionName lambda-name) 112 | (.withS3Bucket bucket-name) 113 | (.withS3Key "clojider.jar"))))) 114 | 115 | (defn- create-lambda-fn [lambda-name bucket-name region role-arn] 116 | (println "Creating Lambda function" lambda-name "to region" region) 117 | (let [client (create-lambda-client region)] 118 | (.createFunction client (-> (CreateFunctionRequest.) 119 | (.withFunctionName lambda-name) 120 | (.withMemorySize (int 1536)) 121 | (.withTimeout (int 300)) 122 | (.withRuntime "java8") 123 | (.withHandler "clojider.LambdaFn") 124 | (.withCode (-> (FunctionCode.) 125 | (.withS3Bucket bucket-name) 126 | (.withS3Key "clojider.jar"))) 127 | (.withRole role-arn))))) 128 | 129 | (defn install-lambda [{:keys [region bucket file]}] 130 | (let [role (str "clojider-role-" region) 131 | policy (str "clojider-policy-" region) 132 | role-arn (create-role-and-policy role policy bucket)] 133 | (Thread/sleep (* 10 1000)) 134 | (create-results-bucket bucket region) 135 | (store-jar-to-bucket bucket file) 136 | (create-lambda-fn "clojider-load-testing-lambda" bucket region role-arn))) 137 | 138 | (defn update-lambda [{:keys [region bucket file]}] 139 | (store-jar-to-bucket bucket file) 140 | (update-lambda-fn "clojider-load-testing-lambda" bucket region)) 141 | 142 | (defn uninstall-lambda [{:keys [region bucket]}] 143 | (let [role (str "clojider-role-" region) 144 | policy (str "clojider-policy-" region)] 145 | (delete-role-and-policy role policy) 146 | (delete-lambda-fn "clojider-load-testing-lambda" region) 147 | (delete-all-objects-from-bucket bucket) 148 | (delete-results-bucket bucket))) 149 | -------------------------------------------------------------------------------- /src/clojider/core.clj: -------------------------------------------------------------------------------- 1 | (ns clojider.core 2 | (:require [clojure.tools.cli :refer [parse-opts]] 3 | [clojider.rc :as rc] 4 | [clojure.string :refer [split]] 5 | [clojider-gatling-highcharts-sthree-reporter.core :as s3] 6 | [clj-gatling.core :as gatling] 7 | [clj-time.core :as t] 8 | [clojider.aws :as aws]) 9 | (:gen-class)) 10 | 11 | (def cli-options 12 | [["-r" "--region REGION" "Which region to use"] 13 | ["-b" "--bucket BUCKET" "Amazon S3 bucket name for Clojider result files"] 14 | ["-f" "--file FILE" "Path of uberjar"] 15 | ["-s" "--simulation SIMULATION" "Fully qualified name of simulation"] 16 | ["-e" "--custom-reporters CUSTOM-REPORTERS" "Comma separated list of fully qualified names of custom reporters to use"] 17 | ["-c" "--concurrency CONCURRENCY" "Concurrency" 18 | :default 1 19 | :parse-fn #(Integer/parseInt %)] 20 | ["-n" "--nodes NODES" "How many Lambda nodes to use" 21 | :default 1 22 | :parse-fn #(Integer/parseInt %)] 23 | ["-t" "--timeout TIMEOUT" "Rquest timeout in milliseconds" 24 | :default 5000 25 | :parse-fn #(Integer/parseInt %)] 26 | ["-d" "--duration DURATION" "Duration in seconds" 27 | :default (t/seconds 1) 28 | :parse-fn #(t/seconds (Integer/parseInt %))]]) 29 | 30 | (defn- choose-reporters [reporters-str] 31 | (if reporters-str 32 | (map #(eval (read-string %)) (split reporters-str #",")) 33 | [s3/reporter])) 34 | 35 | (defn run-with-lambda [{:keys [simulation region bucket concurrency nodes duration timeout custom-reporters] :as options}] 36 | (println "Running simulation" simulation "with options" options) 37 | (rc/run-simulation (read-string simulation) {:region region 38 | :concurrency concurrency 39 | :node-count nodes 40 | :bucket-name bucket 41 | :reporters (choose-reporters custom-reporters) 42 | :timeout-in-ms timeout 43 | :duration duration})) 44 | 45 | (defn run-using-local-machine [{:keys [simulation region bucket concurrency duration timeout custom-reporters] :as options}] 46 | (println "Running simulation" simulation "with options" options) 47 | (gatling/run (read-string simulation) 48 | {:concurrency concurrency 49 | :context {:region region :bucket-name bucket} 50 | :root "tmp" 51 | :reporters (choose-reporters custom-reporters) 52 | :timeout-in-ms timeout 53 | :duration duration})) 54 | 55 | (def cmds 56 | {"install" aws/install-lambda 57 | "uninstall" aws/uninstall-lambda 58 | "update" aws/update-lambda 59 | "load-lambda" run-with-lambda 60 | "load-local" run-using-local-machine}) 61 | 62 | (defn -main [& args] 63 | (let [options (parse-opts args cli-options) 64 | cmd-str (first (:arguments options))] 65 | (if-let [cmd (get cmds cmd-str)] 66 | (cmd (:options options)) 67 | (println "Unknown command:" cmd-str)))) 68 | 69 | -------------------------------------------------------------------------------- /src/clojider/examples.clj: -------------------------------------------------------------------------------- 1 | (ns clojider.examples 2 | (:require [clojure.core.async :refer [chan go >!]] 3 | [org.httpkit.client :as http])) 4 | 5 | (def base-url "http://clj-gatling-demo-server.herokuapp.com") 6 | 7 | (defn- http-get [url _] 8 | (let [response (chan) 9 | check-status (fn [{:keys [status]}] 10 | (go (>! response (= 200 status))))] 11 | (http/get (str base-url url) {} check-status) 12 | response)) 13 | 14 | (defn- http-get-with-ids [url ids {:keys [user-id]}] 15 | (let [response (chan) 16 | check-status (fn [{:keys [status]}] 17 | (go (>! response (= 200 status)))) 18 | id (nth ids user-id)] 19 | (http/get (str base-url url id) {} check-status) 20 | response)) 21 | 22 | (def ping 23 | (partial http-get "/ping")) 24 | 25 | (def ping-simulation 26 | {:name "Ping simulation" 27 | :scenarios [{:name "Ping scenario" 28 | :steps [{:name "Ping Endpoint" :request ping}]}]}) 29 | 30 | (def article-read 31 | (partial http-get-with-ids "/metrics/article/read/" (cycle (range 100 200)))) 32 | 33 | (def program-start 34 | (partial http-get-with-ids "/metrics/program/start/" (cycle (range 200 400)))) 35 | 36 | (def metrics-simulation 37 | {:name "Metrics simulation" 38 | :scenarios [{:name "Article read scenario" 39 | :weight 2 40 | :steps [{:name "Article read request" 41 | :request article-read}]} 42 | {:name "Program start scenario" 43 | :weight 1 44 | :steps [{:name "Program start request" 45 | :request program-start}]}]}) 46 | 47 | (def simulations 48 | {:ping ping-simulation 49 | :metrics metrics-simulation}) 50 | -------------------------------------------------------------------------------- /src/clojider/lambda.clj: -------------------------------------------------------------------------------- 1 | (ns clojider.lambda 2 | (:require [uswitch.lambada.core :refer [deflambdafn]] 3 | [clojure.java.io :as io] 4 | [clj-time.core :refer [millis]] 5 | [clj-gatling.pipeline :as pipeline] 6 | [cheshire.core :refer [generate-stream parse-stream]])) 7 | 8 | (defn- run-simulation [input] 9 | (let [collector-as-symbol (fn [reporter] 10 | (update reporter :collector read-string)) 11 | simulation (read-string (:simulation input))] 12 | (pipeline/simulation-runner simulation 13 | (-> (:options input) 14 | (update :duration millis) 15 | (assoc :progress-tracker (fn [_])) ;;TODO How to send this info to CLI? 16 | (assoc :results-dir (System/getProperty "java.io.tmpdir")) 17 | (update :reporters #(map collector-as-symbol %)))))) 18 | 19 | (deflambdafn clojider.LambdaFn 20 | [is os ctx] 21 | (let [input (parse-stream (io/reader is) true) 22 | output (io/writer os)] 23 | (println "Running simulation with config" input) 24 | (let [result (run-simulation input)] 25 | (println "Returning result" result) 26 | (generate-stream result output) 27 | (.flush output)))) 28 | -------------------------------------------------------------------------------- /src/clojider/rc.clj: -------------------------------------------------------------------------------- 1 | (ns clojider.rc 2 | (:require [clojider.aws :refer [aws-credentials]] 3 | [clojure.java.io :as io] 4 | [clj-time.core :as t] 5 | [clj-gatling.core :as gatling] 6 | [cheshire.core :refer [generate-string parse-stream]]) 7 | (:import [com.amazonaws ClientConfiguration] 8 | [com.amazonaws.regions Regions] 9 | [com.amazonaws.services.lambda.model InvokeRequest] 10 | [com.amazonaws.services.lambda AWSLambdaClient])) 11 | 12 | (defn parse-result [result] 13 | (-> result 14 | (.getPayload) 15 | (.array) 16 | (java.io.ByteArrayInputStream.) 17 | (io/reader) 18 | (parse-stream true))) 19 | 20 | (defn invoke-lambda [simulation lambda-function-name options] 21 | (println "Invoking Lambda for node:" (:node-id options)) 22 | (let [client-config (-> (ClientConfiguration.) 23 | (.withSocketTimeout (* 6 60 1000))) 24 | client (-> (AWSLambdaClient. @aws-credentials client-config) 25 | (.withRegion (Regions/fromName (-> options :context :region)))) 26 | request (-> (InvokeRequest.) 27 | (.withFunctionName lambda-function-name) 28 | (.withPayload (generate-string {:simulation simulation 29 | :options options})))] 30 | 31 | (parse-result (.invoke client request)))) 32 | 33 | (def max-runtime-in-millis (* 4 60 1000)) 34 | 35 | (defn split-to-durations [millis] 36 | (loop [millis-left millis 37 | buckets []] 38 | (if (> millis-left max-runtime-in-millis) 39 | (recur (- millis-left max-runtime-in-millis) (conj buckets max-runtime-in-millis)) 40 | (conj buckets millis-left)))) 41 | 42 | (defn invoke-lambda-sequentially [simulation lambda-function-name options node-id] 43 | (let [durations (split-to-durations (t/in-millis (:duration options)))] 44 | (apply merge-with concat 45 | (mapv #(invoke-lambda simulation 46 | lambda-function-name 47 | (-> options 48 | (assoc :node-id node-id 49 | :duration % 50 | :timeout-in-ms (:timeout-in-ms options)) 51 | (dissoc :progress-tracker))) 52 | durations)))) 53 | 54 | (defn lambda-executor [lambda-function-name node-id simulation options] 55 | (println "Starting AWS Lambda executor with id:" node-id) 56 | (let [only-collector-as-str (fn [reporter] 57 | (-> reporter 58 | (dissoc :generator) 59 | (update :collector str)))] 60 | (invoke-lambda-sequentially (str simulation) 61 | lambda-function-name 62 | (update options :reporters #(map only-collector-as-str %)) 63 | node-id))) 64 | 65 | (defn run-simulation [^clojure.lang.Symbol simulation 66 | {:keys [concurrency 67 | node-count 68 | bucket-name 69 | reporters 70 | timeout-in-ms 71 | duration 72 | region] 73 | :or {node-count 1}}] 74 | (gatling/run simulation (-> {:context {:region region :bucket-name bucket-name} 75 | :concurrency concurrency 76 | :timeout-in-ms timeout-in-ms 77 | :reporters reporters 78 | :duration duration 79 | :nodes node-count 80 | :executor (partial lambda-executor "clojider-load-testing-lambda")}))) 81 | --------------------------------------------------------------------------------