Skip to content

Commit 2587356

Browse files
committed
test(json-crdt): 💍 add ability to collect fuzzer traces
1 parent 5979096 commit 2587356

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/json-crdt/__tests__/fuzzer/JsonCrdtFuzzer.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {FuzzerOptions} from './types';
55
import {RandomJson} from '../../../json-random/RandomJson';
66
import {generateInteger} from './util';
77
import {PatchBuilder} from '../../../json-crdt-patch/PatchBuilder';
8+
import {Patch} from '../../../json-crdt-patch';
89

910
export const defaultFuzzerOptions: FuzzerOptions = {
1011
startingValue: undefined,
@@ -18,12 +19,14 @@ export const defaultFuzzerOptions: FuzzerOptions = {
1819
concurrentPeers: [1, 6],
1920
patchesPerPeer: [0, 12],
2021
testCodecs: true,
22+
collectPatches: false,
2123
};
2224

2325
export class JsonCrdtFuzzer {
2426
public opts: FuzzerOptions;
2527
public model: Model;
2628
public picker: Picker;
29+
public patches: Patch[] = [];
2730

2831
constructor(opts: Partial<FuzzerOptions> = {}) {
2932
this.opts = {...defaultFuzzerOptions, ...opts};
@@ -39,6 +42,7 @@ export class JsonCrdtFuzzer {
3942
const builder = new PatchBuilder(this.model.clock);
4043
builder.root(builder.json(json));
4144
const patch = builder.flush();
45+
this.patches.push(patch);
4246
this.model.applyPatch(patch);
4347
}
4448

@@ -47,6 +51,8 @@ export class JsonCrdtFuzzer {
4751
const session = new SessionLogical(this, concurrency);
4852
session.generateEdits();
4953
session.synchronize();
54+
if (this.opts.collectPatches)
55+
for (const patches of session.patches) this.patches.push(...patches);
5056
return session;
5157
}
5258
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Run: npx ts-node src/json-crdt/__tests__/fuzzer/generate-trace.ts
2+
3+
import {Patch} from '../../../json-crdt-patch';
4+
import {Model} from '../../model';
5+
import {JsonCrdtFuzzer} from './JsonCrdtFuzzer';
6+
import {CborEncoder} from '../../../json-pack/cbor/CborEncoder';
7+
import {Writer} from '../../../util/buffers/Writer';
8+
import * as fs from 'fs';
9+
10+
const sessionNum = 100;
11+
const fuzzer = new JsonCrdtFuzzer({
12+
// concurrentPeers: [20, 100],
13+
// startingValue: new Uint8Array([1, 2, 3]),
14+
collectPatches: true,
15+
});
16+
fuzzer.setupModel();
17+
18+
for (let ses = 0; ses < sessionNum; ses++) {
19+
fuzzer.executeConcurrentSession();
20+
}
21+
22+
const patches: Patch[] = [];
23+
const dupes: Set<string> = new Set();
24+
for (const patch of fuzzer.patches) {
25+
const key = `${patch.getId()?.sid}_${patch.getId()?.time}`;
26+
if (dupes.has(key)) continue;
27+
dupes.add(key);
28+
patches.push(patch);
29+
}
30+
31+
const model = Model.withLogicalClock();
32+
model.applyBatch(patches);
33+
const cborEncoder = new CborEncoder(new Writer());
34+
fs.writeFileSync(__dirname + '/trace.cbor', cborEncoder.encode(patches.map(p => p.toBinary())));
35+
36+
// console.log(Buffer.from(jsonEncoder.encode(encoded)).toString());
37+
console.log(model.view());
38+
console.log(fuzzer.model.view());
39+
// console.log(model + '');

src/json-crdt/__tests__/fuzzer/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ export interface FuzzerOptions {
3434

3535
/** Do not generate "__proto__" as a string, so it does not appear as object key. */
3636
noProtoString?: boolean;
37+
38+
/** Whether to collect all generated patches. */
39+
collectPatches?: boolean;
3740
}

0 commit comments

Comments
 (0)