Monday, 22 June 2020

Asp.net Core Performance testing using Bombardier

Before staring this blog, i would like to describe the basic of DotNet tool, it will not take more than minutes.

dotnet tools

To simplify .net core work by using cli on Machine or in Path we use dotnet tools.
Ex:
  • dotnet-counters
  • dotnet-ef
  • dotnet-reportgenerator-globaltool
Basic commands:
  • dotnet tool install --global <PACKAGE_NAME>
  • dotnet tool list -g|--global
  • dotnet tool run <COMMAND NAME>
  • dotnet tool uninstall <PACKAGE_NAME> -g|--global
  • dotnet tool update <PACKAGE_NAME> -g|--global
Let's have a look on a tool and It's uses:

dotnet-counters

Is used to monitor and collect diagnostic info of a running process. More could be found here on Stefan Geiger blog and Official Microsoft site.
Installation: dotnet tool install --global dotnet-counters
Monitoring: dotnet counters monitor --process-id 17264 --providers Microsoft.AspNetCore.Hosting System.Runtime

Now its good to start performance testing part from main topic. As we already know how to check various counter matrices using dotnet-counters while we will be using Bombardier.

Bombardier, Is an open source tools to make flood of http1/http2 request with various options using cli. It is written in GO programming, thus we required GO executable to understand GO command.

Installation:
go get -u github.com/codesenberg/bombardier

Execution:
bombardier -c 125 -n 10000 http://localhost:51976/api_documentation/index/index.html

Must have a look on powerful Bombardier's command.

After successful execution we can find statistics like this







Friday, 27 March 2020

Azure Cosmos Db Implementation with Dotnetcore and SQL Api

Simple step by step implementation of Cosmos DB with SQL Api in Dotnet core application. Official explanation could be find here.

Go to Azure Portal, search cosmos and create a subscription in specific resource group  with default/custom setting. I am using:
a. Subscription: Free Trial
b. Resource group: FreeStorageDemo
c. API: Core SQL
d. Account Name: cosmossqlapi



After successful creation of storage, get Keys:
a. URI
b. PRIMARY KEY
c. SECONDARY KEY
d. PRIMARY CONNECTION STRING
e. SECONDARY CONNECTION STRING
Mainly we will be using a and b in our case.


Either we can create Database manually through portal, or can use programming language. Here I am creating a simple dotnet core web api which will automatically create database and its containers with partition. Codebase for Cosmos db implementation with dotnet core api is available here on GitHub.

I have added Nuget package "Microsoft.Azure.Cosmos"


After running the endpoint in demo app, we can verified that Collection has been created under database in Azure portal


 And also verified that collection contains items.


Further we could fetch data by using API.



Note:
We can use _etag for the data consistency by using AccessConditionType  in RequestOption.

Tuesday, 18 February 2020

DotnetCore GraphQL implementation in WebApi

GraphQL is a Modern API architecture that use some standard query to make HTTP request.
You can explore more about GrapQL on official website. In this post we will described how we can create GraplQL API in existing .net core API. So that we will have both Rest and GraphQL within same App.



In GraphQL, API consist implementation of

  • Schema : Contains definition of
    • Query : That is used to fetch data.
    • Mutation : That is used to manipulate data
  • Types : Define data model like DTO.

To implement all these we would require following packages, which you can download from Nuget:

  <ItemGroup>
    <PackageReference Include="GraphQL" Version="3.0.0-preview-1352" />
    <PackageReference Include="GraphQL.Server" Version="1.7.0.1" />
    <PackageReference Include="GraphQL.Server.Core" Version="3.5.0-alpha0027" />
    <PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="3.5.0-alpha0027" />
    <PackageReference Include="GraphQL.Server.Ui.Playground" Version="3.5.0-alpha0027" />
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
  </ItemGroup>

Notice we have added GraphQL.Server.Ui.Playground, which we will be using for documentation like swagger and others. There we can explore all queries, mutations and types. As well as we can play with these in editor.

