|
| 1 | +// @ts-ignore |
1 | 2 | import * as util from 'util'; |
| 3 | + |
2 | 4 | import { IPredicate, Node, Predicate, Property, TransactionBuilder, Uid } from '../src'; |
3 | 5 |
|
4 | 6 | describe('Performance testing', () => { |
@@ -76,37 +78,67 @@ describe('Performance testing', () => { |
76 | 78 | }); |
77 | 79 |
|
78 | 80 | it.only('Fuzz with random data', () => { |
| 81 | + interface IStation { |
| 82 | + uid: string; |
| 83 | + name: string; |
| 84 | + connects: IStation[]; |
| 85 | + } |
| 86 | + |
79 | 87 | @Node() |
| 88 | + // @ts-ignore |
80 | 89 | class Station { |
81 | 90 | @Uid() |
82 | 91 | uid: string; |
83 | 92 |
|
84 | | - @Property() |
| 93 | + @Property({ name: 'name' }) |
85 | 94 | name: string; |
86 | 95 |
|
87 | | - @Predicate({ type: () => Station }) |
| 96 | + @Predicate({ name: 'connects', type: () => Station }) |
88 | 97 | connects: IPredicate<Station>; |
89 | 98 | } |
90 | 99 |
|
91 | | - const STATION_COUNT = 5; |
| 100 | + const MIN_DEPTH = 3; |
| 101 | + const CONNECTION_PER_STATION = 2; |
| 102 | + const STATION_COUNT = 50; |
92 | 103 |
|
93 | | - const allStations = new Array(STATION_COUNT).fill(null).map((_, idx) => ({ |
| 104 | + console.time('Data create'); |
| 105 | + const allStations: IStation[] = new Array(STATION_COUNT).fill(null).map((_, idx) => ({ |
94 | 106 | uid: `${idx}`, |
95 | 107 | name: `Station_${idx}`, |
96 | | - stations: [] as any[] |
| 108 | + connects: [] as any[] |
97 | 109 | })); |
98 | 110 |
|
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])); |
| 111 | + new Array(MIN_DEPTH).fill(null).forEach(() => { |
| 112 | + allStations.forEach((station, idx) => { |
| 113 | + new Array(CONNECTION_PER_STATION).fill(null).forEach(() => { |
| 114 | + let rand = Math.floor(Math.random() * STATION_COUNT); |
| 115 | + if (rand !== idx) { |
| 116 | + station.connects.push(allStations[rand]); |
| 117 | + } |
| 118 | + }); |
| 119 | + }); |
102 | 120 | }); |
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 })); |
| 121 | + // console.timeEnd('Data create'); |
| 122 | + // console.log(util.inspect(allStations, { colors: true, depth: 10, compact: true, breakLength: 200 })); |
| 123 | + |
| 124 | + console.time('Data clean'); |
| 125 | + const tb = TransactionBuilder.of(Station); |
| 126 | + tb.setRoot(allStations); |
| 127 | + console.timeEnd('Data clean'); |
| 128 | + |
| 129 | + // console.time('Map build'); |
| 130 | + // tb.build(); |
| 131 | + // console.timeEnd('Map build'); |
| 132 | + // console.log(util.inspect(result.tree, { colors: true, depth: 20 })); |
| 133 | + // console.log(`Stations ${result.tree[0].name} connects to:\n`, result.tree[0].connects.get()); |
| 134 | + |
| 135 | + // expect(allStations[0].connects[0].connects[0].connects[0].connects[0].connects[0].uid).toEqual( |
| 136 | + // result.tree[0].connects |
| 137 | + // .get()[0] |
| 138 | + // .connects.get()[0] |
| 139 | + // .connects.get()[0] |
| 140 | + // .connects.get()[0] |
| 141 | + // .connects.get()[0].uid |
| 142 | + // ); |
111 | 143 | }); |
112 | 144 | }); |
0 commit comments