-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChallenge_2.linq
114 lines (97 loc) · 3.2 KB
/
Challenge_2.linq
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<Query Kind="Program">
<Reference><RuntimeDirectory>\System.Net.Http.dll</Reference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>System.Net.Http</Namespace>
<Namespace>System.Net.Http.Headers</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<DisableMyExtensions>true</DisableMyExtensions>
</Query>
void Main()
{
const string GetUrl = "http://84.200.109.239:5000/challenges/2/";
const string PostUrl = "http://84.200.109.239:5000/solutions/2/";
var counter = 100;
double gesamt = 0.0;
var httpClient = new HttpClient();
var handler = new HttpClientHandler(httpClient);
var stopwatch = new Stopwatch();
for (int i = 0; i < counter; i++)
{
var responseGet = handler.Get(GetUrl);
var resultGet = responseGet.Content.ReadAsStringAsync().Result;
// Benchmark
stopwatch.Restart();
var resDataGet = JsonConvert.DeserializeObject<Data>(resultGet);
var indexToPost = resDataGet.Werte.ToList().IndexOf(resDataGet.Key);
stopwatch.Stop();
gesamt += stopwatch.Elapsed.TotalMilliseconds;
if ((indexToPost < 0) == false)
{
var solResult = new SolutionResult() { Token = indexToPost };
var responsePost = handler.Post(PostUrl, solResult.AsJson());
var resultPost = responsePost.Content.ReadAsStringAsync().Result;
// resultPost.Dump("Post Response");
}
else { "Fehler!!".Dump("Post Response"); }
}
$"{gesamt} ms".Dump();
$"Durchschnitt bei {counter} Durchläufe: { gesamt / counter} ms".Dump();
/* 24,1051 ms, Durchschnitt bei 100 Durchläufe: 0,241051 ms */
}
public class Data
{
[JsonProperty("k")]
public long Key { get; set; }
[JsonProperty("list")]
public IEnumerable<long> Werte { get; set;}
}
public class SolutionResult
{
[JsonProperty("token")]
public int Token { get; set; }
}
public class HttpClientHandler : IHttpHandler
{
private readonly HttpClient client;
public HttpClientHandler(HttpClient client)
{
this.client = client;
}
public HttpResponseMessage Get(string url)
{
return GetAsync(url).Result;
}
public HttpResponseMessage Post(string url, HttpContent content)
{
return PostAsync(url, content).Result;
}
public async Task<HttpResponseMessage> GetAsync(string url)
{
return await client.GetAsync(url);
}
public async Task<HttpResponseMessage> PostAsync(string url, HttpContent content)
{
return await client.PostAsync(url, content);
}
}
public interface IHttpHandler
{
HttpResponseMessage Get(string url);
HttpResponseMessage Post(string url, HttpContent content);
Task<HttpResponseMessage> GetAsync(string url);
Task<HttpResponseMessage> PostAsync(string url, HttpContent content);
}
public static class Extensions
{
public static StringContent AsJson(this object o)
=> new StringContent(JsonConvert.SerializeObject(o), Encoding.UTF8, "application/json");
public static int IndexOf<T>(this IEnumerable<T> obj, T value, IEqualityComparer<T> comparer)
{
comparer = comparer ?? EqualityComparer<T>.Default;
var found = obj
.Select((a, i) => new { a, i })
.FirstOrDefault(x => comparer.Equals(x.a, value));
return found == null ? -1 : found.i;
}
}