Performance Tuning of .NET Application

27
Performance Tuning of .NET application Md. Mainul Islam Software Engineer, SELISE [email protected]

Transcript of Performance Tuning of .NET Application

Page 1: Performance Tuning of .NET Application

Performance Tuning of .NET application

Md. Mainul IslamSoftware Engineer, SELISE

[email protected]

Page 2: Performance Tuning of .NET Application

✓ What is Performance Tuning?

✓ How to profile performance?

✓ Identify hotspot bottlenecks

✓ Ways to optimize .NET Application.

Why Are We Here?

Page 3: Performance Tuning of .NET Application

Lets Explore

Page 4: Performance Tuning of .NET Application

“Performance tuning is the improvement of system performance”

Page 5: Performance Tuning of .NET Application

Your app

Reality of Performance

You

Page 6: Performance Tuning of .NET Application

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);

}

Page 7: Performance Tuning of .NET Application

“Premature optimization is the root of all evil”

- Donald Knuth

Page 8: Performance Tuning of .NET Application

Mistakes of Performance Tuning✓ Optimize without a reliable measurement

✓ Optimize without verifying the results

✓ Relying on gut feeling

Page 9: Performance Tuning of .NET Application

Performance Matrices

CPUExecution time,

queuing

Memory Utilization, load %

DiskUtilization, I/O Operation

NetworkBandwidth, queueing

Page 10: Performance Tuning of .NET Application

How does it work?1. Select Application to Profile

2. Start Profiling3. Collect Data and Detect Bottlenecks4. Optimize Performance5. Compare Profiling Results6. Repeat Until Desired Results (Or

Possible)

Page 11: Performance Tuning of .NET Application

dotTrace+

dotMemory

Page 12: Performance Tuning of .NET Application

Choosing a Collection

Page 13: Performance Tuning of .NET Application

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

Page 14: Performance Tuning of .NET Application

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.

Page 15: Performance Tuning of .NET Application

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).

Page 16: Performance Tuning of .NET Application

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.

Page 17: Performance Tuning of .NET Application

Make your Application as Parallel as Necessary, But not More

Page 18: Performance Tuning of .NET Application

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

Page 19: Performance Tuning of .NET Application

Demo

Page 20: Performance Tuning of .NET Application

Manage Memory Efficiently

Page 21: Performance Tuning of .NET Application

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.

Page 22: Performance Tuning of .NET Application

There’s no GC if you don’t allocate. Reducing allocation is key to

reducing GC costs.

Page 23: Performance Tuning of .NET Application

Reducing Allocation✓ Use value types✓ Pool large objects (such as buffers)✓ Avoid allocation large temporary objects

Page 24: Performance Tuning of .NET Application

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.

Page 25: Performance Tuning of .NET Application

Demo

Page 26: Performance Tuning of .NET Application

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

Page 27: Performance Tuning of .NET Application

THANKSAny questions?You can find me at

✓ @mainul098✓ [email protected]