Skip to content

Commit a247485

Browse files
committed
Change even more env variables -> cli flags
1 parent 0d6e25e commit a247485

16 files changed

Lines changed: 402 additions & 223 deletions

File tree

templates/keynote-2/bun/bun-server.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ async function rpcTransfer(args: Record<string, unknown>) {
110110
const toId = Number(args.to_id ?? args.to);
111111
const amount = Number(args.amount);
112112

113-
if (!Number.isInteger(fromId) || !Number.isInteger(toId) || !Number.isFinite(amount)) {
113+
if (
114+
!Number.isInteger(fromId) ||
115+
!Number.isInteger(toId) ||
116+
!Number.isFinite(amount)
117+
) {
114118
throw new Error('invalid transfer args');
115119
}
116120
if (fromId === toId || amount <= 0) return;
@@ -174,7 +178,6 @@ async function rpcGetAccount(args: Record<string, unknown>) {
174178
};
175179
}
176180

177-
178181
async function rpcVerify() {
179182
const rawInitial = process.env.SEED_INITIAL_BALANCE;
180183
if (!rawInitial) {

templates/keynote-2/spacetimedb-rust-client/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async fn init_conn(cli: &Common, handle: &Handle) -> (JoinHandle<()>, Recv, Send
5757
let params = WsParams {
5858
compression: Compression::None,
5959
light: true,
60-
confirmed: cli.confirmed_reads.into(),
60+
confirmed: cli.confirmed_reads.unwrap_or(CONFIRMED_READS).into(),
6161
};
6262

6363
let conn = websocket::WsConnection::connect(uri, &cli.module, None, None, params)
@@ -146,7 +146,7 @@ fn bench(cli: &Common, bench: &Bench) {
146146

147147
// Initialize connections.
148148
let connections = bench.connections;
149-
let confirmed_reads = cli.confirmed_reads;
149+
let confirmed_reads = cli.confirmed_reads.unwrap_or(CONFIRMED_READS);
150150
if !cli.quiet {
151151
println!("initializing {connections} connections with confirmed-reads={confirmed_reads}");
152152
}
@@ -261,8 +261,8 @@ struct Common {
261261
#[arg(short, long, default_value = MODULE)]
262262
module: String,
263263

264-
#[arg(long, default_value_t = CONFIRMED_READS)]
265-
confirmed_reads: bool,
264+
#[arg(long)]
265+
confirmed_reads: Option<bool>,
266266

267267
#[arg(long, default_value_t = ACCOUNTS)]
268268
accounts: u32,

templates/keynote-2/src/connectors/bun.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { RpcConnector } from '../core/connectors.ts';
2+
import { bunUrl } from '../opts.ts';
23

3-
export default function bun(
4-
url = process.env.BUN_URL || 'http://127.0.0.1:4000',
5-
): RpcConnector {
4+
export default function bun(url = bunUrl): RpcConnector {
65
if (!url) throw new Error('BUN_URL not set');
76

87
const baseUrl = url.replace(/\/+$/, '');

templates/keynote-2/src/connectors/convex.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { RpcConnector } from '../core/connectors.ts';
2+
import { convexUrl } from '../opts.ts';
23

3-
export default function convex(
4-
url = process.env.CONVEX_URL || 'http://127.0.0.1:3210',
5-
): RpcConnector {
4+
export default function convex(url = convexUrl): RpcConnector {
65
if (!url) throw new Error('CONVEX_URL not set');
76

87
function isWriteConflict(msg: unknown): boolean {

templates/keynote-2/src/connectors/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import cockroach_rpc from './rpc/cockroach_rpc.ts';
66
import sqlite_rpc from './rpc/sqlite_rpc.ts';
77
import supabase_rpc from './rpc/supabase_rpc.ts';
88
import planetscale_pg_rpc from './rpc/planetscale_pg_rpc.ts';
9+
import { ConnectorKey } from '../opts.ts';
910

1011
export const CONNECTORS = {
1112
convex,
@@ -17,5 +18,4 @@ export const CONNECTORS = {
1718
sqlite_rpc,
1819
supabase_rpc,
1920
planetscale_pg_rpc,
20-
};
21-
export type ConnectorKey = keyof typeof CONNECTORS;
21+
} satisfies Record<ConnectorKey, unknown>;

templates/keynote-2/src/connectors/spacetimedb.ts

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,67 @@
11
import type { ReducerConnector } from '../core/connectors';
22
import * as mod from '../../module_bindings';
3+
import {
4+
initialBalance,
5+
stdbConfirmedReads,
6+
stdbModule,
7+
stdbUrl,
8+
} from '../opts';
39

410
export function spacetimedb(
5-
url = process.env.STDB_URL!,
6-
moduleName = process.env.STDB_MODULE!,
11+
url = stdbUrl,
12+
moduleName = stdbModule,
713
): ReducerConnector {
8-
let ready!: Promise<void>;
14+
let ready: ReturnType<typeof Promise.withResolvers<void>>;
915
let conn: mod.DbConnection;
10-
let resolveReady!: () => void;
11-
let rejectReady!: (e: unknown) => void;
12-
13-
function armReady() {
14-
ready = new Promise<void>((res, rej) => {
15-
resolveReady = res;
16-
rejectReady = rej;
17-
});
18-
}
1916

2017
async function connectWithBindings() {
2118
if (!url) throw new Error('STDB_URL not set');
2219
if (!moduleName) throw new Error('STDB_MODULE not set');
2320

2421
const Db = mod.DbConnection;
2522

26-
armReady();
23+
ready = Promise.withResolvers<void>();
2724

2825
const subscriptions: string[] = [];
2926
if (process.env.VERIFY === '1') {
3027
console.log('[spacetimedb] subscribing to accounts');
3128
subscriptions.push('SELECT * FROM accounts');
3229
}
33-
let subscribed = subscriptions.length === 0;
30+
const subscribed = Promise.withResolvers<void>();
31+
if (subscriptions.length === 0) subscribed.resolve();
3432

3533
const builder = Db.builder()
3634
.withUri(url)
3735
.withDatabaseName(moduleName)
38-
.withConfirmedReads(process.env.STDB_CONFIRMED_READS === '1')
36+
.withConfirmedReads(stdbConfirmedReads)
3937
.onConnect((ctx) => {
4038
console.log('[stdb] connected');
4139
const conn = ctx;
4240

43-
resolveReady();
41+
ready.resolve();
4442

4543
if (subscriptions.length > 0) {
4644
conn
4745
.subscriptionBuilder()
4846
.onApplied((_sCtx) => {
49-
subscribed = true;
47+
subscribed.resolve();
5048
})
5149
.onError((ctx) => {
5250
console.error('[stdb] subscription failed', ctx.event?.message);
51+
subscribed.reject(ctx.event);
5352
})
5453
.subscribe(subscriptions);
5554
}
5655
})
5756
.onConnectError((_ctx, err: any) => {
58-
console.error('[stdb] onConnectError', err);
59-
6057
if (err instanceof Error) {
61-
rejectReady(err);
58+
ready.reject(err);
59+
} else if (err && err.error instanceof Error) {
60+
ready.reject(err.error);
6261
} else if (err && typeof err.message === 'string') {
63-
rejectReady(new Error(err.message));
62+
ready.reject(new Error(err.message));
6463
} else {
65-
rejectReady(
64+
ready.reject(
6665
new Error(`Spacetime connection error: ${JSON.stringify(err)}`),
6766
);
6867
}
@@ -71,9 +70,8 @@ export function spacetimedb(
7170

7271
conn = builder.build();
7372

74-
while (!subscribed) {
75-
await new Promise((res) => setTimeout(res, 25));
76-
}
73+
await ready.promise;
74+
await subscribed.promise;
7775
}
7876

7977
return {
@@ -83,9 +81,9 @@ export function spacetimedb(
8381
async open() {
8482
try {
8583
await connectWithBindings();
86-
await ready;
84+
await ready.promise;
8785
} catch (err) {
88-
console.error('[spacetimedb] open() failed', err);
86+
console.error('[spacetimedb] open() failed:', err);
8987
throw err;
9088
}
9189
},
@@ -94,7 +92,7 @@ export function spacetimedb(
9492
try {
9593
conn.disconnect();
9694
} catch (e) {
97-
console.error('[spacetimedb] close() failed', e);
95+
console.error('[spacetimedb] close() failed:', e);
9896
}
9997
},
10098

@@ -111,7 +109,7 @@ export function spacetimedb(
111109
},
112110

113111
async call(fn: string, args: Record<string, any>) {
114-
await ready;
112+
await ready.promise;
115113

116114
switch (fn) {
117115
case 'seed': {
@@ -156,7 +154,7 @@ export function spacetimedb(
156154
async verify() {
157155
if (!conn) throw new Error('SpacetimeDB not connected');
158156

159-
const rawInitial = process.env.SEED_INITIAL_BALANCE;
157+
const rawInitial = initialBalance;
160158
if (!rawInitial) {
161159
console.warn(
162160
'[spacetimedb] SEED_INITIAL_BALANCE not set; skipping verification',

templates/keynote-2/src/core/runner.ts

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@ import { getSpacetimeCommittedTransfers } from './spacetimeMetrics.ts';
55
import { makeCollisionTracker } from './collision_tracker.ts';
66
import { RunResult } from './types.ts';
77
import { BaseConnector } from './connectors.ts';
8-
9-
const OP_TIMEOUT_MS = Number(process.env.BENCH_OP_TIMEOUT_MS ?? '15000');
10-
const MIN_OP_TIMEOUT_MS = Number(process.env.MIN_OP_TIMEOUT_MS ?? '250');
11-
const TAIL_SLACK_MS = Number(process.env.TAIL_SLACK_MS ?? '1000');
12-
const DEFAULT_PRECOMPUTED_TRANSFER_PAIRS = 10_000_000;
8+
import {
9+
benchPipelined,
10+
logErrors,
11+
maxInflightPerWorker,
12+
minOpTimeoutMs,
13+
opTimeoutMs,
14+
precomputedTransferPairs,
15+
tailSlackMs,
16+
useSpacetimeMetricsEndpoint,
17+
verifyTransactions,
18+
} from '../opts.ts';
19+
20+
const OP_TIMEOUT_MS = opTimeoutMs;
21+
const MIN_OP_TIMEOUT_MS = minOpTimeoutMs;
22+
const TAIL_SLACK_MS = tailSlackMs;
23+
const PRECOMPUTED_TRANSFER_PAIRS = precomputedTransferPairs;
1324

1425
function precomputeZipfTransferPairs(
1526
accounts: number,
@@ -101,8 +112,7 @@ export async function runOne({
101112
}
102113

103114
const useSpacetimeMetrics =
104-
process.env.USE_SPACETIME_METRICS_ENDPOINT === '1' &&
105-
connector.name === 'spacetimedb';
115+
useSpacetimeMetricsEndpoint && connector.name === 'spacetimedb';
106116
let beforeTransfers: bigint | null = null;
107117

108118
if (useSpacetimeMetrics) {
@@ -126,13 +136,7 @@ export async function runOne({
126136
}
127137
}
128138

129-
const precomputedPairsRaw = Number(
130-
process.env.BENCH_PRECOMPUTED_TRANSFER_PAIRS ??
131-
DEFAULT_PRECOMPUTED_TRANSFER_PAIRS,
132-
);
133-
const precomputedPairs = Number.isFinite(precomputedPairsRaw)
134-
? Math.max(1, Math.floor(precomputedPairsRaw))
135-
: DEFAULT_PRECOMPUTED_TRANSFER_PAIRS;
139+
const precomputedPairs = PRECOMPUTED_TRANSFER_PAIRS;
136140

137141
console.log(
138142
`[${connector.name}] precomputing ${precomputedPairs} Zipf transfer pairs...`,
@@ -148,27 +152,13 @@ export async function runOne({
148152
`[${connector.name}] precomputed ${transferPairs.count} pairs in ${(precomputeElapsedMs / 1000).toFixed(2)}s`,
149153
);
150154

151-
const getEnvTernary = (envVal: string | undefined) => {
152-
switch (envVal) {
153-
case '0':
154-
return false;
155-
case '1':
156-
return true;
157-
default:
158-
return null;
159-
}
160-
};
161-
162-
const PIPELINED =
163-
getEnvTernary(process.env.BENCH_PIPELINED) ??
164-
!!connector.maxInflightPerWorker;
165-
const MAX_INFLIGHT_ENV = process.env.MAX_INFLIGHT_PER_WORKER;
155+
const PIPELINED = benchPipelined ?? !!connector.maxInflightPerWorker;
166156
const MAX_INFLIGHT_PER_WORKER =
167-
MAX_INFLIGHT_ENV == null
157+
maxInflightPerWorker === undefined
168158
? (connector.maxInflightPerWorker ?? 8)
169-
: MAX_INFLIGHT_ENV === '0'
159+
: maxInflightPerWorker == 0
170160
? Infinity
171-
: Number(MAX_INFLIGHT_ENV);
161+
: maxInflightPerWorker;
172162

173163
console.log(
174164
`[${connector.name}] max inflight per worker: ${MAX_INFLIGHT_PER_WORKER}`,
@@ -240,7 +230,7 @@ export async function runOne({
240230
);
241231
ok = true;
242232
} catch (err) {
243-
if (process.env.LOG_ERRORS === '1') {
233+
if (logErrors) {
244234
const msg =
245235
err instanceof Error
246236
? `${err.name}: ${err.message}`
@@ -292,7 +282,7 @@ export async function runOne({
292282
hist.recordValue(Math.max(1, Math.round((t1 - t0) * 1e3)));
293283
}
294284
} catch (err) {
295-
if (process.env.LOG_ERRORS === '1') {
285+
if (logErrors) {
296286
const msg =
297287
err instanceof Error
298288
? `${err instanceof Error ? err.message : String(err)}`
@@ -383,10 +373,10 @@ export async function runOne({
383373
return { start, completedWithinWindow, completedTotal, committedDelta };
384374
};
385375

386-
const warmUpSeconds = 5;
387-
console.log(`[${connector.name}] Warming up for ${warmUpSeconds}s...`);
388-
await run(warmUpSeconds);
389-
console.log(`[${connector.name}] Finished warmup.`);
376+
// const warmUpSeconds = 5;
377+
// console.log(`[${connector.name}] Warming up for ${warmUpSeconds}s...`);
378+
// await run(warmUpSeconds);
379+
// console.log(`[${connector.name}] Finished warmup.`);
390380

391381
console.log(`[${connector.name}] Starting workers for ${seconds}s run...`);
392382

@@ -397,7 +387,7 @@ export async function runOne({
397387
`[${connector.name}] All workers finished (including in-flight ops)`,
398388
);
399389

400-
if (process.env.VERIFY === '1') {
390+
if (verifyTransactions) {
401391
console.log(`[${connector.name}] Running verification pass...`);
402392
try {
403393
await withOpTimeout(connector.verify(), `${connector.name} verify()`);

0 commit comments

Comments
 (0)