diff options
author | carson <carson@leaddyno.com> | 2020-06-17 12:30:52 -0700 |
---|---|---|
committer | carson <carson@leaddyno.com> | 2020-06-17 12:30:52 -0700 |
commit | b6f1c834cfd9935cee7694fc679f71575b7b8964 (patch) | |
tree | 4456451d5d4ed0e411db20dfb740c9326d97f8d0 | |
parent | 19b933449b1af4d4589bee24ae201dbe0c8d374f (diff) | |
download | htmx-b6f1c834cfd9935cee7694fc679f71575b7b8964.tar.gz htmx-b6f1c834cfd9935cee7694fc679f71575b7b8964.zip |
include perf tests
-rw-r--r-- | test/attributes/hx-push-url.js | 53 | ||||
-rw-r--r-- | test/core/perf.js | 71 | ||||
-rw-r--r-- | test/index.html | 1 |
3 files changed, 82 insertions, 43 deletions
diff --git a/test/attributes/hx-push-url.js b/test/attributes/hx-push-url.js index efb91b02..d1f6b137 100644 --- a/test/attributes/hx-push-url.js +++ b/test/attributes/hx-push-url.js @@ -1,15 +1,16 @@ describe("hx-push-url attribute", function() { - var KUTTY_HISTORY_CACHE = "htmx-history-cache"; + var HTMX_HISTORY_CACHE_NAME = "htmx-history-cache"; + beforeEach(function () { this.server = makeServer(); clearWorkArea(); - localStorage.removeItem(KUTTY_HISTORY_CACHE); + localStorage.removeItem(HTMX_HISTORY_CACHE_NAME); }); afterEach(function () { this.server.restore(); clearWorkArea(); - localStorage.removeItem(KUTTY_HISTORY_CACHE); + localStorage.removeItem(HTMX_HISTORY_CACHE_NAME); }); it("navigation should push an element into the cache ", function () { @@ -19,7 +20,7 @@ describe("hx-push-url attribute", function() { div.click(); this.server.respond(); getWorkArea().textContent.should.equal("second") - var cache = JSON.parse(localStorage.getItem(KUTTY_HISTORY_CACHE)); + var cache = JSON.parse(localStorage.getItem(HTMX_HISTORY_CACHE_NAME)); cache.length.should.equal(1); }); @@ -38,7 +39,7 @@ describe("hx-push-url attribute", function() { this.server.respond(); workArea.textContent.should.equal("test2") - var cache = JSON.parse(localStorage.getItem(KUTTY_HISTORY_CACHE)); + var cache = JSON.parse(localStorage.getItem(HTMX_HISTORY_CACHE_NAME)); cache.length.should.equal(2); htmx._('restoreHistory')("/test1") @@ -58,7 +59,7 @@ describe("hx-push-url attribute", function() { byId("d1").click(); this.server.respond(); } - var cache = JSON.parse(localStorage.getItem(KUTTY_HISTORY_CACHE)); + var cache = JSON.parse(localStorage.getItem(HTMX_HISTORY_CACHE_NAME)); cache.length.should.equal(10); // should only be 10 elements }); @@ -77,49 +78,15 @@ describe("hx-push-url attribute", function() { this.server.respond(); workArea.textContent.should.equal("test2") - var cache = JSON.parse(localStorage.getItem(KUTTY_HISTORY_CACHE)); + var cache = JSON.parse(localStorage.getItem(HTMX_HISTORY_CACHE_NAME)); cache.length.should.equal(2); - localStorage.removeItem(KUTTY_HISTORY_CACHE); // clear cache + localStorage.removeItem(HTMX_HISTORY_CACHE_NAME); // clear cache htmx._('restoreHistory')("/test1") this.server.respond(); getWorkArea().textContent.should.equal("test1") }); - function stringRepeat(str, num) { - num = Number(num); - - var result = ''; - while (true) { - if (num & 1) { // (1) - result += str; - } - num >>>= 1; // (2) - if (num <= 0) break; - str += str; - } - - return result; - } - - it("implementation details should be fast", function(){ - // create an entry with a large content string (256k) and see how fast we can write and read it - // to local storage as a single entry - var entry = {url: stringRepeat("x", 32), content:stringRepeat("x", 256*1024)} - var array = []; - for (var i = 0; i < 10; i++) { - array.push(entry); - } - var start = performance.now(); - var string = JSON.stringify(array); - localStorage.setItem(KUTTY_HISTORY_CACHE, string); - var reReadString = localStorage.getItem(KUTTY_HISTORY_CACHE); - var finalJson = JSON.parse(reReadString); - var end = performance.now(); - var timeInMs = end - start; - chai.assert(timeInMs < 300, "Should take less than 300ms on most platforms"); - }) - it("navigation should push an element into the cache w/ data-* prefix", function () { this.server.respondWith("GET", "/test", "second"); getWorkArea().innerHTML.should.be.equal(""); @@ -127,7 +94,7 @@ describe("hx-push-url attribute", function() { div.click(); this.server.respond(); getWorkArea().textContent.should.equal("second") - var cache = JSON.parse(localStorage.getItem(KUTTY_HISTORY_CACHE)); + var cache = JSON.parse(localStorage.getItem(HTMX_HISTORY_CACHE_NAME)); cache.length.should.equal(1); }); diff --git a/test/core/perf.js b/test/core/perf.js new file mode 100644 index 00000000..c0bfac58 --- /dev/null +++ b/test/core/perf.js @@ -0,0 +1,71 @@ +describe("Core htmx perf Tests", function() { + + var HTMX_HISTORY_CACHE_NAME = "htmx-history-cache"; + + beforeEach(function () { + this.server = makeServer(); + clearWorkArea(); + localStorage.removeItem(HTMX_HISTORY_CACHE_NAME); + }); + afterEach(function () { + this.server.restore(); + clearWorkArea(); + localStorage.removeItem(HTMX_HISTORY_CACHE_NAME); + }); + + function stringRepeat(str, num) { + num = Number(num); + + var result = ''; + while (true) { + if (num & 1) { // (1) + result += str; + } + num >>>= 1; // (2) + if (num <= 0) break; + str += str; + } + + return result; + } + + it("DOM processing should be fast", function(){ + this.server.respondWith("GET", "/test", "Clicked!"); + + // create an entry with a large content string (256k) and see how fast we can write and read it + // to local storage as a single entry + var str = stringRepeat("<div>", 30) + stringRepeat("<div><div><span><button hx-get='/test'> Test Get Button </button></span></div></div>\n", 1000) + stringRepeat("</div>", 30); + console.log(str); + var start = performance.now(); + var stuff = make(str); + var end = performance.now(); + var timeInMs = end - start; + + // make sure the DOM actually processed + var firstBtn = stuff.querySelector("button"); + firstBtn.click(); + this.server.respond(); + firstBtn.innerHTML.should.equal("Clicked!"); + + chai.assert(timeInMs < 100, "Should take less than 100ms on most platforms, took: " + timeInMs + "ms"); + }) + + it("history implementation should be fast", function(){ + // create an entry with a large content string (256k) and see how fast we can write and read it + // to local storage as a single entry + var entry = {url: stringRepeat("x", 32), content:stringRepeat("x", 256*1024)} + var array = []; + for (var i = 0; i < 10; i++) { + array.push(entry); + } + var start = performance.now(); + var string = JSON.stringify(array); + localStorage.setItem(HTMX_HISTORY_CACHE_NAME, string); + var reReadString = localStorage.getItem(HTMX_HISTORY_CACHE_NAME); + var finalJson = JSON.parse(reReadString); + var end = performance.now(); + var timeInMs = end - start; + chai.assert(timeInMs < 300, "Should take less than 300ms on most platforms"); + }) + +})
\ No newline at end of file diff --git a/test/index.html b/test/index.html index 358f207f..9d4bba88 100644 --- a/test/index.html +++ b/test/index.html @@ -60,6 +60,7 @@ <script src="core/parameters.js"></script> <script src="core/headers.js"></script> <script src="core/regressions.js"></script> +<script src="core/perf.js"></script> <!-- attribute tests --> <script src="attributes/hx-boost.js"></script> |