Designing for Performance Martin Thompson - @mjpt777 Feynman is - - PowerPoint PPT Presentation

designing for performance
SMART_READER_LITE
LIVE PREVIEW

Designing for Performance Martin Thompson - @mjpt777 Feynman is - - PowerPoint PPT Presentation

Designing for Performance Martin Thompson - @mjpt777 Feynman is becoming a real pain. He has the greatest scientific honesty of anyone Ive ever meet - William P Rogers The impact of QED cannot be overestimated. It explains


slide-1
SLIDE 1

Designing for Performance

Martin Thompson - @mjpt777

slide-2
SLIDE 2
slide-3
SLIDE 3
slide-4
SLIDE 4

“Feynman is becoming a real pain.” “He has the greatest scientific honesty of anyone I’ve ever meet…”

  • William P Rogers
slide-5
SLIDE 5

“The impact of QED cannot be

  • verestimated. It explains everything that

is not explained by gravity. It’s also the most accurate theory ever tested by experiments on Earth.”

  • Freeman Dyson
slide-6
SLIDE 6
slide-7
SLIDE 7

“It does not matter how intelligent you are, if you guess and that guess cannot be backed up by experimental evidence – then it is still a guess.”

  • Richard Feynman
slide-8
SLIDE 8

How do we Design for Performance?

slide-9
SLIDE 9
  • 1. What is Performance?
  • 2. What is Clean & Representative?
  • 3. Implementing efficient Models
  • 4. How to Performance Test
slide-10
SLIDE 10

Performance

slide-11
SLIDE 11

Throughput (aka Bandwidth)

slide-12
SLIDE 12

Response Time (aka Latency)

slide-13
SLIDE 13

Scalability

slide-14
SLIDE 14
slide-15
SLIDE 15

Queuing Theory

0.0 2.0 4.0 6.0 8.0 10.0 12.0 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

Response Time Utilisation

slide-16
SLIDE 16

Pro Tip:

Ensure you have sufficient capacity

slide-17
SLIDE 17

Can we go parallel to speedup?

slide-18
SLIDE 18

A

Sequential Process time

B

Amdahl’s Law

slide-19
SLIDE 19

A

Sequential Process

A B

Parallel Process A

A A A

time

B

Amdahl’s Law

slide-20
SLIDE 20

A

Sequential Process Parallel Process B

A B

Parallel Process A

A A A

time

B A B B B B

Amdahl’s Law

slide-21
SLIDE 21

Amdahl's Law

slide-22
SLIDE 22

Universal Scalability Law

C(N) = N / (1 + α(N – 1) + ((β* N) * (N – 1)))

C = capacity or throughput N = number of processors α = contention penalty β = coherence penalty

slide-23
SLIDE 23

Universal Scalability Law

2 4 6 8 10 12 14 16 18 20 1 2 4 8 16 32 64 128 256 512 1024

Speedup Processors

Amdahl USL

slide-24
SLIDE 24

Clean & Representative

slide-25
SLIDE 25
  • Clean

“Morally uncontaminated; pure; innocent”

  • Oxford English Dictionary
slide-26
SLIDE 26
  • Representative

“Serving as a portrayal or symbol of something”

  • Oxford English Dictionary
slide-27
SLIDE 27
  • Representative

Code is the best place to capture current understanding

  • f a model
slide-28
SLIDE 28

Abstractions

slide-29
SLIDE 29

Rules of Abstraction

  • 1. Don’t use abstraction
  • 2. Don’t use abstraction
  • 3. Only consider abstracting when you

see at least 3 things that ARE the same

  • 4. Abstractions must pay for themselves
  • 5. Beware DRY, the evil siren that tricks

you into abstraction

slide-30
SLIDE 30

Abstraction

Megamorphism => Branch Hell

slide-31
SLIDE 31

Abstraction

Not Representative => Big Smell

slide-32
SLIDE 32

Abstraction

Say no to big frameworks!

slide-33
SLIDE 33
slide-34
SLIDE 34

Pro Tip: Abstract when you are

sure of the benefits

slide-35
SLIDE 35

Law of Leaky Abstractions

“All non-trivial abstractions, to some extent, are leaky.”

  • Joel Spolsky
slide-36
SLIDE 36

Law of Leaky Abstractions

“The detail of underlying complexity cannot be ignored.”

slide-37
SLIDE 37

“the purpose of abstracting is not to be vague, but to create a new semantic level in which

  • ne can be absolutely precise”
  • Dijkstra
slide-38
SLIDE 38

How can we abstract memory systems?

slide-39
SLIDE 39
  • It’s about 3 bets!
  • 1. The Temporal Bet
slide-40
SLIDE 40
  • It’s about 3 bets!
  • 1. The Temporal Bet
  • 2. The Spatial Bet
slide-41
SLIDE 41
  • It’s about 3 bets!
  • 1. The Temporal Bet
  • 2. The Spatial Bet
  • 3. The Striding Bet
slide-42
SLIDE 42

Model Implementation

slide-43
SLIDE 43

Coupling vs Cohesion

slide-44
SLIDE 44

Coupling vs Cohesion

