SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Downloaden Sie, um offline zu lesen
Performance Tuning of
.NET application
Md. Mainul Islam
Software Engineer, SELISE
mainul098@mail.com
âś“ What is Performance Tuning?
âś“ How to profile performance?
âś“ Identify hotspot bottlenecks
âś“ Ways to optimize .NET Application.
Why Are We Here?
Lets Explore
“Performance tuning is the improvement of
system performance”
Your app
Reality of Performance
You
Where is the Bottleneck?
for (int n = 2; n < 10000; ++n)
{
bool prime = true;
for (int d = 2; d < n; ++d)
{
if (n % d == 0)
{
prime = false;
break;
}
}
if (prime) Console.WriteLine(n);
}
“Premature optimization is the root
of all evil”
- Donald Knuth
Mistakes of Performance Tuning
âś“ Optimize without a reliable measurement
âś“ Optimize without verifying the results
âś“ Relying on gut feeling
Performance Matrices
CPU
Execution time,
queuing
Memory
Utilization, load %
Disk
Utilization, I/O Operation
Network
Bandwidth, queueing
How does it work?
1. Select Application to Profile
2. Start Profiling
3. Collect Data and Detect Bottlenecks
4. Optimize Performance
5. Compare Profiling Results
6. Repeat Until Desired Results (Or
Possible)
dotTrace
+
dotMemory
Choosing a Collection
Dozens of Builtin Collections
âś“ List<T>
âś“ ArrayList<T>
âś“ Dictionary<K,V>
âś“ HashSet<T>
âś“ Queue<T>
âś“ SortedList<K,V>
âś“ SortedDictionary<K,V>
âś“ SortedSet<T>
âś“ Stack<T>
… and more
Choose Proper Collection
â–  Stop using List<T> for everything
â–  Ask yourself:
Do you need to access each element by index?
For zero-based index: Use ArrayList and StringCollection classes and
the List<T> generic class.
For key based index: The Hashtable, SortedList, ListDictionary, and
StringDictionary classes, and the Dictionary<TKey, TValue> and
SortedDictionary<TKey, TValue> generic classes.
Do you need to sort your collection?
The Hashtable class sorts its elements by their hash codes.
The SortedDictionary<TKey, TValue> and SortedList<TKey, TValue> generic
classes sort their elements by the key.
Collection - (Contd.)
Do you need to search your collection?
ListDictionary is faster than Hashtable for small collections
(10 items or fewer).
The Dictionary<TKey, TValue> generic class provides faster
lookup than the SortedDictionary<TKey, TValue> generic class.
The multi-threaded implementation is
ConcurrentDictionary<TKey, TValue>. ConcurrentBag<T>
provides fast multi-threaded insertion for unordered data.
Do you need collections that accept only strings?
StringCollection (based on IList) and StringDictionary
(based on IDictionary).
Collection Ordering
Direct
Access?
Lookup
Efficiency
Manipulate
Efficiency
Notes
Dictionary Unordered Via Key Key: O(1) O(1) Best for high performance lookups.
SortedDictionary Sorted Via Key Key: O(log n) O(log n)
Compromise of Dictionary speed and
ordering, uses binary search tree.
List Unordered Via Index
Index: O(1)
Value: O(n)
O(n)
Best for smaller lists where direct
access required and no sorting.
SortedList Sorted Via Key Key: O(log n) O(n)
Faster lookup on preloaded data, but
slower loads.
LinkedList Unordered No Key: O(n) O(1)
Best for lists where inserting/deleting in
middle is common and no direct access
required.
HashSet Unordered Via Key Key: O(1) O(1)
Unique unordered collection, like a
Dictionary except key and value are
same object.
Make your Application as Parallel as
Necessary, But not More
Kind of Parallelism
Parallelism
âś“ Running multiple threads in parallel
✓ .NET 4.0 provide “Task Parallel Library(TPL)” to
simplify parallelism
âś“ But, Threads are expensive - context switch, lock
Asynchrony
✓ Without blocking the caller’s thread
âś“ .NET 4.5 introduce async programming
Demo
Manage Memory Efficiently
GC as a Performance Problem
â–  The .NET GC is non-deterministic
âś“ Contributes unexpecteds, potentially very
long delays that destroy responsiveness.
âś“ No way to anticipate the delay or react to
it.
There’s no GC if you don’t allocate.
Reducing allocation is key to
reducing GC costs.
Reducing Allocation
âś“ Use value types
âś“ Pool large objects (such as buffers)
âś“ Avoid allocation large temporary objects
Use IDisposable
âś“ Finalize is expensive.
âś“ Dispose the object that is not needed.
✓ GC does not call Dispose() for you. It’s
your duty to call it.
Demo
Going Further
âś“ Making .NET Application Faster
âś“ Making .NET Application Even Faster
âś“ IDisposable Best Practices for C# Developers
âś“ Entity Framework Database Performance Anti-Pattern
âś“ Measuring .NET Performance
âś“ .NET Performance Optimization and Profiling with JetBrains
dotTrace
âś“ http://geekswithblogs.
net/BlackRabbitCoder/archive/2011/06/16/c.net-
fundamentals-choosing-the-right-collection-class.aspx
âś“ https://msdn.microsoft.com/en-us/library/dd460717(v=vs.
100).aspx
THANKS
Any questions?
You can find me at
âś“ @mainul098
âś“ mainul098@mail.com