So let's start exploring  a solution from GitHub  in which we have projects:
1. DotNetCoreGraphQl.Order.WebApi
2. DotNetCoreGraphQl.Order.ViewModels
3. DotNetCoreGraphQl.Order.Providers
4. DotNetCoreGraphQl.Order.Ql

After having implementation we can make query and mutation call to endpoint /graphql in exposed UI http://localhost:60132/ui/playground

Query
{
  "query" {
    "orderDetails(orderId": "12)",
    {
      "orderId",
      "product" {
        "name"
      },
      "totalPrice"
    }
  }


Mutation
mutation {
  addToCart(cart: {
    "quantity": 1,
    "productId": 212,
    "remarks": "Its too good"
  }) {
    orderId,
    product{
      name
    },
    totalPrice
  }
}

Key Points:
1. Each ObjectGraphType must be register.
2. Use EnableMetrics = true/false to get extensions node in response.
3. Use app.UseGraphQL() to change default endpoint (/graphql)
4. Use app.UseGraphQLPlayground() to change UI playground url (/ui/playground)

Demo on GitHub

Monday, 13 January 2020

Text to ASCII Art Generator Nuget

Create ASCII art from text with various settings like set text and background text with printing speed in .net. You might found various online Text to ASCII Art Generator, but now can do it by yourself in .net. Just Import TextToAsciiArt.dll and write some snippet and done!

Install-Package TextToAsciiArt -Version 1.0.0


Limitation: Only A-Z and 0-9 accepted.

Snippet:
using System;
using TextToAsciiArt;
class Program
{
    static void Main(string[] args)
    {
        IArtWriter writer = new ArtWriter();
 
        //1
        writer.WriteConsole("happy lohri 2020", null);
 
        //2
        var settings = new ArtSetting
        {
            ConsoleSpeed = 100,
            IsBreakSpace = true,
            Text = "|",
            BgText = "_"
        };
        writer.WriteConsole("happy lohri 2020", settings);
 
        //3
        var data = writer.WriteString("My name is Raj");
        Console.WriteLine(data);
    }
}
 

Output:


Running demo here.
Tags:

  • Text to ASCII Art Generator
  • Text Art
  • CMD Fancy Text
  • Console Text Art
  • Console Art

Friday, 10 January 2020

Create Azure CI/CD pipeline for building and deploying Azure Functions

In this post we will see, how we create an Azure CI/CD pipeline for building and deploying Azure Functions. We have already covered how we can create an Azure Function locally.

Now have a quick look on steps, which we will be performing here:

  1. Create Azure storage account in Azure portal: As AZ Function pricing is based on computation, we don’t have to pay much for running as long service. We can simply keep it in a storage account. Whenever any trigger would fire, it will start processing.
  2. Create an Function App in Azure portal: It will be treated as the service and we can define this service will be processing on which platform like OS or Container.
  3. Create CI build pipeline: To build and publish as zip artifact.
  4. Create CD release pipeline: To deploy artifact as service which we have created on azure portal.
  5. Verify function triggered successfully or not.


Let's see step by steps with images:

1. Create Azure storage account in Azure portal





2. Create an Function App in Azure portal





3. Create CI build pipeline




4. Create CD release pipeline








5. Verify function triggered successfully or not





Reference:
https://www.youtube.com/watch?v=zN0W2IPF1hI
https://www.asptricks.net/2018/11/azure-functions-basic-information.html


Friday, 3 January 2020

Create Azure CD release pipeline to deploy images on Kubernetes | Docker part 5

We already covered how to build and push Docker image to Docker hub or azure container registry through azure DevOps build pipeline. So we have:
  1. GitHub repo.
  2. Azure devops build pipeline with Container Build and Push tasks.
  3. DockerHub repository. (Public)
  4. Kubernetes service created and running.
Now what we have TODO now:
  1. Create deployment yaml file that required for Kubernetes deployment.
  2.  Add some more task in Build pipeline.
  3. Get Devops artifact.
  4. Create a Release pipeline.
  5. Deploy and verify pods on local Kubernetes dashboard.
Instead of doing TODO, if you want to deploy containers manually on Kubernetes you can follow YouTube video of Houssem Dellai.

Let's have a look on above TODO listed points, one by one. Special thanks to Microsoft Visual Studio, who described nicely on his YouTube video.

Create deployment yaml file that required for Kubernetes deployment.
Get the quick reference from Microsoft Official for creation of deployment file. And create simple kubernetes-deployement.yml in project folder with below content:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: mysamplekubapps
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: demo-kub-apps
    spec:
      containers:
        - name: mssample-services-app
          image: rajkrs/docker_linux_redis_netcore:latest
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
    name: demo-kub-apps
spec:
  ports:
    - name: http-port
      port: 80
      targetPort: 80
  selector:
    app: demo-kub-apps
  type: LoadBalancer


Add some more task in Build pipeline.
Because we will be regularly using build id as version variable, so let's create a variable

We want to change deployment file in such a way that, for each build image rajkrs/docker_linux_redis_netcore:latest would become  rajkrs/docker_linux_redis_netcore:<1.100>

So we are adding one CLI script task that will take care of latest version replacement.


Get Artifact.
And will publish changed deployment yml as artifact.

Create a Release pipeline.
We just create a release pipeline and assign the source build pipeline which we have created earlier.




To create Kubernetes service connection, click on New


Make sure in above image we have correct Manifests *. 

Now we have created the release pipeline, you may enable continues deployment  from Artifacts trigger option.


Deploy and verify pods on local Kubernetes dashboard.
Let's got to build and Queue one new build. Verify build completred.


Verify we have pushed tag 1.100 in Dockerhub.
Verify release pipeline is in progress..


Verify release completed successfully.

Verify we have Pods in Kubernetes.

Verify app had assigned with external IP: 52.154.232.146:80


And we are able to browse app. 🙂


Don’t forgot to verify azure subscription credit. 🤯









Thursday, 2 January 2020

Create Azure Kubernetes service and view dashboard | Docker part 4

Where by Using Docker, We build and run containers, and store and share container images.

There by using Kubernetes we orchestrates and manages the containerized applications that Docker creates. It also provides the infra needed to deploy and run those applications on a cluster of machines. Know more about Kubernetes here on Microsoft Official.





Let's know simple steps by following which we will be create Kubernetes serves and kubernetes dashboard.
Kubernetes dashboard will run locally.










PS C:\Users\raj.kumar1> az login
You have logged in. Now let us find all the subscriptions to which you have access...
[
  {
    "cloudName": "AzureCloud",
    "id": "88b268f4-b6e6-432a-84c7-64cc125a9f11",
    "isDefault": true,
    "name": "Free Trial",
    "state": "Enabled",
    "tenantId": "0fec8578-a553-4c8f-839b-a6d83f5932a3",
    "user": {
      "name": "mr.raj111@hotmail.com",
      "type": "user"
    }
  }
]

PS C:\Users\raj.kumar1> kubectl create clusterrolebinding kubernetes-dashboard --clusterrole=cluster-admin --serviceaccount=kube-system:kubernetes-dashboard

clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created

PS C:\Users\raj.kumar1> az aks get-credentials --resource-group FreeKubernetesResource --name KubApps
Merged "KubApps" as current context in C:\Users\raj.kumar1\.kube\config

PS C:\Users\raj.kumar1> az aks browse --resource-group FreeKubernetesResource --name KubApps
Merged "KubApps" as current context in C:\Users\RAJ~1.KUM\AppData\Local\Temp\tmp3_vvoxr5
Proxy running on http://127.0.0.1:8001/
Press CTRL+C to close the tunnel...