TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

52
© Gatling 2015. All rights reserved. Requirements Background in programming 1. Object oriented language mostly 2. Functional programming is a plus, but optional Experience 1. Gatling? 2. Scala? 1

Transcript of TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

Page 1: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Requirements

Background in programming1. Object oriented language mostly2. Functional programming is a plus, but optional

Experience1. Gatling?2. Scala?

1

Page 2: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Load Testing Done Righthttp://gatling.io

Guillaume Corré @notdryft

2

Page 3: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Workshop Schedule

1. Architecture2. Usage3. Exercises

3

Page 4: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Workshop Schedule

1. Architecture 2. Usage3. Exercises

4

Page 5: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Gatling: Architecture

Designed for :1. Complex use cases2. High Performance3. Maintainability

5

Page 6: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Gatling: Architecture

High PerformanceOptimize thread usage:

1. Non Blocking IO

2. Message oriented orchestration

6

Page 7: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Gatling: Architecture

High Performance

7

Page 8: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Gatling: Architecture

MaintainabilityCode

1. Real programming language2. DSL3. API

8

Page 9: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Gatling: Architecture

MaintainabilityWhy?

1. Versioning2. Refactoring3. Peer review4. Composition

9

Page 10: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Gatling: Architecture

Maintainability

10

class Simple extends Simulation { val scn = scenario("simple") .exec( http("home") .get("http://gatling.io") .check(status.is(200)))

setUp( scn.inject( atOnceUsers(1)))}

Page 11: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Workshop Schedule

1. Architecture2. Usage 3. Exercises

11

Page 12: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Gatling: Usage

12

Page 13: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Usage: DSL

Extensive documentation:http://gatling.io/#/docs

Cheat-sheet:http://gatling.io/docs/2.1.7/cheat-sheet.html

13

Page 14: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Step 1: Create a Simulation class

14

import io.gatling.core.Predef._import io.gatling.http.Predef._

import scala.concurrent.duration._

class Simple extends Simulation {

}

Page 15: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Step 2: Create a Scenario

15

import …

class Simple extends Simulation {

val scn = scenario("simple")}

Page 16: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Step 3: Chain Actions

16

import …

class Simple extends Simulation {

val scn = scenario("simple") .exec( http("home") .get("http://gatling.io"))}

Page 17: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Step 4: Check your requests are fine

17

import …

