Top Banner
Software Performance Analysis Chapter 11: Performance Antipatterns SOFT 437
45

Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

Apr 30, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

Software Performance Analysis

Chapter 11: Performance Antipatterns

SOFT 437

Page 2: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 2

Antipatterns

• Design patterns document recurring solutions to common design problems

• The use (or misuse) of design patterns produces negative consequences

• Antipatterns document common mistakes• Performance patterns illustrate “best practice”

approaches to achieving responsiveness and scalability

• Performance antipatterns illustrate how to identify a bad situation and provide a way to rectify the problem

Page 3: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 3

Refactoring

• Antipatterns are refactored (restructured or reorganized) to overcome their negative consequences

• A refactoring is a correctness-preservingtransformation that improves the quality of the software

• Example:– A set of class can be refactored to improve modifiability

by moving common properties to an abstract superclass– The transformation does not alter the semantics of the

application

Page 4: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 4

Page 5: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 5

Origins of AntiPatterns

• A manager or developer– Does not know any better– Does not have sufficient knowledge or experience

solving a particular problem– Applied a perfectly good design pattern in the wrong

context

Page 6: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 6

State of Affairs

• Five out of six software projects are considered unsuccessful

• One third of all software projects are canceled• For delivered systems the actual budget and time is

double than expected

Page 7: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 7

AntiPattern Structure

• Description of the general form• Symptoms on how to recognize the general form• Causes that led to the general form• Consequences of the general form• Refactored solution on how to change the Antipattern

into a healthier situation

Page 8: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 8

AntiPattern Categories

• Development AntiPatterns• Architectural AntiPatterns• Managerial AntiPatterns• AntiPatterns apply to software construction as well

as software evolution

Page 9: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 9

Page 10: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 10

Page 11: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 11

Page 12: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 12

Antipattern Template

• Name: the section title• Problem: What is the recurrent situation that causes

negative consequences?• Solution: How can we avoid, minimize or refactor

the antipattern?

Page 13: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 13

Antipatterns

• “God” Class• Excessive Dynamic Allocation• Circuitous Treasure Hunt• One-Lane Bridge• Traffic Jam

Page 14: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 14

God Class

• A “god” class is one that performs most of the work of the system, relegating other classes to minor, supporting roles

• A “god class” typically– Has a single, complex controller class– Surrounded by simple classes that serve only as data

containers• The data container classes typically

– Contain only accessor and mutator operations (get() and set() data)

– Perform little or no computation of their own

Page 15: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 15

Example

• The Controller does all of the work– Requests information – Makes decisions– Tells the Valve what to do

• The Valve has no intelligence– Reports its status– Responds to open() and

close() invocations

void openValve(){status currentStatus;currentStatus = theValve-> getStatus();if (currentStatus != open)

theValve->open();}

ControlleropenValve()closeValve()

Valves tatus : enum

getStatus()open()c lose()

Page 16: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 16

Example (con’t)Controller

openV alve()closeValve()

Valves tatus : enum

open()c lose()

void openValve(){theValve->open();

}

void open(){if (status != open)

status = open;}

Page 17: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 17

Problems -- God Class

• Causes excessive message traffic– In functional form, the “god” class requests and

updates the data it needs to control the system from subordinate classes

– In the data form, the subordinate classes request and update data in the “god” class

– The number of messages required to perform a function is larger than it would be in a design that assigned related data and behavior to the same class

Page 18: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 18

Page 19: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 19

Solution – God Class• Refactor the design to distribute intelligence uniformly across

the top-level class in the application• Keep related data and behavior together• Beware of either:

– An object that must request lots of data from other objects and then update their states with the results

– A group of objects that must access a common object to get and update the data it deals with

• The solution to the “god” class problem embodies the Locality Principle– an algorithm and the data that it requires are localized in the

same object

Page 20: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 20

Performance Gain for the Refactoring

• The performance gain for the refactored solution will be

– Ts is the processing time saved– Ms is the number of messages saved– O is the overhead per message

OMT ss

Page 21: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 21

Excessive Dynamic Allocation

• With dynamic allocation, objects are– Created when they are first accessed– Destroyed when they are no longer needed

• This can often be a good approach to structuring a system, providing flexibility in highly dynamic situations

• Excessive dynamic allocation addresses frequent, unnecessary creation and destruction of objects of the same class

Page 22: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 22

Problem – Excessive Dynamic Allocation

• When an object is created,– The memory to contain it (and any objects that it contains) must

be allocated from the heap– Any initialization code for the object and the contained objects

must be executed• When the object is no longer needed

– Necessary clean-up must be performed– The reclaimed memory must be returned to the heap to avoid

“memory leaks”• The performance impact may be significant when a large

number of objects are frequently created and then destroyed

Page 23: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 23

Example: A Call Processing

• A Call object may not seem excessive– A Call is a complex object

that contains several other objects that must also be created

• A switch can receive hundreds of thousands of offHook events each hour

• The overhead for dynamically allocating call objects adds substantial delays to the time needed to complete a call

Page 24: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 24

Cost of Dynamic Allocation

