Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/synchronous io #151

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 88 additions & 48 deletions SynchronousIO/ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
# SynchronousIO Sample Code

The SynchronousIO sample code illustrates techniques for retrieving information from a web service and returning it to a client. The sample comprises the following items:

* SynchronousIO solution file

* AzureCloudService

* CreateFileToUpload project
It is a sample implementation to ilustrate [Synchronous I/O antipattern](https://learn.microsoft.com/azure/architecture/antipatterns/synchronous-io/)

* WebRole WebAPI project
The SynchronousIO sample code illustrates techniques for retrieving information from a web service and returning it to a client. The sample comprises the following items:

* [Detailed Documentation][docs]
- SynchronousIO Web Api Application
-

The sample simulates fetching information from a data store. The data returned is a
`UserProfile` object (defined in the Models folder in the WebRole project):
The sample simulates fetching information from a data store. The data returned is a `UserProfile` object (defined in the Models folder in the project):

**C#**
``` C#

```C#
public class UserProfile
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
```

The code that actually retrieves the data is located in the `FakeUserProfileService`
class, located in the WebRole project. This class exposes the following three methods:
***C#***
``` C#
The code that actually retrieves the data is located in the `FakeUserProfileService` class, located in the project. This class exposes the following three methods:
**_C#_**

```C#
public class FakeUserProfileService : IUserProfileService
{
public UserProfile GetUserProfile()
Expand All @@ -46,54 +41,99 @@ public class FakeUserProfileService : IUserProfileService
}
}
```
These methods demonstrate the synchronous, task-based asynchronous, and wrapped async
techniques for fetching data. The methods return hard-coded values, but simulate the
delay expected when retrieving information from a remote data store.

WebRole is a Web API project. It contains five controllers:
These methods demonstrate the synchronous, task-based asynchronous, and wrapped async techniques for fetching data. The methods return hard-coded values, but simulate the delay expected when retrieving information from a remote data store.

The Web API project contains five controllers:

- `AsyncController`

- `AsyncUploadController`

- `SyncController`

- `SyncUploadController`

- `WrappedSyncController`

* `AsyncController`
The `AsyncController`, `SyncController`, and `WrappedSyncController` controllers call the corresponding methods of the `FakeUserProfileService` class.

* `AsyncUploadController`
The `AsyncUploadController` and `SyncUploadController` controllers call corresponding methods in the Azure Blob storage sdk to upload the "FileToUpload.txt" file to Blob storage.

* `SyncController`
The CreateFile class is used to generate a file content that is 10 MB in size.

* `SyncUploadController`
## Prerequisites

* `WrappedSyncController`
- Permission to create a new resource group and resources in an [Azure subscription](https://azure.com/free)
- Unix-like shell. Also available in:
- [Azure Cloud Shell](https://shell.azure.com/)
- [Windows Subsystem for Linux (WSL)](https://learn.microsoft.com/windows/wsl/install)
- [Git](https://git-scm.com/downloads)
- [.NET 8 SDK](https://dotnet.microsoft.com/download/dotnet/8.0)
- [Azure CLI](https://learn.microsoft.com/cli/azure/install-azure-cli)
- Optionally, an IDE, like [Visual Studio](https://visualstudio.microsoft.com/downloads/) or [Visual Studio Code](https://code.visualstudio.com/).

The `AsyncController`, `SyncController`, and `WrappedSyncController` WebAPI
controllers call the corresponding methods of the `FakeUserProfileService` class.
## Steps

The `AsyncUploadController` and `SyncUploadController` WebAPI controllers call
corresponding methods in the Azure Blob storage sdk to upload the "FileToUpload.txt"
file to Blob storage.
1. Clone this repository to your workstation and navigate to the working directory.

The CreateFileToUpload project is a console app that can be used to generate a file
named "FileToUpload.txt" that is 10 MB in size.
```bash
git clone https://github.com/mspnp/performance-optimization
cd SynchronousIO
```

## Configuring the project
1. Log into Azure and create an empty resource group.

The `SyncUploadController` and `AsyncUploadController` use Azure Storage to save blob data. Use the Azure Management Portal to create an Azure Storage Account and add the connection string for this account to the AzureCloudService ServiceConfiguration files.
```bash
az login
az account set -s <Name or ID of subscription>

## Deploying the project to Azure
export USER_OBJECTID=<Microsoft Entra Id user's object id>

LOCATION=eastus
RESOURCEGROUP=rg-synchronous-IO-${LOCATION}

In Visual Studio Solution Explorer, right-click the AzureCloudService project and then
click *Publish* to deploy the project to Azure.
az group create --name ${RESOURCEGROUP} --location ${LOCATION}

```

## Load testing
1. Deploy the supporting Azure resources.
It will create a storage account that only allows Managed Identity access. The User Object Id will have the Role to upload Blobs.

```bash
az deployment group create --resource-group ${RESOURCEGROUP} \
-f ./bicep/main.bicep \
-p userObjectId=${USER_OBJECTID}
```

1. Configure database connection string

On appsettings.json you need to complete with your azure account name.

```bash
"https://<storageAccountName>.blob.core.windows.net/",
```

1. Authenticate with a Microsoft Entra identity

As far the implementation is using manage identity, you need to assign the role to your [developer identity](https://learn.microsoft.com/azure/azure-functions/functions-reference?tabs=blob&pivots=programming-language-csharp#local-development-with-identity-based-connections).

1. Run proyect locally

Execute the API and then you will be able to call both endpoints

## :broom: Clean up resources

Most of the Azure resources deployed in the prior steps will incur ongoing charges unless removed.

```bash
az group delete -n ${RESOURCEGROUP} -y
```

You can use [Visual Studio Online](http://www.visualstudio.com/en-us/get-started/load-test-your-app-vs.aspx) to
load test your application.
For details of the load testing strategy for this sample, see [Load Testing][Load Testing].
## Contributions

For more realistic results, configure the user load to simulate bursts of traffic with
periods of low usage between bursts of high usage. In order to raise and lower the
user load within a load test, you will need to create a [custom load test plugin](https://msdn.microsoft.com/en-us/library/ms243153.aspx).
Please see our [Contributor guide](./CONTRIBUTING.md).

## Dependencies
This project requires Azure SDK 2.5
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact <opencode@microsoft.com> with any additional questions or comments.

[docs]: docs/SynchronousIO.md
[Load Testing]: docs/LoadTesting.md
With :heart: from Azure Patterns & Practices, [Azure Architecture Center](https://azure.com/architecture).
46 changes: 0 additions & 46 deletions SynchronousIO/SynchronousIO/.vscode/launch.json

This file was deleted.

4 changes: 0 additions & 4 deletions SynchronousIO/SynchronousIO/.vscode/settings.json

This file was deleted.

18 changes: 0 additions & 18 deletions SynchronousIO/SynchronousIO/.vscode/tasks.json

This file was deleted.

28 changes: 0 additions & 28 deletions SynchronousIO/SynchronousIO/Controllers/AsyncController.cs

This file was deleted.

46 changes: 0 additions & 46 deletions SynchronousIO/SynchronousIO/Controllers/AsyncUploadController.cs

This file was deleted.

27 changes: 0 additions & 27 deletions SynchronousIO/SynchronousIO/Controllers/SyncController.cs

This file was deleted.

Loading