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