0% found this document useful (0 votes)
0 views

code performace issue example in real time project

A performance issue in an ASP.NET Core application was identified due to slow response times from an API endpoint that retrieved user data inefficiently and performed synchronous computations. The solution involved optimizing the database query with pagination and selecting only necessary columns, as well as modifying the computation to avoid blocking operations. As a result, the API's response time improved significantly, enhancing the application's overall performance.

Uploaded by

vivek.kurri121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

code performace issue example in real time project

A performance issue in an ASP.NET Core application was identified due to slow response times from an API endpoint that retrieved user data inefficiently and performed synchronous computations. The solution involved optimizing the database query with pagination and selecting only necessary columns, as well as modifying the computation to avoid blocking operations. As a result, the API's response time improved significantly, enhancing the application's overall performance.

Uploaded by

vivek.kurri121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Certainly! Here’s a real-world example of a performance issue in a .

NET Core
project and how it was addressed:

### Scenario:
You have an ASP.NET Core web application that’s experiencing slow response times
under load. After monitoring and profiling, you discover that a particular API
endpoint is significantly slowing down the application.

### Problem:
The API endpoint retrieves a list of users from a SQL database. The endpoint
queries the database to fetch user data and then performs a series of operations on
this data before sending it to the client. Here’s the initial code for the
endpoint:

```csharp
[HttpGet("users")]
public async Task<IActionResult> GetUsers()
{
var users = await _context.Users.ToListAsync();

foreach (var user in users)


{
// Perform some computation on each user
user.ComputedValue = ComputeSomething(user);
}

return Ok(users);
}

private int ComputeSomething(User user)


{
// Simulate a time-consuming computation
Thread.Sleep(100);
return user.Id * 2;
}
```

### Issue:
1. **Inefficient Query:** The `.ToListAsync()` method retrieves all users from the
database, which can be inefficient if the table is large.
2. **Synchronous Computation:** The `ComputeSomething` method introduces a delay,
which can add up when processing a large number of users.

### Solution:
1. **Optimize the Database Query:**
- If the full user list is not necessary, use pagination to fetch only a subset
of users.
- Select only the necessary columns instead of fetching the entire user object.

2. **Optimize the Computation:**


- Perform computations asynchronously or in parallel to avoid blocking the
thread.
- Use caching or pre-compute values if possible.

Here’s an optimized version of the code:

```csharp
[HttpGet("users")]
public async Task<IActionResult> GetUsers([FromQuery] int page = 1, [FromQuery] int
pageSize = 50)
{
var users = await _context.Users
.Skip((page - 1) * pageSize)
.Take(pageSize)
.Select(u => new
{
u.Id,
u.Name,
ComputedValue = ComputeSomething(u.Id) // Compute value on the server
side
})
.ToListAsync();

return Ok(users);
}

private int ComputeSomething(int userId)


{
// Perform the computation without blocking
return userId * 2;
}
```

### Result:
1. **Improved Query Efficiency:** By using pagination and selecting only necessary
columns, you reduce the amount of data retrieved and processed.
2. **Optimized Computation:** The computation is now part of the query, and by
removing `Thread.Sleep`, it’s performed more efficiently.

### Summary:
In this example, the performance issue was addressed by optimizing both the
database query and the computation logic. By reducing the data being processed and
avoiding blocking operations, the API endpoint’s response time was significantly
improved, enhancing the overall performance of the application.

You might also like