Skip to content

Commit 84c5eb0

Browse files
add random data fuzz
1 parent 9a7670a commit 84c5eb0

1 file changed

Lines changed: 92 additions & 54 deletions

File tree

tests/perf.spec.ts

Lines changed: 92 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,112 @@
1-
import { IPredicate, Node, Predicate, TransactionBuilder, Uid } from '../src';
1+
import * as util from 'util';
2+
import { IPredicate, Node, Predicate, Property, TransactionBuilder, Uid } from '../src';
23

3-
it('performance Testing', () => {
4-
@Node()
5-
class Cell {
6-
@Uid()
7-
uid: string;
4+
describe('Performance testing', () => {
5+
it('mutation string generate', () => {
6+
@Node()
7+
class Cell {
8+
@Uid()
9+
uid: string;
810

9-
@Predicate({ name: 'from_row', type: () => Row })
10-
fromRow: IPredicate<Row>;
11+
@Predicate({ name: 'from_row', type: () => Row })
12+
fromRow: IPredicate<Row>;
1113

12-
@Predicate({ name: 'from_column', type: () => Column })
13-
fromColumn: IPredicate<Column>;
14-
}
14+
@Predicate({ name: 'from_column', type: () => Column })
15+
fromColumn: IPredicate<Column>;
16+
}
1517

16-
@Node()
17-
class Row {
18-
@Uid()
19-
uid: string;
18+
@Node()
19+
class Row {
20+
@Uid()
21+
uid: string;
2022

21-
@Predicate({ name: 'has_cell', type: () => Cell })
22-
hasCell: IPredicate<Cell>;
23-
}
23+
@Predicate({ name: 'has_cell', type: () => Cell })
24+
hasCell: IPredicate<Cell>;
25+
}
2426

25-
@Node()
26-
class Column {
27-
@Uid()
28-
uid: string;
27+
@Node()
28+
class Column {
29+
@Uid()
30+
uid: string;
2931

30-
@Predicate({ name: 'has_cell', type: () => Cell })
31-
hasCell: IPredicate<Cell>;
32-
}
32+
@Predicate({ name: 'has_cell', type: () => Cell })
33+
hasCell: IPredicate<Cell>;
34+
}
3335

34-
const transaction = TransactionBuilder.build();
36+
const transaction = TransactionBuilder.build();
3537

36-
const column = transaction.nodeFor(Column);
38+
const column = transaction.nodeFor(Column);
3739

38-
const times = 1000;
40+
const times = 1000;
3941

40-
let i = 0;
41-
while (i++ < times) {
42-
const cell = transaction.nodeFor(Cell);
43-
const row = transaction.nodeFor(Row);
42+
let i = 0;
43+
while (i++ < times) {
44+
const cell = transaction.nodeFor(Cell);
45+
const row = transaction.nodeFor(Row);
4446

45-
cell.fromRow.add(row);
46-
cell.fromColumn.add(column);
47+
cell.fromRow.add(row);
48+
cell.fromColumn.add(column);
4749

48-
row.hasCell.add(cell);
49-
column.hasCell.add(cell);
50-
}
50+
row.hasCell.add(cell);
51+
column.hasCell.add(cell);
52+
}
5153

52-
console.time('Initial');
53-
transaction.getSetNQuadsString();
54-
console.timeEnd('Initial');
54+
console.time('Initial');
55+
transaction.getSetNQuadsString();
56+
console.timeEnd('Initial');
5557

56-
const transaction2 = TransactionBuilder.build();
57-
const column2 = transaction2.nodeFor(Column);
58+
const transaction2 = TransactionBuilder.build();
59+
const column2 = transaction2.nodeFor(Column);
5860

59-
i = 0;
60-
while (i++ < times) {
61-
const cell2 = transaction2.nodeFor(Cell);
62-
const row2 = transaction2.nodeFor(Row);
61+
i = 0;
62+
while (i++ < times) {
63+
const cell2 = transaction2.nodeFor(Cell);
64+
const row2 = transaction2.nodeFor(Row);
6365

64-
cell2.fromRow.add(row2);
65-
cell2.fromColumn.add(column2);
66+
cell2.fromRow.add(row2);
67+
cell2.fromColumn.add(column2);
6668

67-
row2.hasCell.add(cell2);
68-
column2.hasCell.add(cell2);
69-
}
69+
row2.hasCell.add(cell2);
70+
column2.hasCell.add(cell2);
71+
}
7072

71-
console.time('Second');
72-
transaction2.getSetNQuadsString();
73-
console.timeEnd('Second');
73+
console.time('Second');
74+
transaction2.getSetNQuadsString();
75+
console.timeEnd('Second');
76+
});
77+
78+
it.only('Fuzz with random data', () => {
79+
@Node()
80+
class Station {
81+
@Uid()
82+
uid: string;
83+
84+
@Property()
85+
name: string;
86+
87+
@Predicate({ type: () => Station })
88+
connects: IPredicate<Station>;
89+
}
90+
91+
const STATION_COUNT = 5;
92+
93+
const allStations = new Array(STATION_COUNT).fill(null).map((_, idx) => ({
94+
uid: `${idx}`,
95+
name: `Station_${idx}`,
96+
stations: [] as any[]
97+
}));
98+
99+
new Array(2).fill(null).forEach(() => {
100+
const rand = Math.floor(Math.random() * STATION_COUNT);
101+
allStations.forEach(station => station.stations.push(allStations[rand]));
102+
});
103+
104+
console.log(util.inspect(allStations, { colors: true, depth: 10 }));
105+
106+
const result = TransactionBuilder.of(Station)
107+
.setRoot(allStations)
108+
.build();
109+
110+
console.log(util.inspect(result.tree, { colors: true, depth: 10 }));
111+
});
74112
});

0 commit comments

Comments
 (0)