This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
generated from well-known-components/base-ts-project
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimple.ts
186 lines (152 loc) · 5.81 KB
/
simple.ts
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
import { ArchipelagoController, Island, Position3D } from "../src"
import { defaultOptions } from "../src/domain/Archipelago"
import seedrandom from "seedrandom"
import { sequentialIdGenerator } from "../src/misc/idGenerator"
import { createRandomizer } from "../test/random"
// import { SimpleArchipelagoController } from "./simple-controller"
import { Archipelago } from "../src/domain/Archipelago"
import { ArchipelagoControllerImpl } from "../src/controller/ArchipelagoController"
const MAX_PEERS_PER_ISLAND = process.env.MAX_PEERS_PER_ISLAND
? parseInt(process.env.MAX_PEERS_PER_ISLAND)
: defaultOptions().maxPeersPerIsland
const SEED = process.env.SEED ? process.env.SEED : `${seedrandom()()}`
const TARGET_PEERS = parseInt(process.env.TARGET_PEERS ?? "20000")
const DISCONNECT_CHANCE = parseFloat(process.env.DISCONNECT_CHANCE ?? "0.01")
const HOTSPOT_CHANCE = parseFloat(process.env.HOTSPOT_CHANCE ?? "0.95")
const TELEPORT_CHANCE = parseFloat(process.env.TELEPORT_CHANCE ?? "0.01")
const MIN_POSITION = (process.env.MIN_POSITION ? JSON.parse(process.env.MIN_POSITION) : [-2400, 0, -2400]) as Position3D
const MAX_POSITION = (process.env.MAX_POSITION ? JSON.parse(process.env.MAX_POSITION) : [2400, 0, 2400]) as Position3D
const DURATION = parseFloat(process.env.DURATION ?? "120")
const HOTSPOTS = parseInt(process.env.HOTSPOTS ?? "100")
const DEBUG = process.env.DEBUG === "true"
const MAX_OPERATIONS_PER_LOOP = 5000
const TIME_BETWEEN_LOOPS_MS = 20
const activePeers: { id: string; position: Position3D }[] = []
const peerIdGenerator = sequentialIdGenerator("Peer")
const archipelagoController: ArchipelagoController = new ArchipelagoControllerImpl({
archipelagoParameters: { joinDistance: 64, leaveDistance: 80, maxPeersPerIsland: MAX_PEERS_PER_ISLAND },
workerSrcPath: 'dist/worker/worker.js'
})
console.log("Using seed " + SEED)
const prng = seedrandom(SEED)
const randomizer = createRandomizer(prng)
function* generateHotspots() {
for (let i = 0; i < HOTSPOTS; i++) {
yield randomizer.generatePosition(MIN_POSITION, MAX_POSITION)
}
}
const hotSpots = [...generateHotspots()]
function generateRandomHotspotPositon(): Position3D {
const hotspotIndex = randomizer.randomArrayIndex(hotSpots)
return randomizer.generatePositionAround(hotSpots[hotspotIndex], [64, 0, 64])
}
function generateNewPeerPosition(): Position3D {
return randomizer.randomBoolean(HOTSPOT_CHANCE)
? generateRandomHotspotPositon()
: randomizer.generatePosition(MIN_POSITION, MAX_POSITION)
}
function addPeer() {
const randomPosition = generateNewPeerPosition()
const id = peerIdGenerator.generateId()
const request = { id, position: randomPosition }
archipelagoController.setPeersPositions(request)
activePeers.push(request)
}
function disconnectRandomPeer() {
const index = randomizer.randomArrayIndex(activePeers)
const activePeer = activePeers[index]
activePeers[index] = activePeers.pop()! // we remove the last element because is fast, and store it in the current index
archipelagoController.clearPeers(activePeer.id)
}
function changeRandomPeerPosition() {
const index = randomizer.randomArrayIndex(activePeers)
const newPosition = randomizer.randomBoolean(TELEPORT_CHANCE)
? generateNewPeerPosition()
: randomizer.generatePositionAround(activePeers[index].position, [5, 0, 5])
archipelagoController.setPeersPositions({ id: activePeers[index].id, position: newPosition })
}
function loop() {
if (activePeers.length < TARGET_PEERS) {
addPeer()
} else {
if (randomizer.randomBoolean(DISCONNECT_CHANCE)) {
disconnectRandomPeer()
} else {
changeRandomPeerPosition()
}
}
}
const startTime = Date.now()
let timeToLog = 1000
console.log(`Starting test at ${startTime}`)
let operations = 0
let elapsed = 0
let currentCycleOperations = 0
let currentCycleStartTime = Date.now()
async function getOrNA(method: "getPeersCount" | "getIslandsCount" | "getIslands"): Promise<any> {
try {
return await archipelagoController[method]()
} catch (e) {
return "N/A"
}
}
async function getPeersCount() {
return getOrNA("getPeersCount")
}
async function getIslandsCount() {
return getOrNA("getIslandsCount")
}
async function getIslands() {
return getOrNA("getIslands")
}
const logger = DEBUG
? async () => {
console.log(
`
Performed ${operations} ops. ${((operations * 1000) / elapsed).toFixed(2)} avg ops/s
Last second: ${currentCycleOperations} ops. ${(
(currentCycleOperations * 1000) /
(Date.now() - currentCycleStartTime)
).toFixed(2)} avg ops/s
Number of peers: ${await getPeersCount()}
Number of islands: ${await getIslandsCount()}
Number of peer per islands: ${(await getIslands()).map((it: Island) => it.peers.length)}
Elapsed: ${elapsed / 1000}s. Remaining: ${DURATION - elapsed / 1000}s
`
)
}
: async () => {
console.log(
`
Performed ${operations} ops. ${((operations * 1000) / elapsed).toFixed(2)} avg ops/s
Last second: ${currentCycleOperations} ops. ${(
(currentCycleOperations * 1000) /
(Date.now() - currentCycleStartTime)
).toFixed(2)} avg ops/s
Number of peers: ${await getPeersCount()}
Number of islands: ${await getIslandsCount()}
Elapsed: ${elapsed / 1000}s. Remaining: ${DURATION - elapsed / 1000}s
`
)
}
const timerLoop = async () => {
for (let i = 0; i < MAX_OPERATIONS_PER_LOOP; i++) {
loop()
operations++
currentCycleOperations++
}
if (elapsed > timeToLog) {
logger()
timeToLog += 1000
currentCycleStartTime = Date.now()
currentCycleOperations = 0
}
if ((elapsed = Date.now() - startTime) < DURATION * 1000) {
setTimeout(timerLoop, TIME_BETWEEN_LOOPS_MS)
} else {
await archipelagoController.flush()
await archipelagoController.dispose()
console.log(`Test finished at ${Date.now()}. Total operations: ${operations}`)
}
}
timerLoop()