Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/wasm-rquickjs/skeleton/Cargo.toml_
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ timezone = ["dep:chrono-tz"]
typescript-runtime = ["dep:swc_common", "dep:swc_ts_fast_strip"]
typescript-transform-runtime = ["typescript-runtime"]

# Private measurement hooks for the GOL-360 TCP write/backpressure benchmark.
net-write-profiling = []

# WebSocket support via the target-specific `golem:websocket@1.5.0` bindings.
websocket = ["dep:golem-websocket"]

Expand Down
76 changes: 76 additions & 0 deletions crates/wasm-rquickjs/skeleton/src/builtin/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ function forwardNativeHandle(wrap, handle) {
wrap.local_address = handle.local_address.bind(handle);
wrap.set_no_delay = handle.set_no_delay.bind(handle);
wrap.set_keep_alive = handle.set_keep_alive.bind(handle);
// The bridge method must exist in every build; only profiling builds return JSON.
// Probe once so ordinary sockets allocate no counters or listeners.
const initialNativeProfile = typeof handle.write_profile === 'function'
? handle.write_profile()
: null;
if (typeof initialNativeProfile === 'string') {
wrap._writeProfile = {
writeCalls: 0,
writeBytes: 0,
writevCalls: 0,
writevBytes: 0,
concatCalls: 0,
concatBytes: 0,
serializedWritevChunks: 0,
nativeCrossings: 0,
completions: 0,
drainEvents: 0,
elapsedMs: 0,
nativeReadCrossings: 0,
readChunks: 0,
readBytes: 0,
readElapsedMs: 0,
streamReadRequests: 0,
lastReadRequestSize: 0,
};
wrap.get_write_profile = () => ({
js: { ...wrap._writeProfile },
native: JSON.parse(handle.write_profile()),
});
}
}

// IPC path → TCP loopback mapping for in-process Unix socket emulation
Expand Down Expand Up @@ -958,6 +988,10 @@ Socket.prototype._read = function _read(n) {
return;
}
if (!this._handle || this.destroyed) return;
if (this._handle._writeProfile) {
this._handle._writeProfile.streamReadRequests++;
this._handle._writeProfile.lastReadRequestSize = n;
}
if (this._pendingReadChunks.length > 0) {
this._drainPendingReadChunks();
if (this._netPaused || this._pendingReadChunks.length > 0) return;
Expand Down Expand Up @@ -1007,7 +1041,11 @@ Socket.prototype._startPollLoop = function _startPollLoop() {
while (this._reading && this._handle && token === this._readToken) {
try {
this._readInFlight = true;
const profile = this._handle._writeProfile;
const readStartedAt = profile ? Date.now() : 0;
if (profile) profile.nativeReadCrossings++;
const chunk = await this._handle.read(16384);
if (profile) profile.readElapsedMs += Date.now() - readStartedAt;
this._readInFlight = false;
if (token !== this._readToken) break;
if (chunk === null || chunk === undefined) {
Expand All @@ -1019,6 +1057,10 @@ Socket.prototype._startPollLoop = function _startPollLoop() {
}
break;
}
if (profile) {
profile.readChunks++;
profile.readBytes += chunk.length;
}
this.bytesRead += chunk.length;
this._resetTimeout();
const buffer = Buffer.from(chunk);
Expand Down Expand Up @@ -1055,17 +1097,30 @@ Socket.prototype._write = function _write(chunk, encoding, callback) {
const data = typeof chunk === 'string' ? Buffer.from(chunk, encoding) : chunk;
const buf = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
const handle = this._handle;
const profile = handle._writeProfile;
const startedAt = profile ? Date.now() : 0;
if (profile) {
profile.writeCalls++;
profile.writeBytes += buf.byteLength;
profile.nativeCrossings++;
if (!this._writeProfileDrainListener) {
this._writeProfileDrainListener = true;
this.on('drain', () => profile.drainEvents++);
}
}
handle.writeQueueSize += buf.byteLength;

(async () => {
try {
const written = await handle.write(buf);
this._bytesDispatched += written;
this._resetTimeout();
if (profile) profile.completions++;
callback(null);
} catch (e) {
callback(parseNativeError(e));
} finally {
if (profile) profile.elapsedMs += Date.now() - startedAt;
handle.writeQueueSize = Math.max(0, handle.writeQueueSize - buf.byteLength);
}
})();
Expand All @@ -1080,15 +1135,25 @@ Socket.prototype._writev = function _writev(chunks, callback) {
: Buffer.from(chunk)
));
const totalLength = buffers.reduce((total, buffer) => total + buffer.length, 0);
const profile = this._handle && this._handle._writeProfile;
if (profile) {
profile.writevCalls++;
profile.writevBytes += totalLength;
}

// Coalesce ordinary corked writes (notably HTTP response framing) while
// bounding the temporary Buffer.concat allocation for large batches.
if (totalLength <= 64 * 1024) {
if (profile) {
profile.concatCalls++;
profile.concatBytes += totalLength;
}
this._write(Buffer.concat(buffers, totalLength), 'buffer', callback);
return;
}

let index = 0;
if (profile) profile.serializedWritevChunks += buffers.length;
const writeNext = (error) => {
if (error || index === buffers.length) {
callback(error || null);
Expand Down Expand Up @@ -1130,6 +1195,17 @@ Socket.prototype._destroy = function _destroy(err, callback) {
this._connectingHandle = null;
}
if (this._handle) {
if (typeof this._handle.get_write_profile === 'function') {
try {
const profile = this._handle.get_write_profile();
if (
profile.native.requestedBytes > 0 ||
profile.native.completedReadBytes > 0
) {
console.error('[net-write-profile]' + JSON.stringify(profile));
}
} catch (_) {}
}
this._handle.close();
this._handle = null;
}
Expand Down
Loading
Loading