-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.html
213 lines (203 loc) · 8.8 KB
/
benchmark.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Winnow Benchmark</title>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro:ital,wght@0,400;0,700;1,400;1,700&display=swap" rel="stylesheet">
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; font-family: Georgia; }
body { padding: 1rem; max-width: 800px; }
p { margin-bottom: 1rem; }
button { margin-bottom: 1rem; }
h1, h2, h3 { margin-bottom: 1rem; width: 100%; }
table { border-collapse: collapse; }
tr:nth-child(even) td { background: #eee; }
th, td { padding: 0.5rem; }
</style>
</head>
<body>
<main>
<h1>Winnow Benchmark</h1>
<p>This benchmark repeatedly initializes a pool of N partial sifting pattern matches and pushes 100 random events onto the database, updating the partial match pool at each step. We track the per-event time taken to update the partial match pool and report the minimum, maximum and average per-event pool update times for each value of N. The following values of N are used: 10, 50, 100, 500, 1000.</p>
<p>Before running the benchmark, we suggest that you open the browser console to view benchmark progress and exact timings. The page may appear to hang while the benchmark is running, but console printouts should continue.</p>
<button id="runBenchmarkButton">Run benchmark</button>
<table id="summaryTable">
<thead>
<tr>
<th>Pool size</th>
<th>Min time (ms)</th>
<th>Max time (ms)</th>
<th>Avg time (ms)</th>
</tr>
</thead>
<tbody></tbody>
</table>
</main>
<script src="datascript-0.18.4.min.js"></script>
<script src="util.js"></script>
<script src="parser.js"></script>
<script src="compiler.js"></script>
<script src="runner.js"></script>
<script>
const schema = {
// character traits
curse: {":db/cardinality": ":db.cardinality/many"},
value: {":db/cardinality": ":db.cardinality/many"},
// other stuff
actor: {":db/valueType": ":db.type/ref"},
cause: {":db/valueType": ":db.type/ref"},
source: {":db/valueType": ":db.type/ref"},
target: {":db/valueType": ":db.type/ref"},
projectContributor: {":db/valueType": ":db.type/ref", ":db/cardinality": ":db.cardinality/many"},
tag: {":db/cardinality": ":db.cardinality/many"},
};
const allEventSpecs = [
//{eventType: 'enterTown'},
//{eventType: 'leaveTown'},
//{eventType: 'showHospitality'},
{eventType: 'getCoffeeWith', tags: ['friendly']},
{eventType: 'physicallyAttack', tags: ['unfriendly', 'harm', 'major']},
{eventType: 'disparagePublicly', tags: ['unfriendly', 'harm']},
{eventType: 'sendPostcard', tags: ['friendly']},
{eventType: 'insult', tags: ['unfriendly']},
{eventType: 'insultDismissively', tags: ['unfriendly', 'highStatus']},
{eventType: 'rejectSuperiority', tags: ['unfriendly', 'lowStatus']},
{eventType: 'flirtWith_accepted', tags: ['romantic', 'positive']},
{eventType: 'flirtWith_rejected', tags: ['romantic', 'negative', 'awkward']},
{eventType: 'askOut_accepted', tags: ['romantic', 'positive', 'major']},
{eventType: 'askOut_rejected', tags: ['romantic', 'negative', 'awkward', 'major']},
{eventType: 'propose_accepted', tags: ['romantic', 'positive', 'major']},
{eventType: 'propose_rejected', tags: ['romantic', 'negative', 'awkward', 'major']},
{eventType: 'breakUp', tags: ['romantic', 'negative', 'major']},
{eventType: 'buyLunchFor', tags: ['friendly']},
{eventType: 'inviteIntoGroup', tags: ['highStatus', 'friendly', 'helps']},
{eventType: 'shunFromGroup', tags: ['highStatus', 'unfriendly', 'harm']},
{eventType: 'apologizeTo', tags: ['friendly']},
{eventType: 'begForFavor', tags: ['lowStatus']},
{eventType: 'extortFavor', tags: ['highStatus']},
{eventType: 'callInFavor', tags: ['highStatus']},
{eventType: 'callInExtortionateFavor', tags: ['highStatus', 'harm']},
{eventType: 'playTheFool', tags: ['lowStatus', 'friendly']},
{eventType: 'playRoyalty', tags: ['highStatus', 'friendly']},
{eventType: 'neg', tags: ['highStatus', 'romantic', 'negative']},
{eventType: 'askForHelp', tags: ['lowStatus', 'friendly']},
{eventType: 'deferToExpertise', tags: ['career', 'lowStatus']},
//{eventType: 'noticeMeSenpai', tags: ['lowStatus', 'romantic']},
{eventType: 'deliberatelySabotage', tags: ['career', 'unfriendly', 'harm', 'major']},
{eventType: 'collab:phoneItIn', tags: ['career', 'harm']},
{eventType: 'collab:goAboveAndBeyond', tags: ['career', 'helps']},
];
// Add an event to the DB and return an updated DB.
function addEvent(db, event) {
const transaction = [[":db/add", -1, "type", "event"]];
for (const attr of Object.keys(event)) {
if (attr === "tags") continue;
transaction.push([":db/add", -1, attr, event[attr]]);
}
for (const tag of event.tags || []) {
transaction.push([":db/add", -1, "tag", tag]);
}
return datascript.db_with(db, transaction);
}
// set up the code that runs the benchmark when the button is pressed
const summaryTable = document.querySelector("#summaryTable");
const runBenchmarkButton = document.querySelector("#runBenchmarkButton");
const poolSizes = [10, 50, 100, 500, 1000];
runBenchmarkButton.onclick = function() {
for (const poolSize of poolSizes) {
console.log(`Running benchmark with pool size ${poolSize}...`);
const result = runBenchmark(poolSize);
console.log("Benchmark result", result);
const row = summaryTable.insertRow();
for (const key of ["poolSize", "min", "max", "avg"]) {
const cell = row.insertCell();
cell.innerText = result[key];
}
}
}
// compile pattern
const violationOfHospitality = `
(pattern violationOfHospitality
(event ?e1 where
eventType: enterTown,
actor: ?guest)
(event ?e2 where
eventType: showHospitality,
actor: ?host,
target: ?guest,
?host.value: communalism) ; not strictly needed?
(event ?e3 where
tag: harm,
actor: ?host,
target: ?guest)
;; make sure the guest hasn't left town
(unless-event ?eMid between ?e1 ?e3 where
eventType: leaveTown,
actor: ?guest))
`;
const parsed = parse(violationOfHospitality);
const compiled = compile(parsed);
const pattern = compiled[0];
// Run one instance of the benchmark and return the results.
function runBenchmark(poolSize) {
// set up partial match pool
const initialPoolSize = poolSize / 2; // this number is implicitly doubled further down
let partialMatches = [];
for (let i = 0; i < initialPoolSize; i++) {
partialMatches.push({pattern: pattern, bindings: {}});
}
// make DB
let db = datascript.empty_db(schema);
// generate and add characters
const charsToCreate = 5;
const testCharNames = ["Mira", "Emin", "Sarah", "Vincent", "Zach"];
const allCharacterIDs = [];
for (let i = 0; i < charsToCreate; i++) {
const transaction = [
[":db/add", -1, "type", "char"],
[":db/add", -1, "charName", testCharNames[i]],
[":db/add", -1, "value", "communalism"], // everyone values communalism for testing lol
];
db = datascript.db_with(db, transaction);
allCharacterIDs.push(i+1);
}
// push a bunch of random events and benchmark performance
const events = [];
const timings = [];
const rules = "[]";
const firstEventID = charsToCreate + 1;
for (let i = firstEventID; i < firstEventID + 100; i++) {
// On the first iteration, immediately push an enterTown event.
// This doubles the match pool size and ensures that half the matches
// in the pool are initialized, to imitate a more "realistic" pool
// (with a mix of uninitialized and initialized matches)
// while keeping the overall pool size fixed.
const event = (i === firstEventID)
? {eventType: "enterTown"}
: clone(randNth(allEventSpecs));
event.actor = randNth(allCharacterIDs);
event.target = randNth(allCharacterIDs.filter(id => id !== event.actor));
events.push(event);
db = addEvent(db, event);
// benchmark
const startTime = performance.now();
partialMatches = mapcat(partialMatches, pm => tryAdvance(pm, db, rules, i));
partialMatches = partialMatches.filter(pm => pm.lastStep !== "die" && pm.lastStep !== "complete");
const endTime = performance.now();
console.log(partialMatches.length, endTime - startTime);
timings.push(endTime - startTime);
}
return {
poolSize: poolSize,
avg: timings.reduce((a, b) => a + b) / timings.length,
min: Math.min(...timings),
max: Math.max(...timings),
allTimings: timings
};
}
</script>
</body>
</html>