Skip to content
Closed
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
43 changes: 43 additions & 0 deletions benchmark/https/https-session-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const common = require('../common.js');
const https = require('https');
const crypto = require('crypto');

const bench = common.createBenchmark(main, {
n: [100, 1000, 10000],
sessions: [10, 100, 256],
});

function generateSessionId() {

Check failure on line 12 in benchmark/https/https-session-cache.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'generateSessionId' is defined but never used
return crypto.randomBytes(32).toString('hex');
}

function main({ n, sessions }) {
const agent = new https.Agent({
maxCachedSessions: sessions,
});

// Create dummy session objects
const sessionData = Buffer.allocUnsafe(1024);

bench.start();

for (let i = 0; i < n; i++) {
// Simulate session caching operations
const sessionId = `session-${i % sessions}`;

// Cache session
agent._cacheSession(sessionId, sessionData);

// Occasionally evict sessions
if (i % 10 === 0) {
agent._evictSession(`session-${Math.floor(Math.random() * sessions)}`);
}

// Get session
agent._getSession(sessionId);
}

bench.end(n);
}

Check failure on line 43 in benchmark/https/https-session-cache.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Newline required at end of file but not found
23 changes: 14 additions & 9 deletions lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,29 +572,34 @@ Agent.prototype._cacheSession = function _cacheSession(key, session) {
if (this.maxCachedSessions === 0)
return;

// Cache property accesses for better performance
const map = this._sessionCache.map;

// Fast case - update existing entry
if (this._sessionCache.map[key]) {
this._sessionCache.map[key] = session;
if (map[key]) {
map[key] = session;
return;
}

// Put new entry
if (this._sessionCache.list.length >= this.maxCachedSessions) {
const oldKey = ArrayPrototypeShift(this._sessionCache.list);
const list = this._sessionCache.list;
if (list.length >= this.maxCachedSessions) {
const oldKey = ArrayPrototypeShift(list);
debug('evicting %j', oldKey);
delete this._sessionCache.map[oldKey];
delete map[oldKey];
}

ArrayPrototypePush(this._sessionCache.list, key);
this._sessionCache.map[key] = session;
ArrayPrototypePush(list, key);
map[key] = session;
};

Agent.prototype._evictSession = function _evictSession(key) {
const index = ArrayPrototypeIndexOf(this._sessionCache.list, key);
const list = this._sessionCache.list;
const index = ArrayPrototypeIndexOf(list, key);
if (index === -1)
return;

ArrayPrototypeSplice(this._sessionCache.list, index, 1);
ArrayPrototypeSplice(list, index, 1);
delete this._sessionCache.map[key];
};

Expand Down
Loading