Weitere ähnliche Inhalte

Was ist angesagt?

Can Apache Kafka Replace a Database?
Can Apache Kafka Replace a Database?Can Apache Kafka Replace a Database?
Can Apache Kafka Replace a Database?
Kai Wähner
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
guest11106b
 

Was ist angesagt? (20)

Domain Driven Design Introduction
Domain Driven Design IntroductionDomain Driven Design Introduction
Domain Driven Design Introduction
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
C# Exceptions Handling
C# Exceptions Handling C# Exceptions Handling
C# Exceptions Handling
 
Building towards a Composite API Framework in Salesforce
Building towards a Composite API Framework in SalesforceBuilding towards a Composite API Framework in Salesforce
Building towards a Composite API Framework in Salesforce
 
Kotlin
KotlinKotlin
Kotlin
 
Decomposing Applications for Scalability and Deployability (April 2012)
Decomposing Applications for Scalability and Deployability (April 2012)Decomposing Applications for Scalability and Deployability (April 2012)
Decomposing Applications for Scalability and Deployability (April 2012)
 
CQRS + Event Sourcing
CQRS + Event SourcingCQRS + Event Sourcing
CQRS + Event Sourcing
 
Domain Driven Design 101
Domain Driven Design 101Domain Driven Design 101
Domain Driven Design 101
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Distributed Transactions: Saga Patterns
Distributed Transactions: Saga PatternsDistributed Transactions: Saga Patterns
Distributed Transactions: Saga Patterns
 
Can Apache Kafka Replace a Database?
Can Apache Kafka Replace a Database?Can Apache Kafka Replace a Database?
Can Apache Kafka Replace a Database?
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Domain Driven Design Quickly
Domain Driven Design QuicklyDomain Driven Design Quickly
Domain Driven Design Quickly
 
API
APIAPI
API
 
Kafka Intro With Simple Java Producer Consumers
Kafka Intro With Simple Java Producer ConsumersKafka Intro With Simple Java Producer Consumers
Kafka Intro With Simple Java Producer Consumers
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Clean Code @Voxxed Days Cluj 2023 - opening Keynote
Clean Code @Voxxed Days Cluj 2023 - opening KeynoteClean Code @Voxxed Days Cluj 2023 - opening Keynote
Clean Code @Voxxed Days Cluj 2023 - opening Keynote
 
Introduction to APIs (Application Programming Interface)
Introduction to APIs (Application Programming Interface) Introduction to APIs (Application Programming Interface)
Introduction to APIs (Application Programming Interface)
 
So You Want to Write an Exporter
So You Want to Write an ExporterSo You Want to Write an Exporter
So You Want to Write an Exporter
 

Andere mochten auch

.Net Performance by Bijoy Singhal
.Net Performance by Bijoy Singhal.Net Performance by Bijoy Singhal
.Net Performance by Bijoy Singhal
Rishu Mehra
 
PHX - Session #2 Test Driven Development: Improving .NET Application Performa...
PHX - Session #2 Test Driven Development: Improving .NET Application Performa...PHX - Session #2 Test Driven Development: Improving .NET Application Performa...
PHX - Session #2 Test Driven Development: Improving .NET Application Performa...
Steve Lange
 
