In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to measure and report the response time of ASP.NET Core Web API requests". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how to measure and report the response time of ASP.NET Core Web API requests"!
introduced
Everyone knows that performance is the buzzword for APIs. Time response is an important and measurable parameter of API performance. In this article, we'll learn how to use code to measure the response time of an API and then return the response time data to the client.
Why we need to measure response time
First, let's take a moment to think about why we need such a feature to measure API response time. Here are some scenarios for writing code to capture response time.
You need to define an SLA (Service Level Agreement) for your API for your customers. Customers need to know how long the API takes to respond. Response time data can help us determine the SLA of the API.
Management is interested in how fast the reporting application is. You need to have data to substantiate your reported claims. It pays to report on application performance and share it with stakeholders.
Clients need to have information about the API's response time so they can track how much time is spent on the client and server.
You've probably encountered similar requests in your projects, so it's worth exploring a way to capture API response time.
Where to add measurement codes?
Let's explore some ways to capture API response time, focusing primarily on the time spent capturing APIs. Our goal is to calculate the elapsed time (in milliseconds) from when the ASP.NET Core runtime receives a request to when it processes the response and returns the result from the server.
What factors should we ignore?
It is important to understand that this discussion does not include the time spent on N/W and the time spent on IIS and application pool startup. If the application pool is not up and running, the first request may affect the overall response time of the API. We could use an application initialization module, but that's beyond the scope of this article.
first attempt
A very whimsical way to capture API response time is to add the following code to each API method at the beginning and end, and then measure the delta to calculate the response time, as shown below.
This code should be able to calculate how long the operation takes. But this does not seem to be the right approach for the following reasons.
If the API has a lot of operations, then we need to add this code to a number of places that are not conducive to maintainability.
This code only measures the time spent on methods, it does not measure the time spent on middleware, filters, controller selection, Action selection, model binding, and other activities.
the second attempt
Let's try to improve the code above by concentrating it in one place for easier maintenance. We need to execute code that calculates response times before and after method execution. If you've used earlier versions of the ASP.NET Web API, you'll be familiar with the concept of Filter. Filters allow you to run code before or after a particular stage in the request processing pipeline.
We will implement a filter for calculating response time, as shown below. We'll create a Filter and start the timer with OnActionExecuting, then stop the timer in the method OnActionExecuted to calculate the API's response time.
This code is not a reliable technique for calculating response time because it does not solve the problem of calculating the time it takes to execute middleware, controller selection, operation method selection, model binding, etc. The filter pipeline executes after MVC selects Action. Therefore, it cannot actually detect the time spent in other ASP.NET pipelines.
the third attempt
We will use ASP.NET Core middleware to calculate API response times
So, what is middleware?
Basically, middleware is a software component that processes requests/responses. Middleware is assembled into the application pipeline and provides services in incoming requests. Each component performs the following operations.
Choose whether to pass the request to the next component in the pipeline.
Work can be performed before and after the next component in the pipeline is invoked.
If you've used HTTP Modules or HTTP Handlers in ASP.NET, you can think of middleware as a replacement in ASP.NET Core. Some examples of middleware are:
MVC middleware
Authentication middleware
Static File Serving
Caching
CORS, wait.
We want to add code to start the timer after the request enters the ASP.NET Core pipeline and stop the timer after the pipeline processes the response. Custom middleware at the beginning of the request pipeline seems to be the best way to access the earliest access of the request and to do so before performing the final step in the pipeline.
We'll build a response time middleware that we'll add to the request pipeline as the first middleware so that we can start the timer as soon as the request enters the ASP.NET Core pipeline.
What about response time data?
Once we have captured the response time data, we can process the data in the following ways.
The complete code snippet for ResponseTimeMiddleware looks like this:
Add response time data to a reporting database or analytics solution.
Writes response time data to a log file.
Response time data is passed to a message queue that can be further processed by another application for reporting and analysis.
Response headers are used to send response time information to client applications that use our Rest API.
There may be other useful ways to use response time data. You can leave a comment in the comments section and tell me how you handle response time data in your app.
Let's start coding.
We will follow the steps below to write the code.
Calculate response time data for API
Reports data back to the client application by passing it in the response header.
code illustrates
The main code is that in the InvokeAsync method, we use the stopwatch class to start the stopwatch once the request enters the first middleware, and then stop the stopwatch after processing the request and the response is ready to return to the client. The OnStarting method provides an opportunity to write custom code to add the delegate to be invoked before sending the response hair to the client.
Finally, we added response time information to the custom header. We use the X-Response-Time-ms header as the response header. As a convention, custom titles begin with an X.
Thank you for reading, the above is "how to measure and report the response time of ASP.NET Core Web API requests" content, after learning this article, I believe that everyone on how to measure and report the response time of ASP.NET Core Web API requests this problem has a deeper understanding, the specific use of the situation also needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.