@mark1russell7/capacity 0.0.0-bootstrap.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1107 -1
- package/build/index.d.ts +858 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +1713 -0
- package/build/index.js.map +1 -0
- package/build/instance-0n6WhJzF.d.ts +782 -0
- package/build/instance-0n6WhJzF.d.ts.map +1 -0
- package/build/instance-check-CAuD9L4Z.js +1711 -0
- package/build/instance-check-CAuD9L4Z.js.map +1 -0
- package/build/schema.d.ts +69 -0
- package/build/schema.d.ts.map +1 -0
- package/build/schema.js +315 -0
- package/build/schema.js.map +1 -0
- package/limits/alerts-routes.line.json +78 -0
- package/limits/alerts-scale.limits.json +10 -0
- package/limits/alerts-start-gate.limits.json +1149 -0
- package/limits/alerts-start-routes-limit-home.bracket.json +543 -0
- package/limits/alerts-start-routes-limit-mid.bracket.json +464 -0
- package/limits/alerts-start-routes-limit.bracket.json +579 -0
- package/limits/alerts-tail-gate.limits.json +610 -0
- package/package.json +79 -4
- package/src/app-check.ts +477 -0
- package/src/app-metrics.ts +105 -0
- package/src/app.ts +239 -0
- package/src/check.ts +228 -0
- package/src/composition.ts +680 -0
- package/src/demand.ts +312 -0
- package/src/fit.ts +153 -0
- package/src/forms.ts +358 -0
- package/src/gate.ts +237 -0
- package/src/index.ts +142 -0
- package/src/instance-check.ts +122 -0
- package/src/instance.ts +84 -0
- package/src/knee.ts +606 -0
- package/src/law.ts +215 -0
- package/src/limit.ts +112 -0
- package/src/names.ts +19 -0
- package/src/observe.ts +74 -0
- package/src/program.ts +74 -0
- package/src/scale.ts +617 -0
- package/src/schema.ts +260 -0
- package/src/value.ts +206 -0
- package/src/workload-check.ts +130 -0
- package/src/workload.ts +88 -0
package/src/schema.ts
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { err, ok, type Result } from "@mark1russell7/optional";
|
|
2
|
+
import { parse, type Expr } from "@mark1russell7/vex";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { checkApp } from "./app-check.ts";
|
|
5
|
+
import { APP_FORMAT, APP_LINE_KINDS, type AppContract, type AppProblem } from "./app.ts";
|
|
6
|
+
import { checkLaw } from "./check.ts";
|
|
7
|
+
import { COMPOSITION_FORMAT, checkComposition, type Composition, type CompositionProblem } from "./composition.ts";
|
|
8
|
+
import { checkInstance, checkLimits } from "./instance-check.ts";
|
|
9
|
+
import {
|
|
10
|
+
INSTANCE_FORMAT,
|
|
11
|
+
LIMITS_FORMAT,
|
|
12
|
+
type InstanceProblem,
|
|
13
|
+
type InstanceType,
|
|
14
|
+
type Limit,
|
|
15
|
+
type LimitProblem,
|
|
16
|
+
type LimitSet,
|
|
17
|
+
} from "./instance.ts";
|
|
18
|
+
import { LAW_FORMAT, type Law, type LawProblem, type Problem } from "./law.ts";
|
|
19
|
+
import { LAW_DOMAINS } from "./program.ts";
|
|
20
|
+
import { checkWorkload } from "./workload-check.ts";
|
|
21
|
+
import { WORKLOAD_FORMAT, type Workload, type WorkloadProblem } from "./workload.ts";
|
|
22
|
+
|
|
23
|
+
/** This function tells if a value from JSON is a Vex expression. */
|
|
24
|
+
const isProgram = (value: unknown): boolean => {
|
|
25
|
+
const text = JSON.stringify(value);
|
|
26
|
+
return text !== undefined && parse(text, LAW_DOMAINS).ok;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* This function makes the reader of a record. The reader examines the shape of the data with the
|
|
31
|
+
* schema, then it examines the meaning with `check`. It gives the record, or each problem.
|
|
32
|
+
*/
|
|
33
|
+
const reader =
|
|
34
|
+
<T, Code extends string>(schema: z.ZodType<T>, check: (record: T) => readonly Problem<Code>[]) =>
|
|
35
|
+
(data: unknown): Result<T, readonly Problem<Code | "shape">[]> => {
|
|
36
|
+
const shape = schema.safeParse(data);
|
|
37
|
+
if (!shape.success) {
|
|
38
|
+
return err(shape.error.issues.map((issue) => ({ code: "shape", path: issue.path.join("."), message: issue.message })));
|
|
39
|
+
}
|
|
40
|
+
const problems = check(shape.data);
|
|
41
|
+
return problems.length > 0 ? err(problems) : ok(shape.data);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const fitErrorSchema = z.strictObject({ mae: z.number(), rmse: z.number() });
|
|
45
|
+
|
|
46
|
+
const runSchema = z.strictObject({ url: z.string(), commit: z.string(), date: z.string() });
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* This schema accepts the shape of a law file: each field with its type, and no other field. It
|
|
50
|
+
* does not examine what the fields mean. The function `checkLaw` does that.
|
|
51
|
+
*/
|
|
52
|
+
export const lawSchema: z.ZodType<Law> = z.strictObject({
|
|
53
|
+
format: z.literal(LAW_FORMAT),
|
|
54
|
+
id: z.string(),
|
|
55
|
+
title: z.string(),
|
|
56
|
+
subject: z.string(),
|
|
57
|
+
measure: z.strictObject({ id: z.string(), unit: z.string() }),
|
|
58
|
+
variables: z.array(
|
|
59
|
+
z.strictObject({ id: z.string(), unit: z.string(), min: z.number(), max: z.number(), scale: z.enum(["linear", "log"]) }),
|
|
60
|
+
),
|
|
61
|
+
conditions: z.record(
|
|
62
|
+
z.string(),
|
|
63
|
+
z.union([z.string(), z.number(), z.boolean(), z.strictObject({ value: z.number(), unit: z.string() })]),
|
|
64
|
+
),
|
|
65
|
+
form: z.strictObject({ id: z.string(), program: z.custom<Expr>(isProgram, "The program is not a Vex expression.") }),
|
|
66
|
+
constants: z.array(z.strictObject({ id: z.string(), value: z.number() })),
|
|
67
|
+
replicates: z.array(z.array(z.number())),
|
|
68
|
+
fit: z.strictObject({
|
|
69
|
+
method: z.string(),
|
|
70
|
+
residual: z.enum(["absolute", "relative"]),
|
|
71
|
+
residuals: z.array(z.number()),
|
|
72
|
+
losers: z.array(z.strictObject({ form: z.string(), error: fitErrorSchema })),
|
|
73
|
+
}),
|
|
74
|
+
points: z.array(z.strictObject({ load: z.record(z.string(), z.number()), value: z.number(), run: z.number(), job: z.string() })).exactOptional(),
|
|
75
|
+
evidence: z.strictObject({ runs: z.array(runSchema) }),
|
|
76
|
+
noise: z.strictObject({ floor: z.number(), cpu: z.string(), runs: z.array(runSchema) }),
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* This function reads a law from the data of a law file. It gives the law when the data has the
|
|
81
|
+
* shape of a law and `checkLaw` finds no problem. If not, it gives each problem.
|
|
82
|
+
*/
|
|
83
|
+
export const readLaw: (data: unknown) => Result<Law, readonly LawProblem[]> = reader(lawSchema, checkLaw);
|
|
84
|
+
|
|
85
|
+
const factorSchema = z.number().optional();
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* This schema accepts the shape of a composition file: each field with its type, and no other
|
|
89
|
+
* field. It does not examine what the fields mean. The function `checkComposition` does that.
|
|
90
|
+
*/
|
|
91
|
+
export const compositionSchema: z.ZodType<Composition> = z.strictObject({
|
|
92
|
+
format: z.literal(COMPOSITION_FORMAT),
|
|
93
|
+
id: z.string(),
|
|
94
|
+
title: z.string(),
|
|
95
|
+
subject: z.string(),
|
|
96
|
+
measure: z.strictObject({ id: z.string(), unit: z.string() }),
|
|
97
|
+
terms: z.array(
|
|
98
|
+
z.union([
|
|
99
|
+
z.strictObject({
|
|
100
|
+
law: z.string(),
|
|
101
|
+
over: z.union([z.literal("instance"), z.literal("each"), z.array(z.string())]),
|
|
102
|
+
load: z.enum(["class", "total"]).optional(),
|
|
103
|
+
factor: factorSchema,
|
|
104
|
+
base: z.lazy(() => amountsSchema).optional(),
|
|
105
|
+
}),
|
|
106
|
+
z.strictObject({ composition: z.string(), factor: factorSchema }),
|
|
107
|
+
]),
|
|
108
|
+
),
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* This function reads a composition from the data of a composition file. It gives the composition
|
|
113
|
+
* when the data has the shape of one and `checkComposition` finds no problem. If not, it gives each
|
|
114
|
+
* problem.
|
|
115
|
+
*/
|
|
116
|
+
export const readComposition: (data: unknown) => Result<Composition, readonly CompositionProblem[]> = reader(
|
|
117
|
+
compositionSchema,
|
|
118
|
+
checkComposition,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const amountSchema = z.strictObject({ value: z.number(), unit: z.string() });
|
|
122
|
+
const amountsSchema = z.record(z.string(), amountSchema);
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* This schema accepts the shape of a workload file: each field with its type, and no other field.
|
|
126
|
+
* It does not examine what the fields mean. The function `checkWorkload` does that.
|
|
127
|
+
*/
|
|
128
|
+
export const workloadSchema: z.ZodType<Workload> = z.strictObject({
|
|
129
|
+
format: z.literal(WORKLOAD_FORMAT),
|
|
130
|
+
id: z.string(),
|
|
131
|
+
title: z.string(),
|
|
132
|
+
classes: z.array(z.strictObject({ id: z.string(), of: z.string().optional(), rate: amountSchema, request: amountsSchema })),
|
|
133
|
+
levels: amountsSchema,
|
|
134
|
+
source: z.discriminatedUnion("kind", [
|
|
135
|
+
z.strictObject({ kind: z.literal("plan"), note: z.string() }),
|
|
136
|
+
z.strictObject({ kind: z.literal("metrics"), origin: z.string(), from: z.string(), to: z.string() }),
|
|
137
|
+
]),
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* This function reads a workload from the data of a workload file. It gives the workload when the
|
|
142
|
+
* data has the shape of a workload and `checkWorkload` finds no problem. If not, it gives each
|
|
143
|
+
* problem.
|
|
144
|
+
*/
|
|
145
|
+
export const readWorkload: (data: unknown) => Result<Workload, readonly WorkloadProblem[]> = reader(workloadSchema, checkWorkload);
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* This schema accepts the shape of an instance file: each field with its type, and no other field.
|
|
149
|
+
* It does not examine what the fields mean. The function `checkInstance` does that.
|
|
150
|
+
*/
|
|
151
|
+
export const instanceSchema: z.ZodType<InstanceType> = z.strictObject({
|
|
152
|
+
format: z.literal(INSTANCE_FORMAT),
|
|
153
|
+
id: z.string(),
|
|
154
|
+
title: z.string(),
|
|
155
|
+
resources: amountsSchema,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* This function reads an instance type from the data of an instance file. It gives the instance
|
|
160
|
+
* type when the data has the shape of one and `checkInstance` finds no problem. If not, it gives
|
|
161
|
+
* each problem.
|
|
162
|
+
*/
|
|
163
|
+
export const readInstance: (data: unknown) => Result<InstanceType, readonly InstanceProblem[]> = reader(instanceSchema, checkInstance);
|
|
164
|
+
|
|
165
|
+
/** This schema accepts the shape of one limit: each field with its type, and no other field. */
|
|
166
|
+
export const limitSchema: z.ZodType<Limit> = z.strictObject({
|
|
167
|
+
id: z.string(),
|
|
168
|
+
measure: z.string(),
|
|
169
|
+
ceiling: z.discriminatedUnion("kind", [
|
|
170
|
+
z.strictObject({ kind: z.literal("amount"), amount: amountSchema }),
|
|
171
|
+
z.strictObject({ kind: z.literal("resource"), resource: z.string() }),
|
|
172
|
+
]),
|
|
173
|
+
headroom: z.number(),
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* This schema accepts the shape of a limits file: each field with its type, and no other field.
|
|
178
|
+
* It does not examine what the fields mean. The function `checkLimits` does that.
|
|
179
|
+
*/
|
|
180
|
+
export const limitsSchema: z.ZodType<LimitSet> = z.strictObject({
|
|
181
|
+
format: z.literal(LIMITS_FORMAT),
|
|
182
|
+
id: z.string(),
|
|
183
|
+
title: z.string(),
|
|
184
|
+
limits: z.array(limitSchema),
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* This function reads a limit set from the data of a limits file. It gives the limit set when the
|
|
189
|
+
* data has the shape of one and `checkLimits` finds no problem. If not, it gives each problem.
|
|
190
|
+
*/
|
|
191
|
+
export const readLimits: (data: unknown) => Result<LimitSet, readonly LimitProblem[]> = reader(limitsSchema, checkLimits);
|
|
192
|
+
|
|
193
|
+
const scaleSchema = z.enum(["linear", "log"]);
|
|
194
|
+
const rangeFields = { unit: z.string(), min: z.number(), max: z.number(), scale: scaleSchema };
|
|
195
|
+
const commandFields = { argv: z.array(z.string()), cwd: z.string(), env: z.record(z.string(), z.string()) };
|
|
196
|
+
const commandSchema = z.strictObject(commandFields);
|
|
197
|
+
const processSchema = z.strictObject({
|
|
198
|
+
...commandFields,
|
|
199
|
+
ready: z.discriminatedUnion("kind", [
|
|
200
|
+
z.strictObject({ kind: z.literal("line"), text: z.string() }),
|
|
201
|
+
z.strictObject({ kind: z.literal("http"), path: z.string() }),
|
|
202
|
+
]),
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* This schema accepts the shape of the contract file of an app: each field with its type, and no other
|
|
207
|
+
* field. It does not examine what the fields mean. The function `checkApp` does that.
|
|
208
|
+
*/
|
|
209
|
+
export const appSchema: z.ZodType<AppContract> = z.strictObject({
|
|
210
|
+
format: z.literal(APP_FORMAT),
|
|
211
|
+
id: z.string(),
|
|
212
|
+
title: z.string(),
|
|
213
|
+
repository: z.string(),
|
|
214
|
+
variables: z.array(z.strictObject({ id: z.string(), title: z.string(), kind: z.enum(["request", "level", "store", "run"]), ...rangeFields })),
|
|
215
|
+
classes: z.array(
|
|
216
|
+
z.strictObject({
|
|
217
|
+
id: z.string(),
|
|
218
|
+
title: z.string(),
|
|
219
|
+
rate: z.strictObject(rangeFields),
|
|
220
|
+
request: z.array(z.string()),
|
|
221
|
+
entry: z.discriminatedUnion("kind", [
|
|
222
|
+
z.strictObject({
|
|
223
|
+
kind: z.literal("http"),
|
|
224
|
+
method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE"]),
|
|
225
|
+
path: z.string(),
|
|
226
|
+
body: z.string().optional(),
|
|
227
|
+
}),
|
|
228
|
+
z.strictObject({ kind: z.literal("service"), service: z.string() }),
|
|
229
|
+
]),
|
|
230
|
+
}),
|
|
231
|
+
),
|
|
232
|
+
start: z.strictObject({
|
|
233
|
+
install: commandSchema.optional(),
|
|
234
|
+
check: commandSchema.optional(),
|
|
235
|
+
seed: commandSchema.optional(),
|
|
236
|
+
run: processSchema.extend({
|
|
237
|
+
lines: z.array(z.strictObject({ kind: z.enum(APP_LINE_KINDS), ev: z.string(), title: z.string() })).optional(),
|
|
238
|
+
}),
|
|
239
|
+
}),
|
|
240
|
+
services: z.array(z.strictObject({ id: z.string(), title: z.string(), fake: processSchema })),
|
|
241
|
+
metrics: z.array(
|
|
242
|
+
z.strictObject({
|
|
243
|
+
id: z.string(),
|
|
244
|
+
kind: z.enum(["counter", "gauge"]),
|
|
245
|
+
unit: z.string(),
|
|
246
|
+
of: z.union([
|
|
247
|
+
z.strictObject({ rate: z.string() }),
|
|
248
|
+
z.strictObject({ level: z.string() }),
|
|
249
|
+
z.strictObject({ total: z.string(), class: z.string() }),
|
|
250
|
+
z.strictObject({ measure: z.string() }),
|
|
251
|
+
]),
|
|
252
|
+
}),
|
|
253
|
+
),
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* This function reads the contract of an app from the data of a contract file. It gives the contract when
|
|
258
|
+
* the data has the shape of one and `checkApp` finds no problem. If not, it gives each problem.
|
|
259
|
+
*/
|
|
260
|
+
export const readApp: (data: unknown) => Result<AppContract, readonly AppProblem[]> = reader(appSchema, checkApp);
|
package/src/value.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { err, ok, type Result } from "@mark1russell7/optional";
|
|
2
|
+
import { quantile } from "@mark1russell7/rigor-statistics";
|
|
3
|
+
import { DEFAULT_LEVEL, leastCount, type Law, type LawProblem } from "./law.ts";
|
|
4
|
+
import { recordOf, valuesAt, valuesOf } from "./program.ts";
|
|
5
|
+
|
|
6
|
+
/** A load point gives one value for each variable of a law, in the unit of the variable. */
|
|
7
|
+
export type LoadPoint = Readonly<Record<string, number>>;
|
|
8
|
+
|
|
9
|
+
/** A range goes from `low` to `high`. */
|
|
10
|
+
export interface Range {
|
|
11
|
+
readonly low: number;
|
|
12
|
+
readonly high: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** These options set the level of the intervals of a query. The level is 0.95 when a query gives none. */
|
|
16
|
+
export interface LevelOptions {
|
|
17
|
+
readonly level?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A law value is the answer of a law at one load point, in the unit of the measure.
|
|
22
|
+
*
|
|
23
|
+
* The `interval` is the doubt of the law: the spread of its replicates at the point. It always
|
|
24
|
+
* holds the value. The range `run` is where one measured run falls: the interval, made wider by
|
|
25
|
+
* the residuals of the fit. The list `outside` names each variable whose value is outside its
|
|
26
|
+
* range. With a name in this list, the value is an extrapolation.
|
|
27
|
+
*/
|
|
28
|
+
export interface LawValue {
|
|
29
|
+
readonly value: number;
|
|
30
|
+
readonly unit: string;
|
|
31
|
+
readonly level: number;
|
|
32
|
+
readonly interval: Range;
|
|
33
|
+
readonly run: Range;
|
|
34
|
+
readonly outside: readonly string[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A constant interval is the value of one constant of a law and the interval of its replicates. */
|
|
38
|
+
export interface ConstantInterval {
|
|
39
|
+
readonly id: string;
|
|
40
|
+
readonly value: number;
|
|
41
|
+
readonly level: number;
|
|
42
|
+
readonly interval: Range;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const problem = (code: LawProblem["code"], path: string, message: string): LawProblem => ({ code, path, message });
|
|
46
|
+
|
|
47
|
+
/** This function gives the two ends of the share `level` of some values. */
|
|
48
|
+
export const rangeOf = (values: readonly number[], level: number): Range => ({
|
|
49
|
+
low: quantile(values, (1 - level) / 2),
|
|
50
|
+
high: quantile(values, (1 + level) / 2),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
/** This function gives the problem of a level that the law cannot show, or undefined. */
|
|
54
|
+
export const levelProblem = (law: Law, level: number, residuals: boolean): LawProblem | undefined => {
|
|
55
|
+
if (!(level > 0 && level < 1)) return problem("level", "level", `The level ${level} is not between 0 and 1.`);
|
|
56
|
+
const least = leastCount(level);
|
|
57
|
+
if (law.replicates.length < least) {
|
|
58
|
+
return problem("level", "replicates", `The level ${level} needs ${least} replicates. The law has ${law.replicates.length}.`);
|
|
59
|
+
}
|
|
60
|
+
if (residuals && law.fit.residuals.length < least) {
|
|
61
|
+
return problem("level", "fit.residuals", `The level ${level} needs ${least} residuals. The law has ${law.fit.residuals.length}.`);
|
|
62
|
+
}
|
|
63
|
+
return undefined;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** This function gives the range of one measured run: the interval of the law, made wider by the residuals. */
|
|
67
|
+
export const runOf = (law: Law, interval: Range, level: number): Range => {
|
|
68
|
+
const band = rangeOf(law.fit.residuals, level);
|
|
69
|
+
if (law.fit.residual === "absolute") return { low: interval.low + band.low, high: interval.high + band.high };
|
|
70
|
+
const corners = [interval.low, interval.high].flatMap((end) => [end * (1 + band.low), end * (1 + band.high)]);
|
|
71
|
+
return { low: Math.min(...corners), high: Math.max(...corners) };
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* This function gives the nearest point of a load point in the ranges of the variables of a law. A point in each range
|
|
76
|
+
* gives undefined. A name that is not a variable of the law, or a value that is not a number, does not change.
|
|
77
|
+
*/
|
|
78
|
+
export const edgePointOf = (law: Law, point: LoadPoint): LoadPoint | undefined => {
|
|
79
|
+
let edge: Record<string, number> | undefined;
|
|
80
|
+
for (const variable of law.variables) {
|
|
81
|
+
const value = point[variable.id];
|
|
82
|
+
if (typeof value !== "number" || Number.isNaN(value)) continue;
|
|
83
|
+
const held = Math.min(variable.max, Math.max(variable.min, value));
|
|
84
|
+
if (held !== value) (edge ??= { ...point })[variable.id] = held;
|
|
85
|
+
}
|
|
86
|
+
return edge;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* This function holds a value of a law past its measures. Each measure of a law is a cost, and no run measured the
|
|
91
|
+
* cost past the edge. Thus the value is the higher of the value of the law and its value at the nearest edge. A
|
|
92
|
+
* cost that falls with the load then stays flat past the edge, and a cost that rises keeps its law. A missing value
|
|
93
|
+
* stays missing, and a missing value at the edge keeps the value of the law.
|
|
94
|
+
*/
|
|
95
|
+
export const heldValue = (value: number | undefined, edge: number | undefined): number | undefined =>
|
|
96
|
+
value === undefined || edge === undefined || !Number.isFinite(edge) ? value : Math.max(value, edge);
|
|
97
|
+
|
|
98
|
+
/** This count is the most edge points that the cache keeps for one law. A law with one variable has at most two. */
|
|
99
|
+
const EDGE_POINTS = 64;
|
|
100
|
+
|
|
101
|
+
/** This cache keeps the values of each law at each edge point, one value for each row: the constants, then each replicate. */
|
|
102
|
+
const edges = new WeakMap<Law, Map<string, readonly (number | undefined)[]>>();
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* This function gives the values of a law at an edge point, one for each row: row 0 with the constants, then each
|
|
106
|
+
* replicate. Each load past the top of a variable has the same edge point. Thus a search past the measures evaluates
|
|
107
|
+
* the law at the edge one time, and the cache gives the values after that.
|
|
108
|
+
*/
|
|
109
|
+
export const edgeValuesOf = (law: Law, edge: LoadPoint): readonly (number | undefined)[] => {
|
|
110
|
+
const key = law.variables.map((variable) => edge[variable.id]).join(",");
|
|
111
|
+
let byPoint = edges.get(law);
|
|
112
|
+
const known = byPoint?.get(key);
|
|
113
|
+
if (known !== undefined) return known;
|
|
114
|
+
const names = law.constants.map((constant) => constant.id);
|
|
115
|
+
const rows = [law.constants.map((constant) => constant.value), ...law.replicates];
|
|
116
|
+
const values = valuesAt(law.form.program, rows.map((row) => recordOf(names, row, edge)));
|
|
117
|
+
if (byPoint === undefined || byPoint.size >= EDGE_POINTS) edges.set(law, (byPoint = new Map()));
|
|
118
|
+
byPoint.set(key, values);
|
|
119
|
+
return values;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/** These values are the value of a law at a point with its constants, then with each replicate, and the variables outside their ranges. */
|
|
123
|
+
export interface PointValues {
|
|
124
|
+
readonly values: readonly [number, ...number[]];
|
|
125
|
+
readonly outside: readonly string[];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* This function evaluates a law at a load point with its constants, then with each replicate. The
|
|
130
|
+
* point gives each variable of the law, and no other name.
|
|
131
|
+
*
|
|
132
|
+
* Past the range of a variable, each value holds, as heldValue tells.
|
|
133
|
+
* It gives a problem for a point that the law cannot read, and for a value that is not finite.
|
|
134
|
+
*/
|
|
135
|
+
export const pointValuesOf = (law: Law, point: LoadPoint): Result<PointValues, LawProblem> => {
|
|
136
|
+
const variables = law.variables.map((variable) => variable.id);
|
|
137
|
+
const unknown = Object.keys(point).find((name) => !variables.includes(name));
|
|
138
|
+
if (unknown !== undefined) return err(problem("point", unknown, `The law has no variable "${unknown}".`));
|
|
139
|
+
const load: Record<string, number> = {};
|
|
140
|
+
for (const name of variables) {
|
|
141
|
+
const value = point[name];
|
|
142
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
143
|
+
return err(problem("point", name, `The point gives no finite value for the variable "${name}".`));
|
|
144
|
+
}
|
|
145
|
+
load[name] = value;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const names = law.constants.map((constant) => constant.id);
|
|
149
|
+
const constants = law.constants.map((constant) => constant.value);
|
|
150
|
+
const values = valuesOf(law.form.program, [recordOf(names, constants, load), ...law.replicates.map((row) => recordOf(names, row, load))]);
|
|
151
|
+
if (values.tag === "err") return err(problem("value", "form.program", `The law has no value at this point: ${values.error}.`));
|
|
152
|
+
const outside = law.variables
|
|
153
|
+
.filter((variable) => (load[variable.id] as number) < variable.min || (load[variable.id] as number) > variable.max)
|
|
154
|
+
.map((variable) => variable.id);
|
|
155
|
+
const edge = outside.length === 0 ? undefined : edgePointOf(law, load);
|
|
156
|
+
if (edge === undefined) return ok({ values: values.value as readonly [number, ...number[]], outside });
|
|
157
|
+
const atEdge = edgeValuesOf(law, edge);
|
|
158
|
+
const held = values.value.map((value, row) => heldValue(value, atEdge[row]) ?? value);
|
|
159
|
+
return ok({ values: held as readonly number[] as readonly [number, ...number[]], outside });
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
/** This function gives the interval of some replicate values at the share `level`, made wider to hold `value`. */
|
|
163
|
+
export const intervalOf = (value: number, spread: readonly number[], level: number): Range => {
|
|
164
|
+
// At a bend of a form, each replicate can be on one side of the value. The interval then starts at the value.
|
|
165
|
+
const share = rangeOf(spread, level);
|
|
166
|
+
return { low: Math.min(share.low, value), high: Math.max(share.high, value) };
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* This function gives the value of a law at a load point, with its interval. The point gives each
|
|
171
|
+
* variable of the law, and no other name.
|
|
172
|
+
*
|
|
173
|
+
* The function evaluates the program with the constants, then with each replicate. The interval is
|
|
174
|
+
* the share `level` of the values of the replicates, made wider to hold the value.
|
|
175
|
+
*
|
|
176
|
+
* The function gives a problem for a point that the law cannot read. It also gives a problem for a
|
|
177
|
+
* level that the law cannot show, and for a value that is not finite.
|
|
178
|
+
*/
|
|
179
|
+
export const lawValue = (law: Law, point: LoadPoint, options: LevelOptions = {}): Result<LawValue, LawProblem> => {
|
|
180
|
+
const level = options.level ?? DEFAULT_LEVEL;
|
|
181
|
+
const noLevel = levelProblem(law, level, true);
|
|
182
|
+
if (noLevel !== undefined) return err(noLevel);
|
|
183
|
+
const evaluated = pointValuesOf(law, point);
|
|
184
|
+
if (evaluated.tag === "err") return evaluated;
|
|
185
|
+
const [value, ...spread] = evaluated.value.values;
|
|
186
|
+
const interval = intervalOf(value, spread, level);
|
|
187
|
+
return ok({ value, unit: law.measure.unit, level, interval, run: runOf(law, interval, level), outside: evaluated.value.outside });
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* This function gives each constant of a law with the interval of its replicates. It gives a
|
|
192
|
+
* problem for a level that the law cannot show.
|
|
193
|
+
*/
|
|
194
|
+
export const constantIntervals = (law: Law, options: LevelOptions = {}): Result<readonly ConstantInterval[], LawProblem> => {
|
|
195
|
+
const level = options.level ?? DEFAULT_LEVEL;
|
|
196
|
+
const noLevel = levelProblem(law, level, false);
|
|
197
|
+
if (noLevel !== undefined) return err(noLevel);
|
|
198
|
+
return ok(
|
|
199
|
+
law.constants.map((constant, index) => ({
|
|
200
|
+
id: constant.id,
|
|
201
|
+
value: constant.value,
|
|
202
|
+
level,
|
|
203
|
+
interval: rangeOf(law.replicates.map((row) => row[index] as number), level),
|
|
204
|
+
})),
|
|
205
|
+
);
|
|
206
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { convertible, isQuantity, kindOf } from "@mark1russell7/quantity";
|
|
2
|
+
import { NAME, SLUG, isUtc } from "./names.ts";
|
|
3
|
+
import { RATE, WORKLOAD_FORMAT, type Amounts, type Demand, type Workload, type WorkloadProblem } from "./workload.ts";
|
|
4
|
+
|
|
5
|
+
/** This function makes a problem of a workload. */
|
|
6
|
+
export const workloadProblem = (code: WorkloadProblem["code"], path: string, message: string): WorkloadProblem => ({
|
|
7
|
+
code,
|
|
8
|
+
path,
|
|
9
|
+
message,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
/** This function gives the problems of some amounts: the name and the amount of each. */
|
|
13
|
+
const amountProblems = (amounts: Amounts, path: string, owner: string): readonly WorkloadProblem[] => {
|
|
14
|
+
const problems: WorkloadProblem[] = [];
|
|
15
|
+
for (const [name, amount] of Object.entries(amounts)) {
|
|
16
|
+
const at = `${path}.${name}`;
|
|
17
|
+
if (!NAME.test(name)) {
|
|
18
|
+
problems.push(workloadProblem("id", at, `The name "${name}" is not letters and digits that start with a lower-case letter.`));
|
|
19
|
+
}
|
|
20
|
+
if (name === RATE) problems.push(workloadProblem("id", at, `The name "${RATE}" is the rate of a class. ${owner} cannot use it.`));
|
|
21
|
+
if (!isQuantity(amount)) problems.push(workloadProblem("amount", at, `"${name}" is not a finite amount in a canonical unit.`));
|
|
22
|
+
}
|
|
23
|
+
return problems;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* This function examines a demand and gives each problem that it finds. A demand with no problem
|
|
28
|
+
* gives an empty list.
|
|
29
|
+
*
|
|
30
|
+
* It examines the id, the class of the app and the rate of each class. It examines the name and the amount of
|
|
31
|
+
* each variable and each level. It examines that a variable has one kind of unit in each class, and that no name is
|
|
32
|
+
* a level and a variable.
|
|
33
|
+
*/
|
|
34
|
+
export const demandProblems = (demand: Demand): readonly WorkloadProblem[] => {
|
|
35
|
+
const problems: WorkloadProblem[] = [];
|
|
36
|
+
/** The class that first gives each variable of a request, and the unit of the variable there. */
|
|
37
|
+
const first = new Map<string, { readonly class: string; readonly unit: string | undefined }>();
|
|
38
|
+
const ids = new Set<string>();
|
|
39
|
+
let twice: string | undefined;
|
|
40
|
+
|
|
41
|
+
demand.classes.forEach((entry, index) => {
|
|
42
|
+
const path = `classes[${index}]`;
|
|
43
|
+
if (ids.has(entry.id)) twice ??= entry.id;
|
|
44
|
+
ids.add(entry.id);
|
|
45
|
+
if (!SLUG.test(entry.id)) {
|
|
46
|
+
problems.push(workloadProblem("id", `${path}.id`, `The id "${entry.id}" is not lower-case words joined by ".", "-" or "/".`));
|
|
47
|
+
}
|
|
48
|
+
if (entry.of !== undefined && !SLUG.test(entry.of)) {
|
|
49
|
+
problems.push(workloadProblem("class", `${path}.of`, `The class "${entry.of}" of the app is not lower-case words joined by ".", "-" or "/".`));
|
|
50
|
+
}
|
|
51
|
+
if (!isQuantity(entry.rate)) {
|
|
52
|
+
problems.push(workloadProblem("rate", `${path}.rate`, `The rate of the class "${entry.id}" is not a finite amount in a canonical unit.`));
|
|
53
|
+
} else if (kindOf(entry.rate.unit) !== "frequency") {
|
|
54
|
+
problems.push(
|
|
55
|
+
workloadProblem(
|
|
56
|
+
"rate",
|
|
57
|
+
`${path}.rate`,
|
|
58
|
+
`The rate of the class "${entry.id}" is in "${entry.rate.unit}". A rate is a count in a unit of time, for example in "{request}/s".`,
|
|
59
|
+
),
|
|
60
|
+
);
|
|
61
|
+
} else if (entry.rate.value < 0) {
|
|
62
|
+
problems.push(workloadProblem("rate", `${path}.rate`, `The rate of the class "${entry.id}" is below 0.`));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
problems.push(...amountProblems(entry.request, `${path}.request`, "A variable of a request"));
|
|
66
|
+
for (const [name, amount] of Object.entries(entry.request)) {
|
|
67
|
+
const unit = isQuantity(amount) ? amount.unit : undefined;
|
|
68
|
+
const before = first.get(name);
|
|
69
|
+
if (before === undefined) {
|
|
70
|
+
first.set(name, { class: entry.id, unit });
|
|
71
|
+
} else if (unit !== undefined && before.unit !== undefined && !convertible(before.unit, unit)) {
|
|
72
|
+
problems.push(
|
|
73
|
+
workloadProblem(
|
|
74
|
+
"unit",
|
|
75
|
+
`${path}.request.${name}`,
|
|
76
|
+
`The variable "${name}" is in "${unit}" here, and it is in "${before.unit}" in the class "${before.class}".`,
|
|
77
|
+
),
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
if (twice !== undefined) problems.push(workloadProblem("id", "classes", `Two classes have the id "${twice}".`));
|
|
83
|
+
|
|
84
|
+
problems.push(...amountProblems(demand.levels, "levels", "A level"));
|
|
85
|
+
for (const name of Object.keys(demand.levels)) {
|
|
86
|
+
const before = first.get(name);
|
|
87
|
+
if (before !== undefined) {
|
|
88
|
+
problems.push(workloadProblem("id", `levels.${name}`, `"${name}" names a level and a variable of the class "${before.class}".`));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return problems;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* This function examines a workload and gives each problem that it finds. A workload with no
|
|
96
|
+
* problem gives an empty list.
|
|
97
|
+
*
|
|
98
|
+
* It examines the format, the id and the title. It examines the demand with `demandProblems`. It
|
|
99
|
+
* examines that a plan has a note. It examines that the window of a metrics source goes from a UTC
|
|
100
|
+
* time to a later UTC time.
|
|
101
|
+
*/
|
|
102
|
+
export const checkWorkload = (workload: Workload): readonly WorkloadProblem[] => {
|
|
103
|
+
const problems: WorkloadProblem[] = [];
|
|
104
|
+
if (workload.format !== WORKLOAD_FORMAT) {
|
|
105
|
+
problems.push(workloadProblem("format", "format", `The format is "${String(workload.format)}", not "${WORKLOAD_FORMAT}".`));
|
|
106
|
+
}
|
|
107
|
+
if (!SLUG.test(workload.id)) {
|
|
108
|
+
problems.push(workloadProblem("id", "id", `The id "${workload.id}" is not lower-case words joined by ".", "-" or "/".`));
|
|
109
|
+
}
|
|
110
|
+
if (workload.title.trim() === "") problems.push(workloadProblem("id", "title", "The title is empty."));
|
|
111
|
+
problems.push(...demandProblems(workload));
|
|
112
|
+
|
|
113
|
+
const source = workload.source;
|
|
114
|
+
if (source.kind === "plan") {
|
|
115
|
+
if (source.note.trim() === "") problems.push(workloadProblem("source", "source.note", "The note of the plan is empty."));
|
|
116
|
+
return problems;
|
|
117
|
+
}
|
|
118
|
+
if (source.origin.trim() === "") problems.push(workloadProblem("source", "source.origin", "The origin of the metrics is empty."));
|
|
119
|
+
for (const end of ["from", "to"] as const) {
|
|
120
|
+
if (!isUtc(source[end])) {
|
|
121
|
+
problems.push(
|
|
122
|
+
workloadProblem("source", `source.${end}`, `The time "${source[end]}" is not a UTC time, for example "2026-11-18T14:00:00Z".`),
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (isUtc(source.from) && isUtc(source.to) && !(Date.parse(source.from) < Date.parse(source.to))) {
|
|
127
|
+
problems.push(workloadProblem("source", "source", "The window of the metrics does not go from a time to a later time."));
|
|
128
|
+
}
|
|
129
|
+
return problems;
|
|
130
|
+
};
|
package/src/workload.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Quantity } from "@mark1russell7/quantity";
|
|
2
|
+
import type { Problem } from "./law.ts";
|
|
3
|
+
|
|
4
|
+
/** This text is the format of a workload file. */
|
|
5
|
+
export const WORKLOAD_FORMAT = "capacity-workload/1";
|
|
6
|
+
|
|
7
|
+
/** This name is the variable that gives the rate of a class. A class and a level cannot use it for a different amount. */
|
|
8
|
+
export const RATE = "rate";
|
|
9
|
+
|
|
10
|
+
/** Amounts are amounts with names. Each amount is in a canonical unit of `@mark1russell7/quantity`. */
|
|
11
|
+
export type Amounts = Readonly<Record<string, Quantity>>;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A workload class is one kind of request. Its `rate` tells how many requests arrive in a unit of
|
|
15
|
+
* time, for example in `{request}/s`. Its `request` gives each variable of one request, for example
|
|
16
|
+
* the size of its body and its duration.
|
|
17
|
+
*
|
|
18
|
+
* A class is one point. Requests of two sizes are two classes, each with its own rate.
|
|
19
|
+
*
|
|
20
|
+
* The field `of` names the class of the contract of the app that the class is a point of. A class
|
|
21
|
+
* with no `of` is a point of the class of the app with its own id.
|
|
22
|
+
*/
|
|
23
|
+
export interface WorkloadClass {
|
|
24
|
+
readonly id: string;
|
|
25
|
+
readonly of?: string | undefined;
|
|
26
|
+
readonly rate: Quantity;
|
|
27
|
+
readonly request: Amounts;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A demand is the load that clients put on a service: its classes and its levels. A level is an
|
|
32
|
+
* amount that stays in the service and does not come with each request, for example the open
|
|
33
|
+
* connections.
|
|
34
|
+
*
|
|
35
|
+
* A demand is extensive. The rates and the levels of two demands add, and they divide between the
|
|
36
|
+
* instances of a service. The variables of one request do not.
|
|
37
|
+
*/
|
|
38
|
+
export interface Demand {
|
|
39
|
+
readonly classes: readonly WorkloadClass[];
|
|
40
|
+
readonly levels: Amounts;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A plan is a workload that a person wrote. Its `note` tells where its numbers come from. */
|
|
44
|
+
export interface PlanSource {
|
|
45
|
+
readonly kind: "plan";
|
|
46
|
+
readonly note: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A metrics source is a workload from the live metrics of an app. Its `origin` names the place of
|
|
51
|
+
* the metrics. The window goes from `from` to `to`, and each is a UTC time.
|
|
52
|
+
*/
|
|
53
|
+
export interface MetricsSource {
|
|
54
|
+
readonly kind: "metrics";
|
|
55
|
+
readonly origin: string;
|
|
56
|
+
readonly from: string;
|
|
57
|
+
readonly to: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A workload source tells where the numbers of a workload come from: a plan or the live metrics. */
|
|
61
|
+
export type WorkloadSource = PlanSource | MetricsSource;
|
|
62
|
+
|
|
63
|
+
/** A workload is a demand with a name and a source. A workload file is this record as JSON. */
|
|
64
|
+
export interface Workload extends Demand {
|
|
65
|
+
readonly format: typeof WORKLOAD_FORMAT;
|
|
66
|
+
readonly id: string;
|
|
67
|
+
readonly title: string;
|
|
68
|
+
readonly source: WorkloadSource;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A workload problem code names the part of a workload, or of a query, that is not correct. */
|
|
72
|
+
export type WorkloadProblemCode =
|
|
73
|
+
| "shape"
|
|
74
|
+
| "format"
|
|
75
|
+
| "id"
|
|
76
|
+
| "rate"
|
|
77
|
+
| "amount"
|
|
78
|
+
| "unit"
|
|
79
|
+
| "class"
|
|
80
|
+
| "source"
|
|
81
|
+
| "factor"
|
|
82
|
+
| "variable";
|
|
83
|
+
|
|
84
|
+
/** A workload problem tells what is not correct, and where: `path` is the path of the field in the workload. */
|
|
85
|
+
export type WorkloadProblem = Problem<WorkloadProblemCode>;
|
|
86
|
+
|
|
87
|
+
/** This demand has no class and no level. It is the zero of the sum of demands. */
|
|
88
|
+
export const NO_DEMAND: Demand = { classes: [], levels: {} };
|