PHX Session #3 - "It Works on My Machine!" Closing the Loop Between Developme...
PHX Session #3 - "It Works on My Machine!" Closing the Loop Between Developme...PHX Session #3 - "It Works on My Machine!" Closing the Loop Between Developme...
PHX Session #3 - "It Works on My Machine!" Closing the Loop Between Developme...
Steve Lange
 
Building Scalable .NET Web Applications
Building Scalable .NET Web ApplicationsBuilding Scalable .NET Web Applications
Building Scalable .NET Web Applications
Buu Nguyen
 
10 performance and scalability secrets of ASP.NET websites
10 performance and scalability secrets of ASP.NET websites10 performance and scalability secrets of ASP.NET websites
10 performance and scalability secrets of ASP.NET websites
oazabir
 

Andere mochten auch (11)

.Net Performance by Bijoy Singhal
.Net Performance by Bijoy Singhal.Net Performance by Bijoy Singhal
.Net Performance by Bijoy Singhal
 
PHX - Session #2 Test Driven Development: Improving .NET Application Performa...
PHX - Session #2 Test Driven Development: Improving .NET Application Performa...PHX - Session #2 Test Driven Development: Improving .NET Application Performa...
PHX - Session #2 Test Driven Development: Improving .NET Application Performa...
 
PHX Session #3 - "It Works on My Machine!" Closing the Loop Between Developme...
PHX Session #3 - "It Works on My Machine!" Closing the Loop Between Developme...PHX Session #3 - "It Works on My Machine!" Closing the Loop Between Developme...
PHX Session #3 - "It Works on My Machine!" Closing the Loop Between Developme...
 
Improving Performance in .NET Applications
Improving Performance in .NET ApplicationsImproving Performance in .NET Applications
Improving Performance in .NET Applications
 
Building Scalable .NET Apps
Building Scalable .NET AppsBuilding Scalable .NET Apps
Building Scalable .NET Apps
 
Adam Sitnik "State of the .NET Performance"
Adam Sitnik "State of the .NET Performance"Adam Sitnik "State of the .NET Performance"
Adam Sitnik "State of the .NET Performance"
 
How to Do a Performance Audit of Your .NET Website
How to Do a Performance Audit of Your .NET WebsiteHow to Do a Performance Audit of Your .NET Website
How to Do a Performance Audit of Your .NET Website
 
Building Scalable .NET Web Applications
Building Scalable .NET Web ApplicationsBuilding Scalable .NET Web Applications
Building Scalable .NET Web Applications
 
10 performance and scalability secrets of ASP.NET websites
10 performance and scalability secrets of ASP.NET websites10 performance and scalability secrets of ASP.NET websites
10 performance and scalability secrets of ASP.NET websites
 
Four Practices to Fix Your Top .NET Performance Problems
Four Practices to Fix Your Top .NET Performance ProblemsFour Practices to Fix Your Top .NET Performance Problems
Four Practices to Fix Your Top .NET Performance Problems
 
Metrics Driven DevOps - Automate Scalability and Performance Into your Pipeline
Metrics Driven DevOps - Automate Scalability and Performance Into your PipelineMetrics Driven DevOps - Automate Scalability and Performance Into your Pipeline
Metrics Driven DevOps - Automate Scalability and Performance Into your Pipeline
 

Ă„hnlich wie Performance Tuning of .NET Application

Linq 1224887336792847 9
Linq 1224887336792847 9Linq 1224887336792847 9
Linq 1224887336792847 9
google
 
Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The Enterprise
Daniel Egan
 
Optimizing Application Architecture (.NET/Java topics)
Optimizing Application Architecture (.NET/Java topics)Optimizing Application Architecture (.NET/Java topics)
Optimizing Application Architecture (.NET/Java topics)
Ravi Okade
 
Ssis Best Practices Israel Bi U Ser Group Itay Braun
Ssis Best Practices   Israel Bi U Ser Group   Itay BraunSsis Best Practices   Israel Bi U Ser Group   Itay Braun
Ssis Best Practices Israel Bi U Ser Group Itay Braun
sqlserver.co.il
 
Secrets of highly_avail_oltp_archs
Secrets of highly_avail_oltp_archsSecrets of highly_avail_oltp_archs
Secrets of highly_avail_oltp_archs
Tarik Essawi
 
