forked from ill-inc/biomes-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.ts
138 lines (127 loc) · 3.53 KB
/
stats.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
/* eslint-disable no-console */
import type { AsDelta, ReadonlyEntity } from "@/shared/ecs/gen/entities";
import { DefaultMap } from "@/shared/util/collections";
import { zrpcSerialize } from "@/shared/zrpc/serde";
export class ComponentStats {
observations: number[] = [];
count = 0;
size = 0;
min?: number;
max?: number;
stddev?: number;
get mean() {
if (this.count > 0) {
return this.size / this.count;
}
}
record(size: number) {
this.observations.push(size);
this.count++;
this.size += size;
if (this.min === undefined || this.min > size) {
this.min = size;
}
if (this.max === undefined || this.max < size) {
this.max = size;
}
}
diff(other: ComponentStats) {
const ret = new ComponentStats();
ret.count = this.count - other.count;
ret.size = this.size - other.size;
if (this.min && other.min) {
ret.min = this.min - other.min;
}
if (this.max && other.max) {
ret.max = this.max - other.max;
}
if (this.stddev && other.stddev) {
ret.stddev = this.stddev - other.stddev;
}
return ret;
}
finalize() {
const mean = this.mean;
if (mean === undefined) {
this.stddev = undefined;
return;
}
this.stddev = 0;
for (const o of this.observations) {
this.stddev += (o - mean) * (o - mean);
}
this.stddev = Math.sqrt(this.stddev / this.count);
}
}
function prettyBytes(n: number): string {
if (n > 1024 * 1024) {
return `${n} (${Math.ceil(n / 1024 / 1024)}m)`;
} else if (n > 1024) {
return `${n} (${Math.ceil(n / 1024)}k)`;
}
return String(n);
}
export class EntityStats {
totalFileSize = 0;
totalEntities = 0;
totalComponents = 0;
componentStatistics = new DefaultMap<string, ComponentStats>(
() => new ComponentStats()
);
diff(other: EntityStats) {
const ret = new EntityStats();
ret.totalFileSize = this.totalFileSize - other.totalFileSize;
ret.totalEntities = this.totalEntities - other.totalEntities;
ret.totalComponents = this.totalComponents - other.totalComponents;
for (const [key, stat] of other.componentStatistics) {
ret.componentStatistics.set(
key,
stat.diff(this.componentStatistics.get(key))
);
}
for (const [key, stat] of this.componentStatistics) {
if (!other.componentStatistics.has(key)) {
ret.componentStatistics.set(key, new ComponentStats().diff(stat));
}
}
return ret;
}
observe(entity: ReadonlyEntity | AsDelta<ReadonlyEntity>) {
this.totalEntities++;
for (const [key, component] of Object.entries(entity)) {
if (key === "id") {
continue;
}
this.totalComponents++;
const stat = this.componentStatistics.get(key);
stat.record(zrpcSerialize(component).length);
}
}
finalize() {
for (const stat of this.componentStatistics.values()) {
stat.finalize();
}
}
toString() {
return [
`Total entities: ${this.totalEntities}`,
`Total components: ${this.totalComponents}`,
"",
...Array.from(this.componentStatistics.entries())
.sort((a, b) => b[1].size - a[1].size)
.flatMap(([key, comp]) => [
key,
` count: ${comp.count}`,
...(comp.count === 0
? []
: [
` size: ${prettyBytes(comp.size)}`,
` mean: ${comp.mean}`,
` min: ${comp.min}`,
` max: ${comp.max}`,
` stddev: ${comp.stddev}`,
]),
]),
].join("\n");
}
}