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

Post on 15-Apr-2017

698 views 3 download

Transcript of 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

© Gatling 2015. All rights reserved.

Load Testing Done Righthttp://gatling.io

Guillaume Corré @notdryft

2

© Gatling 2015. All rights reserved.

Workshop Schedule

1. Architecture2. Usage3. Exercises

3

© Gatling 2015. All rights reserved.

Workshop Schedule

1. Architecture 2. Usage3. Exercises

4

© Gatling 2015. All rights reserved.

Gatling: Architecture

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

5

© Gatling 2015. All rights reserved.

Gatling: Architecture

High PerformanceOptimize thread usage:

1. Non Blocking IO

2. Message oriented orchestration

6

© Gatling 2015. All rights reserved.

Gatling: Architecture

High Performance

7

© Gatling 2015. All rights reserved.

Gatling: Architecture

MaintainabilityCode

1. Real programming language2. DSL3. API

8

© Gatling 2015. All rights reserved.

Gatling: Architecture

MaintainabilityWhy?

1. Versioning2. Refactoring3. Peer review4. Composition

9

© 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)))}

© Gatling 2015. All rights reserved.

Workshop Schedule

1. Architecture2. Usage 3. Exercises

11

© Gatling 2015. All rights reserved.

Gatling: Usage

12

© 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

© 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 {

}

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Step 2: Create a Scenario

15

import …

class Simple extends Simulation {

val scn = scenario("simple")}

© 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"))}

© 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))}

© 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)))}

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Step 6: Make things dynamic

19

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

© 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"))

© 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}")

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Step 6: Make things dynamic

22

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

© Gatling 2015. All rights reserved.

Usage: Very Fast Track

Expressions

23

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

© Gatling 2015. All rights reserved.

Workshop Schedule

1. Architecture2. Usage3. Exercises

24

© 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

© 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

© 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

© 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

© 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

© 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")

© Gatling 2015. All rights reserved.

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

1. findAll

31

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

© Gatling 2015. All rights reserved.

Workshop

Solutions1. Not available yet

Urlbit.ly/gatling-workshop-solutions

50

© Gatling 2015. All rights reserved.

© Gatling 2015. All rights reserved.

http://gatling.io

http://github.com/gatling/gatling

@GatlingTool