Book Image

ASP.NET Core 1.0 High Performance

By : James Singleton, Pawan Awasthi
Book Image

ASP.NET Core 1.0 High Performance

By: James Singleton, Pawan Awasthi

Overview of this book

ASP.NET Core is the new, open source, and cross-platform, web-application framework from Microsoft. It's a stripped down version of ASP.NET that's lightweight and fast. This book will show you how to make your web apps deliver high performance when using it. We'll address many performance improvement techniques from both a general web standpoint and from a C#, ASP.NET Core, and .NET Core perspective. This includes delving into the latest frameworks and demonstrating software design patterns that improve performance. We will highlight common performance pitfalls, which can often occur unnoticed on developer workstations, along with strategies to detect and resolve these issues early. By understanding and addressing challenges upfront, you can avoid nasty surprises when it comes to deployment time. We will introduce performance improvements along with the trade-offs that they entail. We will strike a balance between premature optimization and inefficient code by taking a scientific- and evidence-based approach. We'll remain pragmatic by focusing on the big problems. By reading this book, you'll learn what problems can occur when web applications are deployed at scale and know how to avoid or mitigate these issues. You'll gain experience of how to write high-performance applications without having to learn about issues the hard way. You'll see what's new in ASP.NET Core, why it's been rebuilt from the ground up, and what this means for performance. You will understand how you can now develop on and deploy to Windows, Mac OS X, and Linux using cross-platform tools, such as Visual Studio Code.
Table of Contents (18 chapters)
ASP.NET Core 1.0 High Performance
Credits
Foreword
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
2
Measuring Performance Bottlenecks

Preface

Microsoft has released a new open source and cross-platform web application framework, called ASP.NET Core. It runs on top of .NET Core, which is also open source, and is primarily used with the C# programming language. You are no longer tied to using Windows with ASP.NET, and you can now develop on a Mac and deploy to Linux. This new platform also offers much higher performance.

In today's world, a web application that only performs well on a developer's workstation and fails to deliver high-performance in production, is unacceptable. The way that web applications are now deployed at scale has changed, and development practices must adapt to take advantage of this. By reading this book, you'll learn about the modern way of making high-performance web applications, and how to do this with ASP.NET Core.

This book addresses web application performance-improvement techniques from both a general standpoint (HTTP, HTTPS, HTTP/2, TCP/IP, database access, compression, I/O, asset optimization, caching, message queuing, and other concerns) and from a C#, ASP.NET Core, and .NET Core perspective. This includes delving into the details of the latest frameworks and demonstrating software design patterns that improve performance.

We will highlight common performance pitfalls, which can often occur unnoticed on developer workstations, along with strategies to detect and resolve these issues early. By understanding and addressing challenges upfront, you can avoid nasty surprises when it comes to deployment time.

We will introduce performance improvements along with the trade-offs that they entail. We will strike a balance between premature optimization and inefficient code by taking a scientific and evidence-based approach, focusing on the big problems and avoiding changes that have little impact.

We assume that you understand the importance of performance for web applications, but we will recap why it's crucial. However, you may not have had any specific or actionable advice, or have much experience of performance problems occurring in the wild.

By reading this book, you'll understand what problems can occur when web applications are deployed at scale to distributed infrastructure, and know how to avoid or mitigate these issues. You will gain experience of how to write high-performance applications without having to learn about issues the hard way, possibly late at night.

You'll see what's new in ASP.NET Core, why it's been rebuilt from the ground up, and what this means for performance. You will understand the future of .NET Core and how you can now develop on and deploy to Windows, Mac OS X, and Linux. You'll appreciate the performance of new features in ASP.NET Core, including updates to the Razor view engine, and you will be aware of cross platform tools, such as Visual Studio Code.

What this book covers

Let's take a look at what topics we'll be covering throughout the book.

Chapter 1Why Performance Is a Feature, discusses the basic premise of this book and shows you why you need to care about the performance of your software. Responsive applications are vital, and it's not simply enough to have functionality work, it also needs to be quick.

Think of the last time you heard someone complaining about an app or website, and it's likely they were unhappy with the performance. Poor performance doesn't only make users unhappy, it also affects your bottom-line. There's good data to suggest that fast performance increases engagement and improves conversion rates, which is why it's rewarded by search engines.

Chapter 2, Measuring Performance Bottlenecks, tells you that the only way you can solve performance problems is to carefully measure your application. Without knowing where a problem lies, your chance of solving it is extremely slim, and you won't even know whether you've improved matters or made things worse.

We will highlight a few ways of manually monitoring performance and some helpful tools that you can use to measure statistics. You'll see how to gain insights into the database, application, HTTP, and network levels of your software so that you know what is going on internally. We'll also show you how to build your own basic timing code and cover the importance of taking a scientific approach to the results.

Chapter 3, Fixing Common Performance Problems, looks at some of the most frequent performance mistakes. We'll show you how to fix simple issues across a range of different application areas, for example, how to optimize media with image resizing or encoding, Select N+1 problems, and asynchronous background operations.

We will also talk a little about using hardware to improve performance once you know where the bottlenecks lie. This approach buys you some time and allows you to fix things properly at a reasonable pace.

Chapter 4, Addressing Network Performance, digs into the networking layer that underpins all web applications. We'll show you how remote resources can slow down your app and demonstrate what you can do about measuring and addressing these problems.