class Simple extends Simulation {

val scn = scenario("simple") .exec( http("home") .get("http://gatling.io") .check(status.is(200))}

Page 18: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Step 5: Set up and inject users

18

import …

class Simple extends Simulation {

val scn = scenario("simple") .exec( http("home") .get("http://gatling.io") .check(status.is(200))

setUp(scn.inject(atOnceUsers(1)))}

Page 19: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Step 6: Make things dynamic

19

// Set up virtual users with feedersfeed(csv("credentials.csv"))

Page 20: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Step 6: Make things dynamic

20

// Capture response data with checkshttp("request") .get("url") .check(css("someSelector").saveAs("someVariable"))

Page 21: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Step 6: Make things dynamic

21

// Craft requests with the Expression Languagehttp("request") .get("/foo?bar=${someVariable}")

Page 22: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Step 6: Make things dynamic

22

// Use functionsexec { session => println(session) session}

Page 23: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Expressions

23

type Expression[T] = Session => Validation[T]

Page 24: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Workshop Schedule

1. Architecture2. Usage3. Exercises

24

Page 25: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Workshop

4 exercises1. Ranged from (almost) easy to difficult2. Each pinpoint different specifics of Gatling

Urlbit.ly/gatling-workshop

Documentation reminderhttp://gatling.io/#/docs

http://gatling.io/docs/2.1.7/cheat-sheet.html

25

Page 26: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 1:1. First RequestWhat happens if you don't do any checks?

1. Gatling adds a status check by default

26

Page 27: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 1:1. First RequestAre there any defaults? If so, which?

1. status.in2. All statuses of the 20x family3. 304

27

Page 28: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 1:3. ChecksWhich check should you use to fetch the title ?

1. Regex1. Most the time harder to maintain2. Still does the job pretty well

2. XPath1. HTML needs to be strict and well formed

3. CSS is easy to read and fast1. css("#logo-section a", "title")

28

Page 29: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 1:3. ChecksWhat happens when a check matches more than a single element?

1. exists by default2. And, if you don’t specify a fetch method but try to save,

Gatling will add a find(0) before saveAs

29

Page 30: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 1:4. Information retrieval

30

Which check should you use to fetch the menu links ?1. css("#main-menu-inner > ul > li > a", "href")

Page 31: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 1:4. Information retrievalHow can you save all matches?

1. findAll

31

Page 32: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 1:4. Information retrievalHow can you access the “menuLinks” attribute?

1. Use the exec { session => … } syntax2. session(“menuLinks”) for extraction3. Then, either

1. as[Type]2. asOption[Type]3. validate[Type]

4. Validate recommended unless you know what you are doing

32

Page 33: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 1:4. Iterate over menu linksWhich DSL element should you use?

1. foreach2. Takes an Expression[String] as its first argument

1. Which is a function of Session to Validation[String]3. Gatling does the EL conversion for you

33

Page 34: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 1:4. Iterate over menu linksHow do think it works behind the hood?

1. Iterate over each element2. Put current element into the session as an attribute3. Run the action block with the new session

34

Page 35: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 2:1. Fetch the login pageWhich baseURL should you use?

1. Gatling doesn’t add a / before concatenating2. One of the following

1. baseURL(“https://github.com”) and get(“/login”)2. baseURL(“https://github.com/“) and get(“login”)

3. Prefer the first one

35

Page 36: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 2:1. Fetch the login pageWhich check should you use?

1. The general idea is to make sure you are on the page you requested

2. css(".auth-form form h1").is("Sign in")

36

Page 37: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 2:2.1. Performing the loginWhich request should you do?

1. /sessionWhich HTTP method does it need to use?

2. POST

37

Page 38: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 2:2.2. Specifying additional parametersDo you think these are generated or provided?

1. Generated by Github, provided in the HTML.Where can you fetch thoses?

2. Add a check to the previous request3. css(".auth-form form input[name='authenticity_token']",

“value").saveAs("authenticityToken")

38

Page 39: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 2:3. ChecksWhich checks should you use to be sure you are logged in?

1. Check for content, titles, css, etc.2. css(“.logged_in”)3. exists is a default

39

Page 40: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 2:4. Feeding the credentialsWhich feeder file format could you use?

1. Multiple formats are handled by Gatling1. CSV, TSV, etc.

2. Just beware of malformed files

40

Page 41: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 2:4. Feeding the credentialsHow does the session interact with the feeder?

1. A feeder outputs lines of CSV (and else) files into the session

2. One line per user, with different methods of retrieval1. Queue, failure when reading is over2. Random3. Shuffle, which is a shuffle + queue4. Circular

3. If you want more, you can skip the feeding to Gatling and fetch the records yourself

41

Page 42: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 3:1. Create gistThink of multiple ways to load the templates

1. StringBody2. RawFileBody3. ELFileBody, with parsed content

42

Page 43: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 3:1. Create gistWhich one is better in this case?

1. All are good2. Still, scala can handle string template pretty well3. => StringBody

43

Page 44: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 4:Part 1. 1. Start simpleIs using java.util.Random a good idea in this case?What could happen if you do?

1. Random is synchronized2. Multiples threads would block each other when fetching3. ThreadLocalRandom is not4. It creates a single Random for each Thread, resolving

conflicts

44

Page 45: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 4:Part 1. 4. A twistHow does Gatling execute requests?

1. Each requests are launched in sequence2. They cannot overlap3. And cannot be launched asynchronously

How should we proceed instead?

45

Page 46: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 4:Part 1. 4. A twistHow does Gatling execute requests?

1. Each requests are launched in sequence2. They cannot overlap3. And cannot be launched asynchronously

How should we proceed instead?4. We can use the resources mechanism5. Not its purpose, but it will work

46

Page 47: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 4:Part 1I. 2. Making the queriesIs the number of tiles requested really fixed?

1. What about edges ?2. Will reduce the number of tiles3. We can reduce the bounds, excluding the edges

47

Page 48: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 4:Part 1I. 2. Making the queriesMixing Gatling’s EL and Scala’s String Macro

1. This a Gatling EL: “${variableName}”2. Theses are Scala Macros:

1. s”$variableName”2. s”${variableName}

How?

48

Page 49: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Exercise 4:Part 1I. 2. Making the queriesMixing Gatling’s EL and Scala’s String Macro

1. This a Gatling EL: “${variableName}”2. Theses are Scala Macros:

1. s”$variableName”2. s”${variableName}

How?1. Escape Gatling’s EL when in a Scala String Macro

1. s”$${gatlingVariable}/${otherVariable}”2. will output “${gatlingVariable}/variableContent”

49

Page 50: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Workshop

Solutions1. Not available yet

Urlbit.ly/gatling-workshop-solutions

50

Page 51: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

Page 52: TestWorks Conf Performance testing made easy with gatling - Guillaume Corré

© Gatling 2015. All rights reserved.

http://gatling.io

http://github.com/gatling/gatling

@GatlingTool