satisfy your technical curiosity profiling and optimizing.net applications ingo rammer
TRANSCRIPT
Satisfy Your Technical Curiosity
Profiling and OptimizingProfiling and Optimizing.NET Applications.NET Applications
Ingo RammerIngo Rammer
http://www.thinktecture.comhttp://www.thinktecture.com
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Ingo Rammer and Ingo Rammer and Support and consulting for software architects and Support and consulting for software architects and developersdevelopers
Application Optimization and TuningApplication Optimization and TuningDeveloper-Coaching and -MentoringDeveloper-Coaching and -MentoringArchitecture and Code ReviewsArchitecture and Code ReviewsPrototyping and Architectural ConsultingPrototyping and Architectural Consulting
http://www.thinktecture.com [email protected]
Satisfy Your Technical Curiosity
Scenario IScenario I
ArchitectureDesignInitial Coding and Load Testing
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Load Testing PitfallsLoad Testing Pitfalls
It’s important to test It’s important to test the right thingthe right thingDouble-check that network link and client CPUs Double-check that network link and client CPUs are not overloadedare not overloadedRun test clients in the same LAN as the serversRun test clients in the same LAN as the servers
Two ways of testing with very different resultsTwo ways of testing with very different resultsSimulated user load (including think times, etc.)Simulated user load (including think times, etc.)Maximum Request Load (no think time, just load)Maximum Request Load (no think time, just load)
Include ramp-up time (“step user load”)Include ramp-up time (“step user load”)
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Load Testing in PracticeLoad Testing in Practice
Track counters of multiple serversTrack counters of multiple serversTest clients, Web servers, DB serversTest clients, Web servers, DB servers
Liebig’s Law of the MinimumLiebig’s Law of the Minimum is applicable here is applicable here as wellas well
““The scarcest resource will limit growth”The scarcest resource will limit growth”
After the resource is identified, take actionAfter the resource is identified, take action
Satisfy Your Technical Curiosity
Scenario IIScenario II
ArchitectureDesignCodingTestDeployment
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
UsersUsers
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Don‘t assume Don‘t assume anythinganything
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
11stst - Trace the Network Interactions - Trace the Network InteractionsThe wire never liesThe wire never lies
Wireshark/Ethereal (Sniffing)Wireshark/Ethereal (Sniffing)TcpTrace (Explicit Port-Forwarding)TcpTrace (Explicit Port-Forwarding)Fiddler, ProxyTrace (HTTP Proxies)Fiddler, ProxyTrace (HTTP Proxies)
Caution: Some companies do not allow sniffing! Caution: Some companies do not allow sniffing!
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Things you might find outThings you might find outOverhead you didn’t expectOverhead you didn’t expect
Authentication, sub-optimal bindings, …Authentication, sub-optimal bindings, …
Roundtrips you didn’t expectRoundtrips you didn’t expectCookiesCookiesImages Images (check your IE settings!)(check your IE settings!)MarshalByRefObject-roundtripsMarshalByRefObject-roundtrips
General: Too many roundtrips, or too bigGeneral: Too many roundtrips, or too bigLatency vs. BandwidthLatency vs. Bandwidth
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
22ndnd - Trace your SQL - Trace your SQLMost SQL today is generatedMost SQL today is generated
DataSets and AdaptersDataSets and AdaptersO/R MappersO/R Mappers
Suboptimal SQL: main reason for scalability probsSuboptimal SQL: main reason for scalability probsToday's databases are Today's databases are (unfortunately?)(unfortunately?) very good: very good:
Hide a lot of problems in single-user access during DEVHide a lot of problems in single-user access during DEV
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Invoicing Application SubsetInvoicing Application Subset
Invoice
InvoiceIdInvoiceDateCustomerIdStatus
InvoiceItems
InvoiceIdItemldProductIdProductNameQuantityPriceStatus
SerialNumbers
InvoiceIDItemIdSerialNumberStatus
SerialNumberAudit
AuditIdEntryDateUsernameSerialNumberDescription
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Invoicing - RequirementsInvoicing - Requirements
Cancel (Status=2)Cancel (Status=2)CopyCopy
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Lessons Learned from SQL ProfilingLessons Learned from SQL ProfilingDataSets, DataAdapters and O/R Mappers are great DataSets, DataAdapters and O/R Mappers are great toolstoolsCan simplify more than 80% - 90% of your generic Can simplify more than 80% - 90% of your generic database access codedatabase access codeBUT: Please avoidBUT: Please avoid them in high-load / them in high-load / high-throughput parts of your applicationhigh-throughput parts of your application
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Optimize Your SQL for TransactionsOptimize Your SQL for Transactions
““UPDATE” with Foreign KeysUPDATE” with Foreign Keys““INSERT INTO ... SELECT”INSERT INTO ... SELECT”
In the high-load areas of your application. Not In the high-load areas of your application. Not everywhereeverywhere!!
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Database LocksDatabase Locks
Minimize Minimize durationduration and and numbernumber of locks of locksDynamic SQL (with parameters) vs. Stored ProceduresDynamic SQL (with parameters) vs. Stored Procedures
UPDATE only changed rows if possibleUPDATE only changed rows if possibleSELECT only the rows you need, join only tables you needSELECT only the rows you need, join only tables you need
Possibility: changed isolation level (snapshot isolation Possibility: changed isolation level (snapshot isolation or or NOLOCKNOLOCK) in statistics if you have to report from OLTP ) in statistics if you have to report from OLTP datadataDon‘t use “Sequence-Tables” (better go for Don‘t use “Sequence-Tables” (better go for Autoincrement/Identity/Guid columns!)Autoincrement/Identity/Guid columns!)
Satisfy Your Technical Curiosity
The #1 Reason for DeadlocksThe #1 Reason for DeadlocksTableName NextValue
Customer 1234
Employee 34
Invoice 445896
InvoiceDetail 2598999
Order 523456
OrderDetail 3347875
SerialNumber 23454355
Inventory 45543
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Indexes help a bit...Indexes help a bit...
Use: Index (TableName)Use: Index (TableName)Not: Index (TableName, NextValue)Not: Index (TableName, NextValue)
SQL Server: An index is only locked when one or SQL Server: An index is only locked when one or more of the contained fields are locked! more of the contained fields are locked! Even snapshot isolation won’t help you here Even snapshot isolation won’t help you here Please note: Oracle Please note: Oracle sequencessequences behave differently. behave differently. They They alwaysalways run without transaction locks. run without transaction locks.
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
33rdrd - Memory Profiling - Memory Profiling
CLR Profiler 2.0 (free tool)CLR Profiler 2.0 (free tool)
Shows how much memory each method allocatesShows how much memory each method allocatesNot leaks, just allocationsNot leaks, just allocations
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Memory UsageMemory Usage
Important for multi-user applicationsImportant for multi-user applicationsIssue: Multiple GC-cycles during one requestIssue: Multiple GC-cycles during one requestGenerative GCGenerative GCShort-lived objects end up in generation 2Short-lived objects end up in generation 2You can quickly check this in PerfMon, too!You can quickly check this in PerfMon, too!
.NET CLR Memory/Gen #0 Collections, #1, #2.NET CLR Memory/Gen #0 Collections, #1, #2
2,5 GB practical maximum for ASP.NET apps on 32 bit 2,5 GB practical maximum for ASP.NET apps on 32 bit systemssystems
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
In-Process ProfilingIn-Process ProfilingOften as a last stepOften as a last step
Reason: In-Proc affects “only” performanceReason: In-Proc affects “only” performanceReason 2: After eliminating memory hogs, code will Reason 2: After eliminating memory hogs, code will usually be faster anywayusually be faster anyway
ProfilerProfilerJetbrains’ DotTraceJetbrains’ DotTraceRedgate ANTSRedgate ANTSAutomated QA AQTimeAutomated QA AQTimeCompuware DevPartnerCompuware DevPartner
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
I code well, so where’s the scary part?I code well, so where’s the scary part?
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
The Scary FactsThe Scary FactsSqlDataSourceSqlDataSource
Grid AGrid A 23,926,450 Bytes23,926,450 BytesGrid BGrid B 17,147,255 Bytes17,147,255 BytesGrid C Grid C 3,325,839 Bytes3,325,839 BytesGridViewGridView 2,583,207 Bytes2,583,207 Bytes
ObjectDataSourceObjectDataSourceGridView GridView 169,551 Bytes169,551 BytesGrid CGrid C 916,769 Bytes916,769 Bytes
ManualManualHTML TablesHTML Tables 51,237 Bytes51,237 Bytesw/o ViewStatew/o ViewState 43,897 Bytes43,897 Bytes
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
SummarySummaryDon‘t assume anythingDon‘t assume anything
Network – the wire never lies (auth overhead, Network – the wire never lies (auth overhead, roundtrips, …)roundtrips, …)Database – generated SQLDatabase – generated SQLMemory – multiple GCs for one request? Memory – multiple GCs for one request? In-Proc Performance – as the last step In-Proc Performance – as the last step (scale up)(scale up)
Don‘t trust anyoneDon‘t trust anyoneIt might not be your code, but it could be your job!It might not be your code, but it could be your job!
Satisfy Your Technical Curiosity
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
ResourcesResources
Wireshark/Ethereal: Wireshark/Ethereal: http://www.wireshark.com
Fiddler: Fiddler: http://www.fiddlertool.com
TcpTrace, ProxyTrace: TcpTrace, ProxyTrace: http://www.pocketsoap.com
SQL Profiler: out of the boxSQL Profiler: out of the box
CLR Profiler 2.0: cryptic link, please googleCLR Profiler 2.0: cryptic link, please google
Satisfy Your Technical Curiosity
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
20062006
DSL, WLAN, MBits, …DSL, WLAN, MBits, …Ping to New York: 800 msecPing to New York: 800 msecDownload of 3,6 GB (Visual Studio):Download of 3,6 GB (Visual Studio):
6 Hours6 Hours
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
Early 90sEarly 90s
1200 baud acoustic coupler1200 baud acoustic coupler„„Ping“ to New York: 1200 msecPing“ to New York: 1200 msecDownload of 3,6 GB (Visual Studio):Download of 3,6 GB (Visual Studio):
347 Days347 Days
Satisfy Your Technical Curiosity
Early 90sEarly 90sDownload347 Days
Walking20.000 km5 km per hour12 hours per day
333 Days
Satisfy Your Technical CuriositySatisfy Your Technical Curiosity
20062006
Latency (Ping): 33% improvementLatency (Ping): 33% improvementBandwidth: 138 800 % improvementBandwidth: 138 800 % improvement