160 likes | 240 Views
Application Performance and The Trap of Object-Orientedness. Targo Tennisberg Development Manager and Self-Proclaimed Guru http://www.targotennisberg.com/tarkvara September 2008. Trap of Object-Orientedness. Tenets of OOP: abstraction, encapsulation, inheritance and polymorphism
E N D
ApplicationPerformance and TheTrapofObject-Orientedness Targo Tennisberg Development Manager and Self-Proclaimed Guru http://www.targotennisberg.com/tarkvara September 2008
TrapofObject-Orientedness • Tenets of OOP: abstraction, encapsulation, inheritance and polymorphism • OOP is good and makes us more productive • It also reduces our level of understanding about the underlying components • Ideally, we’renotsupposedtoknowwhat’sinside a well-encapsulatedcomponent • Realityisnotquite so simple • Super easy to misuse technology • Grandmasendingyou a 500 MB video bydrag & drop • Programmersmaketheexactsamemistakes!
CivilEngineeringAnalogy • If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization(Weinberg’ssecondlaw) • Everycivilengineerknowsthebasicpropertiesofthematerialsheisworkingwith • Imaginebuilding a skyscraperoutof “encapsulated” components • It’scommonforsoftwareengineersnottoknowthealgorithms and behaviorsoftheirunderlyingcomponents
Example: OR Mappings • Classictask: Object-Relationalimpedancemismatch • Manydifferentframeworksbetweentheapp and DB • Mappingstendtomakethingscomplicated and imperfect. Application .Net/Java classes O-RMapper ((N)Hibernate, LINQ, ApplicationServers) SQL Database
CaseStudy: SharePoint, part 1 • Special Case of an OR-Mapper • Documents, lists, web parts etc stored in a database • Thin .Net object model translates API calls to SQL • Simple APIs • New developers tend to call lots of methods • All ofthemresultin a moreorlesscomplex SQL query • Typicalsymptomsinclude: • Acceptableperformance on developer’smachine • Inactualdeploymentor stress test, operationsexhibitrandomslownessorblockages • SQL CPU/memoryoftenveryhigh • No singleobviousproblematicquery, things are sometimesfaster, sometimesslower • Usually a signofthe “EstonianCustoms” applicationmodel
“Eesti Toll” applicationmodel • No centralconceptofwhatdatawerequire • Wheneveranythingisneeded, a newqueryisexecuted • Lotsof “chattiness” and extradatais sent overthewires • Samedatais sent manytimes • Deathby a thousandcuts • OK forprototypingorcertainlow risk areas, buthighlydangerousin real lifeapplications
CaseStudy: SharePoint, part 2 • Reducingthe number queriesbecametheholygrail • Everyqueryexamined: dowe need it? • Combiningstrategy • DataSharingstrategy • Cachingstrategy • Thinkabouthowoftendataactuallychanges • Variousinvalidationalgorithms • Query police: specifictestsfor number ofqueries, developershadtodefendthe need forthemtoanarchitect • Result: SharepointPortal Server 2003 hadonlytwo DB queriesin order torendertheportalhomepage • Incomparison: manycustom SharePoint developmentshave 30+ queries/page • Canleadtothe “Doctor’s Office” applicationmodel
“Perearst” applicationmodel • Manylittlecaches all overtheplace, just likethelinesin a doctor’soffice • Cancreatememorypressure • Significantimprovementoverthe “Eesti toll” model • Oftensufficient • Manycachescanberedundant • SQL, ADO, yourfavoriteframeworkalreadycachesomestuff
CaseStudy: SharePoint, Part 3 • Cutoutasmanydatatransformations and stagesaspossible • Highlyoptimizedqueriesforcommonoperations (like list rendering) • Dataisoftendirectlytranslatedfrom SQL to HTML • Verycomplex, butefficient • Doesnotworkforcustom SharePoint development • “Hansabank” applicationmodel
“Hansapank” applicationmodel • All therelevantdatacanbeprocessedinoneplace • Minimaldatatransferbetweencomponents • Downside: oftenhighlycomplex and difficulttomaintain • Useonlyperformance-criticalareas
Lessonslearned • Identifywiththedata, thinkabouttheroundtrips and stagesitwillgothrough • Modelchoiceisanarchitecturaldecision • Determinewhichmodelyouwilluseearly on inthedesignphase • Verydifficulttochangeafterwards • 90% ofperformanceproblems are designedin, notcodedin • Peopleoftenthinkofperformanceassomethingthatcanbeoptimizedincodeafterthefact – itdoesn’tworkthisway • Never, everassumeyouknowanything • Profile early and often • Beafraidofthedark • Ifyoudon’tknowhow a certaincodepathworks, chances are itwillbeslow and includeunexpectedwork • Thingsgetworse on theirown • Thinkabout all aspectsofperformance • CPU, Memory, I/O, Network
Lessonslearned 2 • Inspectthesource, becomeonewiththecode(LinusTorvalds) • Programmer’sbesttextbookissourcecode • Inopensource, infiniteamountsofavailablereadingmaterial • Inclosedsource, examinethebehaviorofyourbuildingblocks, see howtheybehaveindifferentconditions • Profile, profile, profile