Static Analysis in Search of Performance Anti-Patterns
description
Transcript of Static Analysis in Search of Performance Anti-Patterns
![Page 1: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/1.jpg)
Static Analysis in Search of Performance Anti-PatternsPatrycja WegrzynowiczFounder and CTO, Yonita Inc.Founder and CTO, Yon Labs and Yon Consulting
![Page 2: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/2.jpg)
About me
Past• 10+ years of professional experience as software developer, architect,
and head of software R&D
• PhD in Computer Science (automated code analysis)
• Speaker at JavaOne, JavaZone, ASE, OOPSLA, Jazoon, and others
Present• Founder and CTO of Yonita Inc., Yon Labs, and Yon Consulting
• Bridge the gap between the industry and the academia
Future• Who cares? Seize the day!
![Page 3: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/3.jpg)
AgendaPerformance issues• Performance bottlenecks and how to deal with them
Automated code analysis• Semantic code query system
Performance anti-patterns• Code samples
• Code queries
![Page 4: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/4.jpg)
PERFORMANCE ISSUES
![Page 5: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/5.jpg)
This is Java.
![Page 6: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/6.jpg)
Is Java slow?On most Intels Java is as fast or faster than C• The Gaia Satellie and Data Processing
• William O’Mullane from European Space Astronomy Centre
• Jazoon 2010
![Page 7: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/7.jpg)
Limited Resources
Hardware
OS
Application
LocksPools
FD
Threads
Network Processors
RAM HD
Memory
![Page 8: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/8.jpg)
Sun’s SDK, Arrays.binarySearch, Joshua Bloch
![Page 9: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/9.jpg)
The Way We Write SoftwareBusiness
Requirements
Technology Stack
Software Teams
JSF
JPA
hibernate
struts Tapestr
y
iBatis
JTA
EJB
RMI
GWTSwing
JSP
JMS
awt
JNDI
JDO
![Page 10: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/10.jpg)
Performance IssuesRoot causes• Bad architecture, design, or code
• Wrong configuration (app/db server, OS, RDBMS)
• Rarely insufficient hardware
Bottlenecks usually unnoticed until happen• Special conditions to happen: size of data, number of users
• The later discovered the more expensive to solve
![Page 11: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/11.jpg)
Performance TuningMIF cycle
Dynamic analysis• Log/traces
• Profiles
Environments• Testing
• Production
Monitor
Identify
Fix
![Page 12: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/12.jpg)
Our Code
![Page 13: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/13.jpg)
A Single Spaghetti is Hard to See
![Page 14: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/14.jpg)
(Anti-)Patterns in Our Spaghetti
![Page 15: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/15.jpg)
Automated Code Performance Tuning
+Automated code
analysisDefinions of performance anti-patterns
![Page 16: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/16.jpg)
Detection of Performance Anti-PatternsBenefits• Hints on performance on higher level of abstraction (architecture,
design, programming constructs) instead of in terms of lower level concepts (CPU usage, memory consumptions etc.)
• Supports developers and development phase
• Promotes best practices (refactored solutions)
Problems• Lack of formalization, high-level concepts, textual description
• Implementation variants
• Size of code
![Page 17: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/17.jpg)
AUTOMATED CODE ANALYSIS
Think? Why think! We have computers to do that for us.
– Jean Rostand
![Page 18: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/18.jpg)
Semantic Code Query SystemCode query systems• Explore relationships among program elements
• Structural and call-related predicates
• CodeQuest, JQuery
Semantic code query systems• Focus on behavior (in-depth data flow analysis)
• Behavioral predicates in addition to structural ones
• D-CUBED – my PhD thesis (focus on design patterns)
• Yonita – rewrite + queries
![Page 19: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/19.jpg)
How does Yonita work?
Analyses
Parser
Store
Query
Bytecodeor
Sources
Asm/Recoder
analyses: structural, call flow, data flowtransitive closures relational (MySQL) or
deductive (XSB) database
SQL or Prolog(anti-)patternsbugsvulnerabilities...
![Page 20: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/20.jpg)
MetamodelStructural elements• Very similar to CodeQuest and Jquery
• Statements
Instances• Symbolic instances
Behavioral predicates supporting call and data flows• Output and input values
• Assignments
![Page 21: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/21.jpg)
InstancesNull
New
This
Parameter
Exception
Multiple
ReturnInstance
ParamInstance
ThisInstance
NewInstance
NullInstance
Except.Instance
Instance Instance …
Instance … Instance …
![Page 22: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/22.jpg)
Behavioral PredicatesCalls• makesCallConditionally(Callable, Callable)
• makesCallAlways(Callable, Callable)
Input and output values• hasInput(Callable, Instance)
• hasInput(Statement, Instance)
• hasOutput(Callable, Instance)
• hasOutput(Statement, Instance)
Assignments• assigns(Field, Instance)
![Page 23: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/23.jpg)
Running Yonita PrototypeRun Yonita to parse an application• yonita [classes] [libs] –prolog –mysql
MySQL configuration• hibernate.cfg.xml
• Run MySQL console and ask queries (SQL)
Prolog configuration• -Dprolog.file=output.P -prolog=output.P
• Run XSB Prolog console and ask queries (Prolog)
Yonita on Google App Engine• In progress
![Page 24: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/24.jpg)
PERFORMANCE ANTI-PATTERNS
![Page 25: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/25.jpg)
Redundant Work – Example 1 (Web)
![Page 26: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/26.jpg)
Redundant Work – Example 1 (Web)Multiple calls to
database (7 calls!)
Multiple security check• Each call to getUser
checks permissions
What if getUser were a remote method?!
![Page 27: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/27.jpg)
1 call 5 calls 10 calls 50 calls 100 calls0
500
1000
1500
2000
2500
3000
3500
4000
Chart Title
Axis Title
![Page 28: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/28.jpg)
Redundant Work – Example 2 (agilefant)
![Page 29: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/29.jpg)
Redundant Work – Example 2 (agilefant)
![Page 30: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/30.jpg)
Redundant Work – Example 2 (agilefant – sourceforge project)
Thank you hibernate!Open Session in View
Session cache
![Page 31: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/31.jpg)
Redundant Work – Example 3 (agilefant)
AJAX Calls
![Page 32: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/32.jpg)
Redundant Work – Example 3 (agilefant)
AJAX Calls
![Page 33: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/33.jpg)
Redundant Work – Example 3 (agilefant)
AJAX Calls
![Page 34: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/34.jpg)
Redundant WorkDescription• A time-consuming method is
called many times on a single execution path
Consequences• A slower execution time
Refactored solution• Call the heavy method only once
and store the result for further re-use
Heavy
Heavy
Heavy
![Page 35: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/35.jpg)
Redundant Work – Code QueryredundantWork(X, Heavy) :-
// method Heavy called at least twice...method(X), method(Heavy),isHeavy(Heavy),hasChild(X, Invocation1), methodInvocation(Invocation1, Heavy),hasChild(X, Invocation2), methodInvocation(Invocation2, Heavy),// ...with the same parameterssetof(I1, (hasInput(Invocation1, newInstance(I1)), All),setof(I2, (hasInput(Invocation2, newInstance(I2)), All).
isHeavy(X) :- isWebService(X).isHeavy(X) :- isDatabaseService(X).
![Page 36: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/36.jpg)
Fine Grained Remote CallsEntity Beans• EJB1
• EJB2
Remote Calls• Remote calls have
significant impact on application performance
ClientBean
getX()
getY()
getZ()
![Page 37: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/37.jpg)
One by One Processing – Example 1Add users to a group• Performed in the web
layer one by one
• Multiple calls to the service layer
• Multiple loads and updates each in a separate transaction!
• Multiple authorization!
![Page 38: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/38.jpg)
One by One Processing – Example 2Delete a group• Detach a group from
every user in a group one by one
![Page 39: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/39.jpg)
One by One Processing
Description• The objects in a collection are
processed one by one and a single processing requires a call to a time-consuming method (e.g. database access, remote call)
Consequences• A significant increase in time with
the increase of collection size
Refactored solution• Consider batch processing
Heavy
Heavy
Heavy
Heavy
![Page 40: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/40.jpg)
Unused Object – Example (richfaces 3.2.2)
![Page 41: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/41.jpg)
Unused ObjectDescription• An object created (usually
retrieved from a database), passed to further methods (usually a long chain of method calls), and than not used
Consequences• Higher memory consumption that
may lead to performance issues
Refactored solution• Create objects only if you need
them.
![Page 42: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/42.jpg)
Unused Objects – Hibernate Mapping
![Page 43: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/43.jpg)
Unused Objects - Standard Collection Mapping
![Page 44: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/44.jpg)
Unused Objects - Immutable Collection
![Page 45: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/45.jpg)
SummaryWe need to write better code!
Performance anti-patterns• High-level concepts
• Many implenentation variants
Static analysis can help in discovery of performance issues• Useful tool but it does not replace a traditional approach to
performance tuning (measure, identify, fix)
![Page 46: Static Analysis in Search of Performance Anti-Patterns](https://reader036.fdocuments.in/reader036/viewer/2022062323/568162d9550346895dd35e76/html5/thumbnails/46.jpg)
ContactPatrycja• [email protected]
• twitter.com/YonLabs
Yonita• http://www.Yonita.com
• http://www.YonLabs.com
• http://www.YonConsulting.com