From 6257a5373f033b02083fa21a3b643da5aae614a1 Mon Sep 17 00:00:00 2001 From: Ting-Yu Lin Date: Thu, 30 Jan 2020 11:18:18 +0700 Subject: [PATCH] Fix sample projects and exclude those from pack --- Agoda.LoadBalancing.sln | 2 + samples/Benchmark/Benchmark.csproj | 17 ----- samples/Benchmark/Program.cs | 75 ------------------- samples/Benchmark/RR1.cs | 31 -------- samples/Benchmark/echo.js | 22 ------ .../HttpClientFactorySample.csproj | 1 + .../Controllers/WeatherForecastController.cs | 39 ++++++++++ .../MultiTypedClient/MultiTypedClient.csproj | 12 +-- samples/MultiTypedClient/Program.cs | 16 ++-- samples/MultiTypedClient/Startup.cs | 67 +++++++++-------- samples/MultiTypedClient/WeatherForecast.cs | 15 ++++ .../appsettings.Development.json | 18 ++--- samples/MultiTypedClient/appsettings.json | 14 ++-- 13 files changed, 120 insertions(+), 209 deletions(-) delete mode 100644 samples/Benchmark/Benchmark.csproj delete mode 100644 samples/Benchmark/Program.cs delete mode 100644 samples/Benchmark/RR1.cs delete mode 100644 samples/Benchmark/echo.js create mode 100644 samples/MultiTypedClient/Controllers/WeatherForecastController.cs create mode 100644 samples/MultiTypedClient/WeatherForecast.cs diff --git a/Agoda.LoadBalancing.sln b/Agoda.LoadBalancing.sln index e2c3960..d836055 100644 --- a/Agoda.LoadBalancing.sln +++ b/Agoda.LoadBalancing.sln @@ -65,9 +65,11 @@ Global {1C23FCB4-EC50-4CD2-8823-C0413E29E580}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {1C23FCB4-EC50-4CD2-8823-C0413E29E580}.Release|Any CPU.ActiveCfg = Release|Any CPU {1C23FCB4-EC50-4CD2-8823-C0413E29E580}.Release|Any CPU.Build.0 = Release|Any CPU + {1C23FCB4-EC50-4CD2-8823-C0413E29E580}.Debug|Any CPU.Build.0 = Debug|Any CPU {33B2E81D-34AE-405F-BFDC-66E0FD2C0813}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {33B2E81D-34AE-405F-BFDC-66E0FD2C0813}.Release|Any CPU.ActiveCfg = Release|Any CPU {33B2E81D-34AE-405F-BFDC-66E0FD2C0813}.Release|Any CPU.Build.0 = Release|Any CPU + {33B2E81D-34AE-405F-BFDC-66E0FD2C0813}.Debug|Any CPU.Build.0 = Debug|Any CPU {E89EAFEC-814F-4B11-8719-3A0889F6D551}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E89EAFEC-814F-4B11-8719-3A0889F6D551}.Debug|Any CPU.Build.0 = Debug|Any CPU {E89EAFEC-814F-4B11-8719-3A0889F6D551}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/samples/Benchmark/Benchmark.csproj b/samples/Benchmark/Benchmark.csproj deleted file mode 100644 index a22a02b..0000000 --- a/samples/Benchmark/Benchmark.csproj +++ /dev/null @@ -1,17 +0,0 @@ - - - - Exe - netcoreapp2.1 - - - - - - - - - - - - diff --git a/samples/Benchmark/Program.cs b/samples/Benchmark/Program.cs deleted file mode 100644 index a8fe0be..0000000 --- a/samples/Benchmark/Program.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System; -using System.Diagnostics; -using System.Linq; -using System.Net.Http; -using System.Threading.Tasks; -using Agoda.Frameworks.Http; - -namespace Benchmark -{ - class Program - { - static string baseUrl = "http://localhost:3000"; - static int testCount = 1000; - static void Main(string[] args) - { - var native = new HttpClient(); - Console.WriteLine("Native"); - RunTest(testCount, async url => - { - return await native.PostAsync(baseUrl, new StringContent("")); - }); - - var rr1 = RR1.CreateClient(baseUrl); - Console.WriteLine("RR1"); - RunTest(testCount, url => RR1.Request(rr1, url)); - - var newrrClient = new RandomUrlHttpClient(new[] { baseUrl }); - Console.WriteLine("RR3"); - RunTest(testCount, async url => - { - return await newrrClient.PostAsync(url, new StringContent("")); - }); - } - - private static void RunTest(int count, Func> get) - { - var requests = Enumerable.Range(0, count).Select(CreateRequest).ToList(); - var stopwatch = Stopwatch.StartNew(); - var requestTasks = requests.Select(r => MakeRequest(r, get)).ToArray(); - Task.WaitAll(requestTasks); - stopwatch.Stop(); - - Console.WriteLine("Sum: " + requests.Sum(x => x.DurationMs)); - Console.WriteLine("Avg: " + requests.Average(x => x.DurationMs)); - Console.WriteLine("Min: " + requests.Min(x => x.DurationMs)); - Console.WriteLine("Max: " + requests.Max(x => x.DurationMs)); - } - - private static async Task MakeRequest(Request request, Func> get) - { - var stopwatch = Stopwatch.StartNew(); - var response = await get(request.Url); - stopwatch.Stop(); - - // save request duration and response - request.DurationMs = stopwatch.ElapsedMilliseconds; - // request.ResponseId = ParseResponse(response); - } - - private static Request CreateRequest(int i) - { - return new Request() - { - Url = "/" - }; - } - - - } - public class Request - { - public string Url { get; set; } - public long DurationMs { get; set; } - } -} diff --git a/samples/Benchmark/RR1.cs b/samples/Benchmark/RR1.cs deleted file mode 100644 index 7fd2eba..0000000 --- a/samples/Benchmark/RR1.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Agoda.RoundRobin; - -namespace Benchmark -{ - public static class RR1 - { - public static HttpClient CreateClient(string baseUrl) - { - var httpClient = new HttpClient(new HttpClientParameters - { - Name = "", - ContentType = "application/json", - //Settings = new List() - //{ - // new ServerSettings("") - //}, - Urls = new[] { baseUrl }, - Timeout = 100, - Retry = 3 - }); - return httpClient; - } - public static async Task Request(HttpClient client, string url) - { - return await client.ExecuteAsync(url, new byte[0], false, new ErrorList()); - } - } -} diff --git a/samples/Benchmark/echo.js b/samples/Benchmark/echo.js deleted file mode 100644 index cefbf3b..0000000 --- a/samples/Benchmark/echo.js +++ /dev/null @@ -1,22 +0,0 @@ -const http = require("http"); - -const hostname = "0.0.0.0"; -const port = 3000; - -const server = http.createServer((req, res) => { - console.log(`\n${req.method} ${req.url}`); - console.log(req.headers); - - req.on("data", function(chunk) { - console.log("BODY: " + chunk); - }); - - res.statusCode = 200; - res.setHeader("Content-Type", "text/plain"); - res.end("Hello World\n"); -}); - -server.listen(port, hostname, () => { - console.log(`Server running at http://localhost:${port}/`); -}); - diff --git a/samples/HttpClientFactorySample/HttpClientFactorySample.csproj b/samples/HttpClientFactorySample/HttpClientFactorySample.csproj index 3e7cf0a..427d060 100644 --- a/samples/HttpClientFactorySample/HttpClientFactorySample.csproj +++ b/samples/HttpClientFactorySample/HttpClientFactorySample.csproj @@ -3,6 +3,7 @@ Exe netcoreapp2.1 + false diff --git a/samples/MultiTypedClient/Controllers/WeatherForecastController.cs b/samples/MultiTypedClient/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..3d29a86 --- /dev/null +++ b/samples/MultiTypedClient/Controllers/WeatherForecastController.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; + +namespace MultiTypedClient.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet] + public IEnumerable Get() + { + var rng = new Random(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = rng.Next(-20, 55), + Summary = Summaries[rng.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git a/samples/MultiTypedClient/MultiTypedClient.csproj b/samples/MultiTypedClient/MultiTypedClient.csproj index b725391..cb00a48 100644 --- a/samples/MultiTypedClient/MultiTypedClient.csproj +++ b/samples/MultiTypedClient/MultiTypedClient.csproj @@ -1,19 +1,11 @@ - netcoreapp2.1 - InProcess + netcoreapp3.1 - - all -true - - - - - + diff --git a/samples/MultiTypedClient/Program.cs b/samples/MultiTypedClient/Program.cs index 343754f..f9d6271 100644 --- a/samples/MultiTypedClient/Program.cs +++ b/samples/MultiTypedClient/Program.cs @@ -1,11 +1,10 @@ -using System; +using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace MultiTypedClient @@ -14,11 +13,14 @@ public class Program { public static void Main(string[] args) { - CreateWebHostBuilder(args).Build().Run(); + CreateHostBuilder(args).Build().Run(); } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup(); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } } diff --git a/samples/MultiTypedClient/Startup.cs b/samples/MultiTypedClient/Startup.cs index 33e7678..2c02971 100644 --- a/samples/MultiTypedClient/Startup.cs +++ b/samples/MultiTypedClient/Startup.cs @@ -1,9 +1,9 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; -using System.Net; -using System.Net.Http; -using System.Net.Http.Headers; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -11,8 +11,8 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; namespace MultiTypedClient { @@ -28,44 +28,47 @@ public Startup(IConfiguration configuration) // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); + services.AddControllers(); + services.AddHttpClient("stackexchange", c => + { + c.DefaultRequestHeaders.Add("Accept", "application/json"); + c.DefaultRequestHeaders.Add("User-Agent", "agoda.frameworks.http.sample"); + }) + .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() + { + // Compression is required by StackExchange + // https://api.stackexchange.com/docs/compression + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate + }); + + services.AddHttpClient("github", c => + { + c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json"); + c.DefaultRequestHeaders.Add("User-Agent", "agoda.frameworks.http.sample"); + c.DefaultRequestHeaders.Authorization = + new AuthenticationHeaderValue("token", "f762b2e8137a0b4979c4f20410ed82f32c167336"); + }); - services.AddHttpClient("stackexchange", c => - { - c.DefaultRequestHeaders.Add("Accept", "application/json"); - c.DefaultRequestHeaders.Add("User-Agent", "agoda.frameworks.http.sample"); - }) - .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() - { - // Compression is required by StackExchange - // https://api.stackexchange.com/docs/compression - AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate - }); - - services.AddHttpClient("github", c => - { - c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json"); - c.DefaultRequestHeaders.Add("User-Agent", "agoda.frameworks.http.sample"); - c.DefaultRequestHeaders.Authorization = - new AuthenticationHeaderValue("token", "f762b2e8137a0b4979c4f20410ed82f32c167336"); - }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } - else - { - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseHsts(); - } app.UseHttpsRedirection(); - app.UseMvc(); + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); } } } diff --git a/samples/MultiTypedClient/WeatherForecast.cs b/samples/MultiTypedClient/WeatherForecast.cs new file mode 100644 index 0000000..9dcb8d2 --- /dev/null +++ b/samples/MultiTypedClient/WeatherForecast.cs @@ -0,0 +1,15 @@ +using System; + +namespace MultiTypedClient +{ + public class WeatherForecast + { + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string Summary { get; set; } + } +} diff --git a/samples/MultiTypedClient/appsettings.Development.json b/samples/MultiTypedClient/appsettings.Development.json index e7d2836..dba68eb 100644 --- a/samples/MultiTypedClient/appsettings.Development.json +++ b/samples/MultiTypedClient/appsettings.Development.json @@ -1,9 +1,9 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } - } -} +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/samples/MultiTypedClient/appsettings.json b/samples/MultiTypedClient/appsettings.json index c05d023..81ff877 100644 --- a/samples/MultiTypedClient/appsettings.json +++ b/samples/MultiTypedClient/appsettings.json @@ -1,8 +1,10 @@ { - "Logging": { - "LogLevel": { - "Default": "Warning" - } - }, - "AllowedHosts": "*" + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" }