Automated Testing with Databases
Automated Testing with DatabasesAutomated Testing with Databases
Automated Testing with Databases
elliando dias
 
Effective Test Driven Database Development
Effective Test Driven Database DevelopmentEffective Test Driven Database Development
Effective Test Driven Database Development
elliando dias
 

Ă„hnlich wie Performance Tuning of .NET Application (20)

Linq 1224887336792847 9
Linq 1224887336792847 9Linq 1224887336792847 9
Linq 1224887336792847 9
 
Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The Enterprise
 
Talend Open Studio Fundamentals #1: Workspaces, Jobs, Metadata and Trips & Tr...
Talend Open Studio Fundamentals #1: Workspaces, Jobs, Metadata and Trips & Tr...Talend Open Studio Fundamentals #1: Workspaces, Jobs, Metadata and Trips & Tr...
Talend Open Studio Fundamentals #1: Workspaces, Jobs, Metadata and Trips & Tr...
 
Being RDBMS Free -- Alternate Approaches to Data Persistence
Being RDBMS Free -- Alternate Approaches to Data PersistenceBeing RDBMS Free -- Alternate Approaches to Data Persistence
Being RDBMS Free -- Alternate Approaches to Data Persistence
 
Building a Testable Data Access Layer
Building a Testable Data Access LayerBuilding a Testable Data Access Layer
Building a Testable Data Access Layer
 
The 90-Day Startup with Google AppEngine for Java
The 90-Day Startup with Google AppEngine for JavaThe 90-Day Startup with Google AppEngine for Java
The 90-Day Startup with Google AppEngine for Java
 
Optimizing Application Architecture (.NET/Java topics)
Optimizing Application Architecture (.NET/Java topics)Optimizing Application Architecture (.NET/Java topics)
Optimizing Application Architecture (.NET/Java topics)
 
Perl and Elasticsearch
Perl and ElasticsearchPerl and Elasticsearch
Perl and Elasticsearch
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Ssis Best Practices Israel Bi U Ser Group Itay Braun
Ssis Best Practices   Israel Bi U Ser Group   Itay BraunSsis Best Practices   Israel Bi U Ser Group   Itay Braun
Ssis Best Practices Israel Bi U Ser Group Itay Braun
 
Secrets of highly_avail_oltp_archs
Secrets of highly_avail_oltp_archsSecrets of highly_avail_oltp_archs
Secrets of highly_avail_oltp_archs
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the Server
 
Sql killedserver
Sql killedserverSql killedserver
Sql killedserver
 
Automated Testing with Databases
Automated Testing with DatabasesAutomated Testing with Databases
Automated Testing with Databases
 
Automated scaling of microservice stacks for JavaEE applications - JEEConf 2017
Automated scaling of microservice stacks for JavaEE applications - JEEConf 2017Automated scaling of microservice stacks for JavaEE applications - JEEConf 2017
Automated scaling of microservice stacks for JavaEE applications - JEEConf 2017
 
JEEconf 2017
JEEconf 2017JEEconf 2017
JEEconf 2017
 
.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community.Net Classes and Objects | UiPath Community
.Net Classes and Objects | UiPath Community
 
Certification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptxCertification preparation - Net classses and functions.pptx
Certification preparation - Net classses and functions.pptx
 
Effective Test Driven Database Development
Effective Test Driven Database DevelopmentEffective Test Driven Database Development
Effective Test Driven Database Development
 

