From 83fad4c52fd27456921ef68f71e9b00e56f27bcd Mon Sep 17 00:00:00 2001
From: Zach Pomerantz <zach@highfidelity.io>
Date: Fri, 6 May 2016 14:19:13 -0700
Subject: [PATCH 1/4] Fix equalities in scriptableResource tests

---
 scripts/developer/tests/scriptableResource/lib.js | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/scripts/developer/tests/scriptableResource/lib.js b/scripts/developer/tests/scriptableResource/lib.js
index 5241d0968e..255b3bd036 100644
--- a/scripts/developer/tests/scriptableResource/lib.js
+++ b/scripts/developer/tests/scriptableResource/lib.js
@@ -26,8 +26,8 @@ function getFrame(callback) {
     }
 
     function makeFrame(state) {
-        if (state == Resource.State.FAILED) { throw "Failed to load frame"; }
-        if (state != Resource.State.FINISHED) { return; }
+        if (state === Resource.State.FAILED) { throw "Failed to load frame"; }
+        if (state !== Resource.State.FINISHED) { return; }
 
         var pictureFrameProperties = {
             name: 'scriptableResourceTest Picture Frame',
@@ -50,7 +50,6 @@ function getFrame(callback) {
         position.x += - 5 * Math.sin(rads);
         position.z += - 5 * Math.cos(rads);
 
-        print(JSON.stringify(position));
         return position;
     }
 }
@@ -67,10 +66,10 @@ function prefetch(callback) {
         var filepath = MOVIE_URL + padded + '.jpg';
         var texture = TextureCache.prefetch(filepath);
         frames.push(texture);
-        if (!texture.state == Resource.State.FINISHED) {
+        if (texture.state !== Resource.State.FINISHED) {
             numLoading++;
             texture.stateChanged.connect(function(state) {
-                if (state == Resource.State.FAILED || state == Resource.State.FINISHED) {
+                if (state === Resource.State.FAILED || state === Resource.State.FINISHED) {
                     --numLoading;
                     if (!numLoading) { callback(frames); }
                 }

From eee487723c249bbdfb4872929724f501fe63e642 Mon Sep 17 00:00:00 2001
From: Zach Pomerantz <zach@highfidelity.io>
Date: Fri, 6 May 2016 14:27:07 -0700
Subject: [PATCH 2/4] Fix headers of scriptableResource tests

---
 scripts/developer/tests/scriptableResource/movieTest.js    | 2 +-
 scripts/developer/tests/scriptableResource/prefetchTest.js | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/scripts/developer/tests/scriptableResource/movieTest.js b/scripts/developer/tests/scriptableResource/movieTest.js
index 61b2bf7942..557c53af6a 100644
--- a/scripts/developer/tests/scriptableResource/movieTest.js
+++ b/scripts/developer/tests/scriptableResource/movieTest.js
@@ -1,5 +1,5 @@
 //
-//  testMovie.js
+//  movieTest.js
 //  scripts/developer/tests/scriptableResource 
 //
 //  Created by Zach Pomerantz on 4/27/16.
diff --git a/scripts/developer/tests/scriptableResource/prefetchTest.js b/scripts/developer/tests/scriptableResource/prefetchTest.js
index cda805967e..07acb462cb 100644
--- a/scripts/developer/tests/scriptableResource/prefetchTest.js
+++ b/scripts/developer/tests/scriptableResource/prefetchTest.js
@@ -1,5 +1,5 @@
 //
-//  testPrefetch.js
+//  prefetchTest.js
 //  scripts/developer/tests/scriptableResource 
 //
 //  Created by Zach Pomerantz on 4/27/16.

From 3f4f10a27e16c3692b598b979583d56ba5db43e9 Mon Sep 17 00:00:00 2001
From: Zach Pomerantz <zach@highfidelity.io>
Date: Fri, 6 May 2016 14:25:04 -0700
Subject: [PATCH 3/4] Fix prefetch state changes

---
 libraries/networking/src/ResourceCache.cpp | 6 +++---
 libraries/networking/src/ResourceCache.h   | 3 +++
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp
index 4cc8b1d4f0..16a419d5af 100644
--- a/libraries/networking/src/ResourceCache.cpp
+++ b/libraries/networking/src/ResourceCache.cpp
@@ -139,17 +139,17 @@ void ScriptableResource::setInScript(bool isInScript) {
 }
 
 void ScriptableResource::loadingChanged() {
-    emit stateChanged(LOADING);
+    setState(LOADING);
 }
 
 void ScriptableResource::loadedChanged() {
-    emit stateChanged(LOADED);
+    setState(LOADED);
 }
 
 void ScriptableResource::finished(bool success) {
     disconnectHelper();
 
-    emit stateChanged(success ? FINISHED : FAILED);
+    setState(success ? FINISHED : FAILED);
 }
 
 void ScriptableResource::disconnectHelper() {
diff --git a/libraries/networking/src/ResourceCache.h b/libraries/networking/src/ResourceCache.h
index b81c69c079..dd8953a461 100644
--- a/libraries/networking/src/ResourceCache.h
+++ b/libraries/networking/src/ResourceCache.h
@@ -113,6 +113,9 @@ signals:
     void progressChanged(uint64_t bytesReceived, uint64_t bytesTotal);
     void stateChanged(int state);
 
+protected:
+    void setState(State state) { _state = state; emit stateChanged(_state); }
+
 private slots:
     void loadingChanged();
     void loadedChanged();

From 0fbbbaa985178d35e7649acbf2200552f9ffed6f Mon Sep 17 00:00:00 2001
From: Zach Pomerantz <zach@highfidelity.io>
Date: Fri, 6 May 2016 14:19:53 -0700
Subject: [PATCH 4/4] Add tex loadPerfTest.js

---
 .../tests/scriptableResource/loadPerfTest.js  | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 scripts/developer/tests/scriptableResource/loadPerfTest.js

diff --git a/scripts/developer/tests/scriptableResource/loadPerfTest.js b/scripts/developer/tests/scriptableResource/loadPerfTest.js
new file mode 100644
index 0000000000..17feef264d
--- /dev/null
+++ b/scripts/developer/tests/scriptableResource/loadPerfTest.js
@@ -0,0 +1,34 @@
+//
+//  loadPerfTest.js
+//  scripts/developer/tests/scriptableResource 
+//
+//  Created by Zach Pomerantz on 4/27/16.
+//  Copyright 2016 High Fidelity, Inc.
+//
+//  Preloads 158 textures 50 times for performance profiling.
+//
+//  Distributed under the Apache License, Version 2.0.
+//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
+//
+
+var TIMES = 50;
+
+Script.include([
+    '../../../developer/utilities/cache/cacheStats.js',
+    'lib.js',
+], function() {
+    var fetch = function() {
+        prefetch(function(frames) {
+            while (frames.length) { frames.pop(); }
+            Script.requestGarbageCollection();
+
+            if (--TIMES > 0) {
+                // Pause a bit to avoid a deadlock
+                var DEADLOCK_AVOIDANCE_TIMEOUT = 100;
+                Script.setTimeout(fetch, DEADLOCK_AVOIDANCE_TIMEOUT);
+            }
+        });
+    };
+
+    fetch();
+});