• The cost of dynamic allocation, C, is

– N is the number of calls– depth is the number of contained objects– Sc and Sd are the service time to create and to destroy

the object, respectively

depth

dc SSNC )(

Page 25: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 25

Page 26: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 26

Solution – Excessive Dynamic Allocation

• Pre-allocate a “pool” of objects and stores them in a collection– Recycle objects rather than to create new ones each

time they are needed– Useful for systems that continually need many short-

lived objects– Eliminate the need for object creation and destruction

by “recycling” objects from a pool– This is an application of the Processing vs.

Frequency Principle

Page 27: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 27

Solution – Excessive Dynamic Allocation (con’t)

• Share objects rather than create new ones– Use sharing to eliminate the need to create new

objects

Page 28: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 28

Problem - Circuitous Treasure Hunt

• Software retrieves data from a first table, uses those results to search a second table, retrieves data from that table, and so on, until the “ultimate results” are obtained

• The impact on performance is the large amount of database processing required each time the “ultimate results” are needed– It is especially problematic when the data is on a

remote server, and each access requires transmitting all the intermediate queries and their results via a network

Page 29: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 29

Database Example

Page 30: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 30

Page 31: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 31

Problem – Circuitous Treasure Hunt (con’t)

• In object oriented systems, operations have large “response sets”– One object invokes an operation in another object,

that object then invokes an operation in another object, and so on, until the “ultimate result” is achieved

– Each operation returns, one by one, to the object that made the original call

• The performance impact is the extra processing required to identify the final operation to be called and invoking it, especially in distributed systems

Page 32: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 32

Database Example

Page 33: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 33

Page 34: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 34

Solution – Circuitous Treasure Hunt

• Select a different data organization if a database access problem occurs early in development– Put the data close to where it will be used

• Calls saved

– Cs is the total number of calls saved– aj is the number of associated objects in the level

below for each object in level j

rootpathj

js aC

Page 35: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 35

Solution – Circuitous Treasure Hunt (con’t)

• For distributed systems, if you cannot change the database organization, you can reduce the number of remote database calls by using Adapter pattern to provide a more reasonable interface for remote calls– The Adapter would then make all the other (local)

database calls required to retrieve the “ultimate result”, and return only those results to the remote callers

Page 36: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 36

Adapter Design Pattern

• Adapters are used to enable objects with different interfaces to communicate with each other

• Object adapters use a compositional technique to adapt one interface to another

Page 37: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 37

Solution – Circuitous Treasure Hunt (con’t)

• For design with large response sets, an alternative is to create a new association that leads directly to the “ultimate result”

Page 38: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 38

The One - Lane Bridge

• One-lane bridge is that – Traffic may only travel in one direction at a time– If there are multiple lanes of traffic all moving in

parallel, they must merge and proceed across the bridge one vehicle at a time

• This increases the time required to get a given number of vehicles across the bridge and can also cause long backups

Page 39: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 39

Problem - The One-Lane Bridge

• One, or only a few, processes may continue to execute concurrently

• All other processes must wait• Examples:

– Locks in database ensure that only one process may update the associated portion of the database at a time

– Many processes make a synchronous call to another process that is not multi-threaded

– The database key is a date-time stamp for an entity, or any key that increases monotonically

Page 40: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 40

Page 41: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 41

Solution – The One-Lane Bridge

• Provide additional paths to reduce traffic on the One-Lane Bridge– Reducing the amount of time required to cross the bridge also

helps relieve congestion• For the database example, the magnitude of the

improvement depends on the intensity of new item orders and the service time for performing update. The relationship is:

RT is the residence time, S is the service time, X is the arrival rateXS

SRT

1

Page 42: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 42

Page 43: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 43

Solution – The One-Lane Bridge (con’t)

• The solution to the One-Lane Bridge problem embodies the Shared Resources Principle– Responsiveness improves when we minimize the

scheduling time + holding time– Holding time is minimized by reducing the service

time for the One-Lane Bridge, and by rerouting the work

Page 44: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 44

Traffic Jam• The performance impact of the Traffic Jam is the

transient behavior that produces wide variability in response time

• Problems occur when– The One-Lane Bridge produces a large backlog in jobs

waiting for service, whereupon it takes a long time to return to “normal” operating conditions

– A large amount of work is scheduled within a relatively small interval

• Every user needs a report at approximately the same time• Stock market activity triggers a sudden surge in trading

activity

Page 45: Chapter 11: Performance Antipatternscs.queensu.ca/~elgazzar/soft437/lecture-notes/Chapter11.pdf · • Performance antipatterns illustrate how to identify a bad situation and provide

SOFT 437 – Chapter 11 45

Solution – Traffic Jam

• If the problem is caused by the One-Lane Bridge, solving that that antipattern will reduce the effect of the Traffic Jam

• If the problem is caused by periodic high demand, seek alternatives that spread the load, or handle the demand in a different manner– Adopt the Alternative Routes pattern or the Flex Time pattern

• If the problem is caused by external factors, use SPE techniques to identify the most important workloads, and then streamline their processing as much as possible