We will look at internet protocols, including TCP/IP, HTTP, HTTP/2, and WebSockets, along with a primer on encryption and how all of these can alter performance. We'll cover compression of textual and image-based assets, including some exotic image formats. Finally, we will introduce caching at the browser, server, proxy, and Content Delivery Network (CDN) levels, showing you some of the basics.

Chapter 5, Optimizing I/O Performance, focuses on input/output and how this can negatively affect performance. We will look at disks, databases, and remote APIs, many of which use the network, particularly if virtualized. We'll cover batching your requests and optimizing database usage with aggregates or sampling, aiming to reduce the data and time required.

Due to networking's ubiquity in cloud environments, we'll spend considerable time on network diagnostics, including pinging, route tracing, and looking up records in the domain name system. You'll learn how latency can be driven by physical distance, or geography, and how this can cause problems for your application. We'll also demonstrate how to build your own networking information gathering tools using .NET.

Chapter 6, Understanding Code Execution and Asynchronous Operations, jumps into the intricacies of C# code and looks at how its execution can alter performance. We'll look at the various open source projects that make up ASP.NET Core and .NET Core, including Kestrel—a high-performance web server.

We will examine the importance of choosing the correct data structures and look at various examples of different options, such as lists and dictionaries. We'll also look at hashing, serialization, and perform some simple benchmarking.

You will learn some techniques that can speed up your processing by parallelizing it, such as Single Instruction Multiple Data (SIMD) and parallel extensions programming with the Task Parallel Library (TPL) and Parallel LINQ (PLINQ). You'll also see some practices that are best avoided, due to their performance penalties, such as reflection and regular expressions.

Chapter 7, Learning Caching and Message Queuing, initially looks at caching, which is widely regarded as difficult. You'll see how caching works from a HTTP perspective in browsers, web servers, proxies, and CDNs. You will learn about cache busting (or breaking) to force your changes and using the new JavaScript service workers in modern browsers to gain finer control over caching.

Additionally, we'll examine caching at the application and database levels in your infrastructure. We will see the benefits of in-memory caches, such as Redis, and how these can reduce the load on your database, lower latency, and increase the performance of your application.

We will investigate message queuing as a way to build a distributed and reliable system. We'll use analogies to explain how asynchronous message passing systems work and show you some common styles of message queuing, including unicast and pub/sub.

We will also show you how message queuing can be useful to an internal caching layer by broadcasting cache invalidation data. You'll learn about message brokers, such as RabbitMQ, and various libraries to interact with them from .NET.

Chapter 8, The Downsides of Performance-Enhancing Tools, concentrates on the negatives of the techniques that we will have covered because nothing comes for free. We'll discuss the virtues of various methods to reduce complexity, use frameworks, and design distributed architecture. We will also cover project culture and see how high-performance is not simply about code but about people too.

We'll look into possible solutions to tackle the problem of distributed debugging and see some available technologies to centrally manage application logging. We'll have a brief introduction to statistics so that you can make sense of your performance metrics, and we will touch upon managing caches.

Chapter 9, Monitoring Performance Regressions, again takes a look at at measuring performance, but in this case, from an automation and Continuous Integration (CI) perspective. We'll reiterate the importance of monitoring and show how you can build this into your development workflow to make it routine and almost transparent. You will see how it is possible to automate almost any type of testing, from simple unit testing to integration testing, and even complex browser User Interface (UI) testing.

We'll show you how you can make your tests more realistic and useful using techniques, such as blue-green deployment and feature switching. You will discover how to perform A/B testing of two versions of a webpage with some very basic feature switching and a few options for fun hardware to keep people engaged in test results. We'll also cover DevOps practices and cloud hosting, both of which make CI easier to implement and complement it nicely.

Chapter 10, The Way Ahead, briefly sums up the lessons of the book and then has a look at some advanced topics that you may like to read more about. We will also try to predict the future for the .NET Core platforms and give you some ideas to take further.

What you need for this book

You will need a development environment to follow the code examples in this book, either Visual Studio Community 2015 or Visual Studio Code if you're not on Windows. You can also use your text editor of choice and the dotnet command line tool. If you use Visual Studio, then you should also install the .NET Core SDK and tooling and the latest NuGet extension.

For some of the chapters, you will also need SQL Server 2014 Express. You can use 2016 too, particularly if you are on Linux. However, you can also use Azure and run against a cloud database.

There are other tools that we will cover, but we will introduce these as they are used. The detailed software/hardware list is uploaded along with the code files.

Who this book is for

This book is mainly aimed at ASP.NET and C# developers, but developers used to other open source platforms may also be interested in .NET Core. You should have experience with the MVC framework for web-application development and be looking to deploy applications that will perform well on live-production environments. These can be virtual machines or hosted by a cloud-service provider, such as AWS or Azure.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The fetch function is the modern version of an XMLHttpRequest."

A block of code is set as follows:

var client = new SmtpClient();
HostingEnvironment.QueueBackgroundWorkItem(ct =>
    client.SendMailAsync(message));

Any command-line input or output is written as follows:

tracert ec2.ap-southeast-2.amazonaws.com

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "If you select Cache Storage, you will see the contents of the cache."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/ASP.NET-Core-1.0-High-Performance We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.