|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using BenchmarkDotNet.Attributes; |
| 4 | +using BenchmarkDotNet.Jobs; |
| 5 | + |
| 6 | +namespace StackExchange.Utils.Benchmarks.Benchmarks |
| 7 | +{ |
| 8 | + [MemoryDiagnoser] |
| 9 | + [SimpleJob(RuntimeMoniker.Net472)] |
| 10 | + [SimpleJob(RuntimeMoniker.NetCoreApp31)] |
| 11 | + public class ResponseBuffering |
| 12 | + { |
| 13 | + private static string _jsonUrl = "https://jsonplaceholder.typicode.com/photos"; |
| 14 | + |
| 15 | + public class SamplePhoto |
| 16 | + { |
| 17 | + public int albumId { get; set; } |
| 18 | + public int id { get; set; } |
| 19 | + public string title { get; set; } |
| 20 | + public string url { get; set; } |
| 21 | + public string thumbnailUrl { get; set; } |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | + [Benchmark(Description = "ExpectHttpSuccess with buffering")] |
| 26 | + public async Task<bool> ExpectHttpSuccessWithBuffering() => (await Http.Request(_jsonUrl).ExpectHttpSuccess().GetAsync()).Success; |
| 27 | + |
| 28 | + [Benchmark(Description = "ExpectHttpSuccess without buffering")] |
| 29 | + public async Task<bool> ExpectHttpSuccessWithOutBuffering() => (await Http.Request(_jsonUrl).WithoutResponseBuffering().ExpectHttpSuccess().GetAsync()).Success; |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + [Benchmark(Description = "ExpectJson with buffering")] |
| 34 | + public async Task<List<SamplePhoto>> ExpectJsonWithBuffering() => (await Http.Request(_jsonUrl).ExpectJson<List<SamplePhoto>>().GetAsync()).Data; |
| 35 | + |
| 36 | + [Benchmark(Description = "ExpectJson without buffering")] |
| 37 | + public async Task<List<SamplePhoto>> ExpectJsonWithoutBuffering() => (await Http.Request(_jsonUrl).WithoutResponseBuffering().ExpectJson<List<SamplePhoto>>().GetAsync()).Data; |
| 38 | + |
| 39 | + |
| 40 | + [Benchmark(Description = "ExpectString with buffering")] |
| 41 | + public async Task<bool> ExpectStringWithBuffering() => (await Http.Request(_jsonUrl).ExpectString().GetAsync()).Success; |
| 42 | + |
| 43 | + [Benchmark(Description = "ExpectString without buffering")] |
| 44 | + public async Task<string> ExpectStringWithOutBuffering() => (await Http.Request(_jsonUrl).WithoutResponseBuffering().ExpectString().GetAsync()).Data; |
| 45 | + |
| 46 | + |
| 47 | + [Benchmark(Description = "ExpectByteArray with buffering")] |
| 48 | + public async Task<bool> ExpectByteArrayWithBuffering() => (await Http.Request(_jsonUrl).ExpectByteArray().GetAsync()).Success; |
| 49 | + |
| 50 | + [Benchmark(Description = "ExpectByteArray without buffering")] |
| 51 | + public async Task<byte[]> ExpectByteArrayWithOutBuffering() => (await Http.Request(_jsonUrl).WithoutResponseBuffering().ExpectByteArray().GetAsync()).Data; |
| 52 | + |
| 53 | + } |
| 54 | +} |
| 55 | + |
0 commit comments