From fb566bdd3556fa655a3fd256c3772f0f96e428d5 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Wed, 30 Nov 2011 23:30:44 -0700
Subject: [PATCH] (loadgen) optimization - re-use assertions to generate
 synthetic load without the client compute cost

---
 lib/load_gen/common.js |  4 ++--
 lib/load_gen/crypto.js | 41 ++++++++++++++++++++++++++++++++++-------
 2 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/lib/load_gen/common.js b/lib/load_gen/common.js
index 619b989dd..cd31954fb 100644
--- a/lib/load_gen/common.js
+++ b/lib/load_gen/common.js
@@ -78,8 +78,8 @@ exports.genAssertionAndVerify = function(cfg, user, ctx, email, audience, cb) {
     });
 
     wcli.post(cfg, '/verify', {}, {
-      audience: audience,
-      assertion: assertion
+      audience: assertion.audience,
+      assertion: assertion.assertion
     }, function (r) {
       try {
         if (r.code !== 200) throw "non-200 status: " + resp.code;
diff --git a/lib/load_gen/crypto.js b/lib/load_gen/crypto.js
index aa4735336..e8ca1c434 100644
--- a/lib/load_gen/crypto.js
+++ b/lib/load_gen/crypto.js
@@ -27,12 +27,39 @@ exports.getKeyPair = function() {
   return userDB.any(keyPairs);
 };
 
+var assertions = [];
+
 exports.getAssertion = function(obj) {
-  // XXX: we can memoize here at some point, returning existing assertions
-  // to reduce compute cost of loadgen client, to simulate more load
-  // on servers
-  var expirationDate = new Date(obj.now.getTime() + (2 * 60 * 1000));
-  var tok = new jwt.JWT(null, expirationDate, obj.audience);
-  var assertion = vep.bundleCertsAndAssertion([obj.cert], tok.sign(obj.secretKey));
-  return assertion;
+  // we can memoize here, returning existing assertions to reduce
+  // compute cost of loadgen client, to simulate more load on servers
+
+  // this is a synthetic benchmark and for assertions we don't really care
+  // what email or RP is associated with the assertion, just that
+  // it applies load.
+
+  function genAssertion() {
+    var expirationDate = new Date(obj.now.getTime() + (2 * 60 * 1000));
+    var tok = new jwt.JWT(null, expirationDate, obj.audience);
+    var assertion = vep.bundleCertsAndAssertion([obj.cert], tok.sign(obj.secretKey));
+
+    return {
+      audience: obj.audience,
+      assertion: assertion,
+      expirationDate: expirationDate
+    };
+  }
+
+  if (assertions.length >= 30) {
+    var which = Math.floor(Math.random()*30)
+    var assertion = assertions[which];
+    // consider assertions which expire in the next minute stale
+    if ((assertion.expirationDate - new Date()) < (60 * 1000)) {
+      assertion = assertions[which] = genAssertion();
+    }
+    return assertions[which];
+  }
+
+  var a = genAssertion();
+  assertions.push(a);
+  return a;
 };
-- 
GitLab