Open History to see the last commit
1import * as NodeAssert from "node:assert/strict";
2import * as NodeCrypto from "node:crypto";
3import * as NodeModule from "node:module";
4import * as NodeURL from "node:url";
5
6const root = process.argv[2];
7const count = Number(process.argv[3] ?? 4);
8const bytes = Number(process.argv[4] ?? 32768);
9const require = NodeModule.createRequire(root + "/packages/shared/package.json");
10const load = (name) => import(NodeURL.pathToFileURL(require.resolve("effect/" + name)));
11const [Effect, Cache, Tracer] = await Promise.all(["Effect", "Cache", "Tracer"].map(load));
12const { makeLocalFileTracer } = await import(
13 NodeURL.pathToFileURL(root + "/packages/shared/src/observability.ts")
14);
15const refs = [];
16let exported = 0;
17const tracer = await Effect.runPromise(
18 makeLocalFileTracer({
19 filePath: "unused",
20 maxBytes: 1024,
21 maxFiles: 1,
22 batchWindowMs: 10000,
23 sink: {
24 filePath: "unused",
25 push(record) {
26 NodeAssert.deepEqual(record.exit, { _tag: "Success" });
27 exported++;
28 },
29 flush: Effect.void,
30 close: () => Effect.void,
31 },
32 }),
33);
34const cache = await Effect.runPromise(
35 Cache.make({ capacity: count, timeToLive: "10 minutes", lookup: (key) => Effect.succeed(key) }),
36);
37
38async function read(index) {
39 const result = await Effect.runPromise(
40 Effect.gen(function* () {
41 const projection = JSON.parse(
42 JSON.stringify({
43 turnItems: [{ output: [{ text: NodeCrypto.randomBytes(bytes).toString("hex") }] }],
44 }),
45 );
46 refs.push(new WeakRef(projection));
47 yield* Cache.get(cache, index);
48 return projection;
49 }).pipe(
50 Effect.withSpan("read-thread-projection"),
51 Effect.provideService(Tracer.Tracer, tracer),
52 ),
53 );
54 NodeAssert.equal(result.turnItems[0].output[0].text.length, bytes * 2);
55}
56
57for (let index = 0; index < count; index++) await read(index);
58for (let index = 0; index < 3; index++) {
59 await new Promise(setImmediate);
60 global.gc();
61}
62console.log(
63 JSON.stringify({
64 cacheSize: await Effect.runPromise(Cache.size(cache)),
65 retained: refs.filter((ref) => ref.deref()).length,
66 exported,
67 payloadMiB: (count * bytes * 2) / 1048576,
68 heapMiB: Math.round(process.memoryUsage().heapUsed / 1048576),
69 }),
70);