jenkins-bot has submitted this change and it was merged.

Change subject: Add --record and --replay options to parse.js
......................................................................


Add --record and --replay options to parse.js

* --record will record http requests in nocks/$prefix/$title.js
* --replay will look for the nock file there and use it to
  replay http requests
* Useful for performance testing by eliminating network latencies
  and freezing wikitext, templates, etc.

Change-Id: Iabe2860d7e2070e0daf3e5ce442d21d55a8f5590
---
M .gitignore
M bin/parse.js
M npm-shrinkwrap.json
M package.json
4 files changed, 97 insertions(+), 0 deletions(-)

Approvals:
  Arlolra: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/.gitignore b/.gitignore
index 42090f4..e103d22 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,3 +17,4 @@
 coverage/
 contrib/
 extensions/
+nocks/
diff --git a/bin/parse.js b/bin/parse.js
index ac81cb9..a735d3f 100755
--- a/bin/parse.js
+++ b/bin/parse.js
@@ -148,6 +148,16 @@
                'boolean': true,
                'default': false,
        },
+       'record': {
+               description: 'Record http requests for later replay',
+               'boolean': true,
+               'default': false,
+       },
+       'replay': {
+               description: 'Replay recorded http requests for later replay',
+               'boolean': true,
+               'default': false,
+       },
        'useBatchAPI': {
                description: 'Turn on/off the API batching system',
                // Since I picked a null default (to let the default config 
setting be the default),
@@ -385,12 +395,51 @@
                parsoidConfig.defaultWiki = prefix ? prefix :
                        parsoidConfig.reverseMwApiMap.get(domain);
 
+               var nock, dir, nocksFile;
+               if (argv.record || argv.replay) {
+                       prefix = prefix || 'enwiki';
+                       dir = path.resolve(__dirname, '../nocks/');
+                       if (!fs.existsSync(dir)) {
+                               fs.mkdirSync(dir);
+                       }
+                       dir = dir + '/' + prefix;
+                       if (!fs.existsSync(dir)) {
+                               fs.mkdirSync(dir);
+                       }
+                       nocksFile = dir + '/' + argv.page + '.js';
+               }
+
+               if (argv.record) {
+                       nock = require('nock');
+                       nock.recorder.rec({dont_print: true});
+               } else if (argv.replay) {
+                       try {
+                               require(nocksFile);
+                       } catch (e) {
+                               console.error('Exception ' + e + ' requiring ' 
+ nocksFile);
+                               console.error('Cannot replay!');
+                               return -1;
+                       }
+               }
+
                return parse(null, argv, parsoidConfig, prefix, 
domain).then(function(res) {
                        var stdout = process.stdout;
                        stdout.write(res.out);
                        if (res.trailingNL && stdout.isTTY) {
                                stdout.write('\n');
                        }
+
+                       if (argv.record) {
+                               var nockCalls = nock.recorder.play();
+                               var stream = fs.createWriteStream(nocksFile);
+                               stream.once('open', function() {
+                                       stream.write("var nock = 
require('nock');");
+                                       for (var i = 0; i < nockCalls.length; 
i++) {
+                                               stream.write(nockCalls[i]);
+                                       }
+                                       stream.end();
+                               });
+                       }
                }).done();
        }());
 }
diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json
index 22feda4..830931e 100644
--- a/npm-shrinkwrap.json
+++ b/npm-shrinkwrap.json
@@ -2260,6 +2260,52 @@
       "from": 
"negotiator@git+https://github.com/arlolra/negotiator.git#0418ab4e9a665772b7e233564a4525c9d9a8ec3a";,
       "resolved": 
"git+https://github.com/arlolra/negotiator.git#0418ab4e9a665772b7e233564a4525c9d9a8ec3a";
     },
+    "nock": {
+      "version": "8.0.0",
+      "resolved": "https://registry.npmjs.org/nock/-/nock-8.0.0.tgz";,
+      "dependencies": {
+        "debug": {
+          "version": "2.2.0",
+          "resolved": "https://registry.npmjs.org/debug/-/debug-2.2.0.tgz";,
+          "dependencies": {
+            "ms": {
+              "version": "0.7.1",
+              "resolved": "https://registry.npmjs.org/ms/-/ms-0.7.1.tgz";
+            }
+          }
+        },
+        "deep-equal": {
+          "version": "1.0.1",
+          "resolved": 
"https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz";
+        },
+        "json-stringify-safe": {
+          "version": "5.0.1",
+          "resolved": 
"https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz";
+        },
+        "lodash": {
+          "version": "4.16.4",
+          "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.16.4.tgz";
+        },
+        "mkdirp": {
+          "version": "0.5.1",
+          "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz";,
+          "dependencies": {
+            "minimist": {
+              "version": "0.0.8",
+              "resolved": 
"https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz";
+            }
+          }
+        },
+        "propagate": {
+          "version": "0.4.0",
+          "resolved": 
"https://registry.npmjs.org/propagate/-/propagate-0.4.0.tgz";
+        },
+        "qs": {
+          "version": "6.2.1",
+          "resolved": "https://registry.npmjs.org/qs/-/qs-6.2.1.tgz";
+        }
+      }
+    },
     "node-uuid": {
       "version": "1.4.7",
       "resolved": "https://registry.npmjs.org/node-uuid/-/node-uuid-1.4.7.tgz";
diff --git a/package.json b/package.json
index a82682b..855f7c4 100644
--- a/package.json
+++ b/package.json
@@ -41,6 +41,7 @@
     "jscs": "^3.0.7",
     "jshint": "^2.9.2",
     "mocha": "^2.5.3",
+    "nock": "^8.0.0",
     "npm-shrinkwrap": "^200.5.1",
     "nsp": "^2.6.1",
     "supertest": "^1.2.0"

-- 
To view, visit https://gerrit.wikimedia.org/r/314785
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iabe2860d7e2070e0daf3e5ce442d21d55a8f5590
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: Arlolra <[email protected]>
Gerrit-Reviewer: C. Scott Ananian <[email protected]>
Gerrit-Reviewer: GWicke <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Ori.livneh <[email protected]>
Gerrit-Reviewer: Ppchelko <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to