public class Queue { private final Object[] buffer; private final int capacity; // Rest of the code }

slide-45
SLIDE 45

Coupling vs Cohesion

public class Queue { private final Object[] buffer; private final int capacity; // Rest of the code }

slide-46
SLIDE 46

Coupling vs Cohesion

Properties Bag

slide-47
SLIDE 47

Pro Tip: Respect

Locality of Reference

slide-48
SLIDE 48

Relationships

slide-49
SLIDE 49

Order Book Order Relationships

slide-50
SLIDE 50

Order Book Order

1 Offers * 1 Bids *

Relationships

slide-51
SLIDE 51

Order Book Order Price Price

1 Bids * 1 Offers *

Relationships

slide-52
SLIDE 52

Order Book Order Price Price

1 Bids * 1 Offers * {ordered, FIFO} {ordered, FIFO}

Relationships

slide-53
SLIDE 53

Pro Tip: Make friends with your

Data Structures

slide-54
SLIDE 54

Pro Tip:

Document, discuss, design tests, before going to code

slide-55
SLIDE 55

Algorithms

slide-56
SLIDE 56

Order of Algorithms

slide-57
SLIDE 57

Order of Algorithms

Magnitude of n?

slide-58
SLIDE 58

Pro Tip:

Know the cardinality

  • f all

significant relationships

slide-59
SLIDE 59

Pro Tip: Algorithms are your

key to service time

slide-60
SLIDE 60

Batching

slide-61
SLIDE 61

Amortise the expensive costs

slide-62
SLIDE 62

Natural Batching

Producers

slide-63
SLIDE 63

Natural Batching

Batcher Producers << Amortise Expensive Costs >>

slide-64
SLIDE 64

Natural Batching

Response Time Load Typical Possible

slide-65
SLIDE 65

Pro Tip: Batch processing is

not just for offline

slide-66
SLIDE 66

Branches, branches, branches...

slide-67
SLIDE 67

Branches

public void doStuff(List<String> things) { if (null == things || things.isEmpty()) { return; } for (String thing : things) { // Do useful work } }

slide-68
SLIDE 68

Branches

public void doStuff(List<String> things) { if (null == things || things.isEmpty()) { return; } for (String thing : things) { // Do useful work } }

slide-69
SLIDE 69

Branches

public void doStuff(List<String> things) { for (String thing : things) { // Do useful work } }

slide-70
SLIDE 70

Pro Tip: Respect the Principle

  • f least surprise
slide-71
SLIDE 71

Loops

slide-72
SLIDE 72

Loops

“If I had more time, I would have written a shorter letter.”

  • Blaise Pascal
slide-73
SLIDE 73

L0 1534 µops

Loops

slide-74
SLIDE 74

L0 1534 µops LBB 28 µops

Loops

LBB 28 µops

slide-75
SLIDE 75

Pro Tip: Craft major loops like

good prose

slide-76
SLIDE 76

Composition

slide-77
SLIDE 77

Composition

Size matters

slide-78
SLIDE 78

Composition

“Inlining is THE optimisation.”

  • Cliff Click
slide-79
SLIDE 79

Composition

Single Responsibility

slide-80
SLIDE 80

Pro Tip:

Small atoms can compose to build anything

slide-81
SLIDE 81

Data

slide-82
SLIDE 82

Data

slide-83
SLIDE 83

Data

slide-84
SLIDE 84

Data

slide-85
SLIDE 85

Data

slide-86
SLIDE 86

Data

slide-87
SLIDE 87

Pro Tip: Embrace Set Theory

and FP techniques

slide-88
SLIDE 88

Performance Testing

slide-89
SLIDE 89

Define Performance Goals

slide-90
SLIDE 90

Establish Design Principles

slide-91
SLIDE 91

Aeron Design Principles

  • 1. Garbage free in steady state running
  • 2. Smart Batching in the message path
  • 3. Lock-free algos in the message path
  • 4. Non-blocking IO in the message path
  • 5. No exceptional cases in message path
  • 6. Apply the Single Writer Principle
  • 7. Prefer unshared state
  • 8. Avoid unnecessary data copies
slide-92
SLIDE 92

How to measure response time?

slide-93
SLIDE 93

Response Time Histograms

slide-94
SLIDE 94

Mode

Response Time Histograms

slide-95
SLIDE 95

Mode Median

Response Time Histograms

slide-96
SLIDE 96

Mode Median Mean

Response Time Histograms

slide-97
SLIDE 97

Source: Gil Tene (Azul Systems)

Coordinated Omission

slide-98
SLIDE 98

HdrHistogram

slide-99
SLIDE 99

Java Microbenchmark Harness

slide-100
SLIDE 100

CPU Performance Counters

slide-101
SLIDE 101

Performance test as part of Continuous Integration

slide-102
SLIDE 102

Can your acceptance tests run as performance tests?

slide-103
SLIDE 103

Build telemetry into production systems

slide-104
SLIDE 104

AGAIN!!!

Build telemetry into production systems

slide-105
SLIDE 105

Counters of:

  • Queue Lengths
  • Concurrent Users
  • Exceptions
  • Transactions - orders, trades
  • Etc.
slide-106
SLIDE 106

Histograms of:

  • Response Times
  • Service Times
  • Queue Lengths
  • Concurrent Users
  • Etc.
slide-107
SLIDE 107

In closing…

slide-108
SLIDE 108

Clean => Uncontaminated

slide-109
SLIDE 109

Representative => True Portrayal

slide-110
SLIDE 110

Does it pass the “Out Loud” test?

slide-111
SLIDE 111

Measure – Don’t Guess!!!

slide-112
SLIDE 112
slide-113
SLIDE 113

Questions?

http://mechanical-sympathy.blogspot.com/ Twitter: @mjpt777 “It does not matter how intelligent you are, if you guess and that guess cannot be backed up by experimental evidence – then it is still a guess.”

  • Richard Feynman