-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_prototype.cpp
261 lines (220 loc) · 6.89 KB
/
event_prototype.cpp
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// EventPrototype.cpp : Defines the entry point for the console application.
//
#include <string>
#include <map>
#include <vector>
#include <set>
#include <chrono>
#include <algorithm>
#include <atomic>
#include <thread>
//#include <omp.h>
#pragma region Helpers
double timeGetCurrentSeconds()
{
long long v = std::chrono::high_resolution_clock::now().time_since_epoch().count();
double dv = static_cast<double>(v);
dv /= 1000000000.0;
return dv;
}
#pragma endregion
// * EVENT SYSTEM
struct SEvent
{
std::set<std::string> topics;
void* payload = nullptr;
};
struct SEventConsumer;
typedef void(*SEventConsumerHandler)(const SEventConsumer*, const SEvent*);
struct SEventConsumer
{
void* custom;
std::set<std::string> topics;
SEventConsumerHandler handler;
bool operator < (const SEventConsumer& o) const
{
if(topics != o.topics)
return topics < o.topics;
return this < &o;
}
};
bool operator < (const std::set<std::string>& a, const SEventConsumer& b)
{
return a < b.topics;
}
bool operator < (const SEventConsumer& a, const std::set<std::string>& b)
{
return a.topics < b;
}
struct SEventConsumerCache
{
std::map<std::string, SEventConsumerCache> keyword;
std::vector<int> leaf;
};
struct SEventState
{
std::vector<SEventConsumer> consumers;
// tag caches
SEventConsumerCache cache;
};
int eventConsumerCreate(SEventState& state, const std::set<std::string>& topics, SEventConsumerHandler handler, void* custom=nullptr)
{
SEventConsumer consumer;
consumer.topics = topics;
consumer.handler = handler;
consumer.custom = custom;
state.consumers.push_back(consumer);
return -1;
}
void eventStateCompile(SEventState& state)
{
int i = 0;
for (auto& consumer : state.consumers)
{
SEventConsumerCache* current = &state.cache;
for (auto& topic : consumer.topics)
{
current = ¤t->keyword[topic];
}
current->leaf.push_back(i++);
}
}
// Slow O(n^2) version.
void eventEmit(SEventState& state, const SEvent& ev)
{
for (auto it=state.consumers.begin(); it!=state.consumers.end(); it++)
{
for (auto& topic : it->topics)
{
if (ev.topics.find(topic) == ev.topics.end())
goto skip_emit;
}
it->handler(&(*it), &ev);
skip_emit:
0;
}
}
// Fast but incorrect version. Doesn't work when tags don't match fully.
void eventEmitBtree(SEventState& state, const SEvent& ev)
{
auto itBegin = std::lower_bound(state.consumers.begin(), state.consumers.end(), ev.topics);
auto itEnd = std::upper_bound(state.consumers.begin(), state.consumers.end(), ev.topics);
for (auto it = itBegin; it != itEnd; it++)
{
it->handler(&(*it), &ev);
}
}
void eventEmitRecurtree(SEventState& state, const SEvent& ev)
{
std::vector<int> consider;
consider.insert(consider.end(), state.cache.leaf.begin(), state.cache.leaf.end());
for (auto itKeywordBase = ev.topics.begin(); itKeywordBase != ev.topics.end(); itKeywordBase++)
{
auto itKeyword = itKeywordBase;
SEventConsumerCache* current = &state.cache;
while (itKeyword != ev.topics.end())
{
auto it = current->keyword.find(*(itKeyword++));
if (it == current->keyword.end())
continue; // chain ends here
current = &(it->second);
consider.insert(consider.end(), current->leaf.begin(), current->leaf.end());
}
}
std::sort(consider.begin(), consider.end());
int lastidx = -1;
for (auto idx : consider)
{
if (idx == lastidx)
continue;
lastidx = idx;
state.consumers[idx].handler(&state.consumers[idx], &ev);
}
}
template< class T>
int testValidityPermutation(std::vector<std::set<std::string>> consumer, std::set<std::string> event, const T& emit)
{
SEventState state;
int numHits = 0;
for (auto& it : consumer)
{
eventConsumerCreate(state, it, [](const SEventConsumer* consumer, const SEvent* ev) {
int* v = ((int*)consumer->custom);
(*v)++;
}, &numHits);
}
eventStateCompile(state);
SEvent e;
e.topics = event;
emit(state, e);
return numHits;
}
template <class T>
bool testValidity(const T& eventFunc)
{
#define test(x) if((x) == false) { printf("Test failed: %s\n", #x); ret = false; } else { printf("Test succeeded: %s\n", #x); }
printf("-------------------------\n");
bool ret = true;
test(testValidityPermutation({ { } }, { "hello" }, eventFunc) == 1);
test(testValidityPermutation({ { "hello" } }, { "hello" }, eventFunc) == 1);
test(testValidityPermutation({ { "hello", "test" } }, { "hello" }, eventFunc) == 0);
test(testValidityPermutation({ { "hello" } }, { "hello", "test" }, eventFunc) == 1);
test(testValidityPermutation({ { "hello", "test" } }, { "hello", "test" }, eventFunc) == 1);
test(testValidityPermutation({ { "hello", "baby", "bomb" } }, { "hello", "baby", "got", "an", "atom", "bomb" }, eventFunc) == 1);
test(testValidityPermutation({ { "hello", "bomb", "baby" }, {"atom", "baby"} }, { "hello", "baby", "got", "an", "atom", "bomb" }, eventFunc) == 2);
return ret;
#undef test
}
template <class T>
void testProfile(const T& eventFunc)
{
printf("-------------------------\n");
double timeElapsed;
double timeConsumerCreateStart = timeGetCurrentSeconds();
int numConsumerIds = 10000;
int numConsumers = 10000;
int numEvents = 100000;
std::atomic<int> numHits = 0;
SEventState state;
// Setup consumer states.
for (int i = 0; i < numConsumers; i++)
{
std::set<std::string> set;
set.insert("type=log");
int group = i % numConsumerIds;
set.insert("id=" + std::to_string(group));
eventConsumerCreate(state, set, [](const SEventConsumer* consumer, const SEvent* ev) {
std::atomic<int>* v = ((std::atomic<int>*)consumer->custom);
++(*v);
}, &numHits);
}
printf("Profiler: Created consumers in %f seconds.\n", timeGetCurrentSeconds() - timeConsumerCreateStart);
// Compile & optimize tree
double timeConsumerOptimize = timeGetCurrentSeconds();
eventStateCompile(state);
printf("Profiler: Created optimized structure for consumers in %f seconds.\n", timeGetCurrentSeconds() - timeConsumerOptimize);
// Start emitting events.
double timeConsumerEventEmit = timeGetCurrentSeconds();
SEvent e;
e.topics.insert("type=log");
e.topics.insert("id=100");
e.topics.insert("cat");
//#pragma omp parallel for
for (int i = 0; i < numEvents; i++)
{
eventFunc(state, e);
}
timeElapsed = timeGetCurrentSeconds() - timeConsumerEventEmit;
printf("Profiler: Emitted (%d) events in %f seconds (events/sec: %f).\n", numEvents, timeElapsed, ((double)numEvents) / timeElapsed);
printf("Num hits: %d\n", numHits.fetch_add(0));
}
int main()
{
testValidityPermutation({ { "hello", "bomb", "baby" },{ "atom", "baby" } }, { "hello", "baby", "got", "an", "atom", "bomb" }, [](SEventState& state, SEvent& ev) { eventEmitRecurtree(state, ev); });
testValidity([](SEventState& state, SEvent& ev) { eventEmit(state, ev); });
testValidity([](SEventState& state, SEvent& ev) { eventEmitRecurtree(state, ev); });
testProfile([](SEventState& state, SEvent& ev) { eventEmit(state, ev); });
testProfile([](SEventState& state, SEvent& ev) { eventEmitRecurtree(state, ev); });
//testProfile();
return 0;
}