|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using Microsoft.AspNetCore.Mvc; |
| 8 | + |
| 9 | +namespace Benchmarks.Controllers |
| 10 | +{ |
| 11 | + [ApiController] |
| 12 | + public class JsonController : Controller |
| 13 | + { |
| 14 | + private static readonly List<Entry> _entries2k = Entry.Create(8).ToList(); |
| 15 | + private static List<Entry> _entriesNk; |
| 16 | + private static int previousSizeInBytes; |
| 17 | + |
| 18 | + [HttpGet("/json-helloworld")] |
| 19 | + [Produces("application/json")] |
| 20 | + public object Json() |
| 21 | + { |
| 22 | + return new { message = "Hello, World!" }; |
| 23 | + } |
| 24 | + |
| 25 | + [HttpGet("/json2k")] |
| 26 | + [Produces("application/json")] |
| 27 | + public List<Entry> Json2k() => _entries2k; |
| 28 | + |
| 29 | + [HttpGet("/jsonNbytes/{sizeInBytes}")] |
| 30 | + [Produces("application/json")] |
| 31 | + public List<Entry> JsonNk([FromRoute] int sizeInBytes) |
| 32 | + { |
| 33 | + if (_entriesNk is null || sizeInBytes != previousSizeInBytes) |
| 34 | + { |
| 35 | + var numItems = sizeInBytes / 340; // ~ 340 bytes per item |
| 36 | + _entriesNk = Entry.Create(numItems); |
| 37 | + previousSizeInBytes = sizeInBytes; |
| 38 | + } |
| 39 | + |
| 40 | + return _entriesNk; |
| 41 | + } |
| 42 | + |
| 43 | + [HttpPost("/jsoninput")] |
| 44 | + [Consumes("application/json")] |
| 45 | + public ActionResult JsonInput([FromBody] List<Entry> entry) => Ok(); |
| 46 | + } |
| 47 | + |
| 48 | + public partial class Entry |
| 49 | + { |
| 50 | + public Attributes Attributes { get; set; } |
| 51 | + public string ContentType { get; set; } |
| 52 | + public string Id { get; set; } |
| 53 | + public bool Managed { get; set; } |
| 54 | + public string[] Tags { get; set; } |
| 55 | + |
| 56 | + public static List<Entry> Create(int n) |
| 57 | + { |
| 58 | + var baseDateTime = new DateTimeOffset(new DateTime(2019, 04, 23)); |
| 59 | + |
| 60 | + return Enumerable.Range(1, n).Select(i => new Entry |
| 61 | + { |
| 62 | + Attributes = new Attributes |
| 63 | + { |
| 64 | + Created = baseDateTime.AddDays(i), |
| 65 | + Enabled = true, |
| 66 | + Expires = baseDateTime.AddDays(i).AddYears(1), |
| 67 | + NotBefore = baseDateTime, |
| 68 | + RecoveryLevel = "Purgeable", |
| 69 | + Updated = baseDateTime.AddSeconds(i), |
| 70 | + }, |
| 71 | + ContentType = "application/xml", |
| 72 | + Id = "https://benchmarktest.id/item/value" + i, |
| 73 | + Tags = new[] { "test", "perf", "json" }, |
| 74 | + }).ToList(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public partial class Attributes |
| 79 | + { |
| 80 | + public DateTimeOffset Created { get; set; } |
| 81 | + public bool Enabled { get; set; } |
| 82 | + public DateTimeOffset Expires { get; set; } |
| 83 | + public DateTimeOffset NotBefore { get; set; } |
| 84 | + public string RecoveryLevel { get; set; } |
| 85 | + public DateTimeOffset Updated { get; set; } |
| 86 | + } |
| 87 | +} |
0 commit comments