Performance Tuning of .NET Application

  • 1. Performance Tuning of .NET application Md. Mainul Islam Software Engineer, SELISE mainul098@mail.com
  • 2. âś“ What is Performance Tuning? âś“ How to profile performance? âś“ Identify hotspot bottlenecks âś“ Ways to optimize .NET Application. Why Are We Here?
  • 4. “Performance tuning is the improvement of system performance”
  • 5. Your app Reality of Performance You
  • 6. Where is the Bottleneck? for (int n = 2; n < 10000; ++n) { bool prime = true; for (int d = 2; d < n; ++d) { if (n % d == 0) { prime = false; break; } } if (prime) Console.WriteLine(n); }
  • 7. “Premature optimization is the root of all evil” - Donald Knuth
  • 8. Mistakes of Performance Tuning âś“ Optimize without a reliable measurement âś“ Optimize without verifying the results âś“ Relying on gut feeling
  • 9. Performance Matrices CPU Execution time, queuing Memory Utilization, load % Disk Utilization, I/O Operation Network Bandwidth, queueing
  • 10. How does it work? 1. Select Application to Profile 2. Start Profiling 3. Collect Data and Detect Bottlenecks 4. Optimize Performance 5. Compare Profiling Results 6. Repeat Until Desired Results (Or Possible)
  • 13. Dozens of Builtin Collections âś“ List<T> âś“ ArrayList<T> âś“ Dictionary<K,V> âś“ HashSet<T> âś“ Queue<T> âś“ SortedList<K,V> âś“ SortedDictionary<K,V> âś“ SortedSet<T> âś“ Stack<T> … and more
  • 14. Choose Proper Collection â–  Stop using List<T> for everything â–  Ask yourself: Do you need to access each element by index? For zero-based index: Use ArrayList and StringCollection classes and the List<T> generic class. For key based index: The Hashtable, SortedList, ListDictionary, and StringDictionary classes, and the Dictionary<TKey, TValue> and SortedDictionary<TKey, TValue> generic classes. Do you need to sort your collection? The Hashtable class sorts its elements by their hash codes. The SortedDictionary<TKey, TValue> and SortedList<TKey, TValue> generic classes sort their elements by the key.
  • 15. Collection - (Contd.) Do you need to search your collection? ListDictionary is faster than Hashtable for small collections (10 items or fewer). The Dictionary<TKey, TValue> generic class provides faster lookup than the SortedDictionary<TKey, TValue> generic class. The multi-threaded implementation is ConcurrentDictionary<TKey, TValue>. ConcurrentBag<T> provides fast multi-threaded insertion for unordered data. Do you need collections that accept only strings? StringCollection (based on IList) and StringDictionary (based on IDictionary).
  • 16. Collection Ordering Direct Access? Lookup Efficiency Manipulate Efficiency Notes Dictionary Unordered Via Key Key: O(1) O(1) Best for high performance lookups. SortedDictionary Sorted Via Key Key: O(log n) O(log n) Compromise of Dictionary speed and ordering, uses binary search tree. List Unordered Via Index Index: O(1) Value: O(n) O(n) Best for smaller lists where direct access required and no sorting. SortedList Sorted Via Key Key: O(log n) O(n) Faster lookup on preloaded data, but slower loads. LinkedList Unordered No Key: O(n) O(1) Best for lists where inserting/deleting in middle is common and no direct access required. HashSet Unordered Via Key Key: O(1) O(1) Unique unordered collection, like a Dictionary except key and value are same object.
  • 17. Make your Application as Parallel as Necessary, But not More
  • 18. Kind of Parallelism Parallelism âś“ Running multiple threads in parallel âś“ .NET 4.0 provide “Task Parallel Library(TPL)” to simplify parallelism âś“ But, Threads are expensive - context switch, lock Asynchrony âś“ Without blocking the caller’s thread âś“ .NET 4.5 introduce async programming
  • 19. Demo
  • 21. GC as a Performance Problem â–  The .NET GC is non-deterministic âś“ Contributes unexpecteds, potentially very long delays that destroy responsiveness. âś“ No way to anticipate the delay or react to it.
  • 22. There’s no GC if you don’t allocate. Reducing allocation is key to reducing GC costs.
  • 23. Reducing Allocation âś“ Use value types âś“ Pool large objects (such as buffers) âś“ Avoid allocation large temporary objects
  • 24. Use IDisposable âś“ Finalize is expensive. âś“ Dispose the object that is not needed. âś“ GC does not call Dispose() for you. It’s your duty to call it.
  • 25. Demo
  • 26. Going Further âś“ Making .NET Application Faster âś“ Making .NET Application Even Faster âś“ IDisposable Best Practices for C# Developers âś“ Entity Framework Database Performance Anti-Pattern âś“ Measuring .NET Performance âś“ .NET Performance Optimization and Profiling with JetBrains dotTrace âś“ http://geekswithblogs. net/BlackRabbitCoder/archive/2011/06/16/c.net- fundamentals-choosing-the-right-collection-class.aspx âś“ https://msdn.microsoft.com/en-us/library/dd460717(v=vs. 100).aspx
  • 27. THANKS Any questions? You can find me at âś“ @mainul098 âś“ mainul098@mail.com