10 Performance and profiling

 

This chapter covers

  • Seeing why performance testing is important
  • Benchmarking code with BenchmarkDotNet
  • Collecting profiles with BenchmarkDotNet
  • Analyzing profiles with tools
  • Understanding the garbage collector
  • Writing web API tests with NBomber

We tend to notice when applications are slow but take for granted ones that aren’t. Sometimes, performance is about perception, as when you click a button and nothing happens, so you don’t know whether you clicked it. A way to handle this situation is to make the button inactive or change its color immediately so you’ll get the impression that it’s working fast even if the underlying action takes the same amount of time.

This chapter doesn’t go into these kinds of techniques; instead, it focuses on how to make your .NET applications faster. You’ll learn about some tools and techniques that measure, profile, and analyze performance. Like security, performance is a vast topic that can’t be covered in a chapter, but we’ll explore some of the basics.

10.1 Why test performance?

Premature optimization is the root of all evil.

— Sir C. Antony R. Hoare

10.2 Introduction to BenchmarkDotNet

10.3 Profiles

10.3.1 Capturing profiles with BenchmarkDotNet

10.3.2 Analyzing profiles

10.3.3 Using PerfView

10.3.4 Understanding garbage collection

10.4 Web performance testing

Summary