-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory.ts
More file actions
75 lines (70 loc) · 1.79 KB
/
Copy pathmemory.ts
File metadata and controls
75 lines (70 loc) · 1.79 KB
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
import type { getBuilder } from "@joyautomation/conch";
export enum MemoryUsageUnits {
BYTES,
KB,
MB,
GB,
}
export type MemoryUsage = {
rss: number;
heapTotal: number;
heapUsed: number;
external: number;
};
export function convertBytesToUnits(
bytes: number,
units?: MemoryUsageUnits | null
) {
switch (units) {
case MemoryUsageUnits.KB:
return bytes / 1024;
case MemoryUsageUnits.MB:
return bytes / (1024 * 1024);
case MemoryUsageUnits.GB:
return bytes / (1024 * 1024 * 1024);
default:
return bytes;
}
}
export const convertDenoMemoryUsage = (
memoryUsage: MemoryUsage,
units?: MemoryUsageUnits | null
) => ({
rss: convertBytesToUnits(memoryUsage.rss, units),
heapTotal: convertBytesToUnits(memoryUsage.heapTotal, units),
heapUsed: convertBytesToUnits(memoryUsage.heapUsed, units),
external: convertBytesToUnits(memoryUsage.external, units),
});
export function addMemoryUsageToSchema(builder: ReturnType<typeof getBuilder>) {
const MemoryUsageRef =
builder.objectRef<ReturnType<typeof Deno.memoryUsage>>("MemoryUsage");
const MemoryUsageUnitsRef = builder.enumType(MemoryUsageUnits, {
name: "MemoryUsageUnits",
});
MemoryUsageRef.implement({
fields: (t) => ({
rss: t.expose("rss", {
type: "Float",
}),
heapTotal: t.expose("heapTotal", {
type: "Float",
}),
heapUsed: t.expose("heapUsed", {
type: "Float",
}),
external: t.expose("external", {
type: "Float",
}),
}),
});
builder.queryField("memoryUsage", (t) =>
t.field({
args: {
units: t.arg({ type: MemoryUsageUnitsRef }),
},
type: MemoryUsageRef,
resolve: (_root, args) =>
convertDenoMemoryUsage(Deno.memoryUsage(), args.units),
})
);
}