-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTechmeServices.cs
171 lines (139 loc) · 4.57 KB
/
TechmeServices.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.Threading;
using ServiceStack;
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;
using ServiceStack.Redis;
namespace Techempower.ServiceInterface
{
[Route("/json")]
public class Hello
{
public string name { get; set; }
}
public class HelloResponse
{
public string message { get; set; }
}
[Route("/db")]
public class Db { }
[Route("/redis")]
public class Redis { }
[Route("/queries")]
public class Queries
{
public int queries { get; set; }
}
[Route("/fortunes")]
public class Fortunes { }
[Route("/updates")]
public class Updates
{
public int queries { get; set; }
}
[Route("/plaintext")]
public class PlainText { }
public class World
{
public int id { get; set; }
public int randomNumber { get; set; }
}
public class Fortune : IComparable<Fortune>
{
[AutoIncrement]
public int id { get; set; }
public string message { get; set; }
public int CompareTo(Fortune other)
{
return message.CompareTo(other.message);
}
}
public class TechmeServices : Service
{
readonly Random rand = new Random();
public object Any(Hello request)
{
return new HelloResponse { message = "Hello, " + (request.name ?? "World") + "!" };
}
public object Any(Db request)
{
return Db.SingleById<World>(rand.Next(0, 10000) + 1);
}
public object Any(Redis request)
{
return Redis.GetById<World>(rand.Next(0, 10000) + 1);
}
public object Any(Queries request)
{
var queries = Math.Max(1, Math.Min(500, request.queries));
var worlds = new World[queries];
for (int i = 0; i < queries; i++)
{
worlds[i] = Db.SingleById<World>(rand.Next(0, 10000) + 1);
}
return worlds;
}
public List<Fortune> Any(Fortunes request)
{
var fortunes = new List<Fortune>();
fortunes.AddRange(Db.Select<Fortune>());
fortunes.Add(new Fortune { id = 0, message = "Additional fortune added at request time." });
fortunes.Sort();
return fortunes;
}
public void Any(Updates request)
{
var queries = Math.Max(1, Math.Min(500, request.queries));
var worlds = new World[queries];
for (int i = 0; i < queries; i++)
{
worlds[i] = Db.SingleById<World>(rand.Next(0, 10000) + 1);
worlds[i].randomNumber = rand.Next(0, 10000) + 1;
}
Db.UpdateAll(worlds);
}
[AddHeader(ContentType = MimeTypes.PlainText)]
public string Any(PlainText request)
{
return "Hello, World";
}
public void Any(Reset request)
{
var fullReset = request.Level == null || request.Level == "full";
if (fullReset || request.Level == "db")
{
Db.DropAndCreateTable<World>();
var worlds = 10000.Times(i => new World { id = i, randomNumber = rand.Next(0, 10000) + 1 });
Db.InsertAll(worlds);
if (TryResolve<IRedisClientsManager>() != null)
{
Redis.FlushAll();
Redis.StoreAll(worlds);
}
Db.DropAndCreateTable<Fortune>();
new[] {
"A budget is just a method of worrying before you spend money, as well as afterward.",
"A classic is something that everybody wants to have read and nobody wants to read.",
"A conclusion is simply the place where someone got tired of thinking.",
"A diplomat is someone who can tell you to go to hell in such a way that you will look forward to the trip.",
"A psychiatrist is a person who will give you expensive answers that your wife will give you for free.",
}.Each(x => Db.Insert(new Fortune { message = x }));
}
if (fullReset || request.Level == "gc")
{
GC.Collect();
}
if (fullReset || request.Level == "pause")
{
Thread.Sleep(2000);
}
}
}
[Route("/reset")]
[Route("/reset/{Level}")]
public class Reset
{
public string Level { get; set; }
}
}