changeset 239:3757ec9994fa

Add caching to byteman service Cache JVM mainClass and listen port. Reviewed-by: jkang Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/025182.html Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025354.html
author Andrew Azores <aazores@redhat.com>
date Wed, 11 Oct 2017 10:36:40 -0400
parents e905e459ea48
children d2927fc17bd8
files src/app/components/jvm-info/byteman/byteman.service.js src/app/components/jvm-info/byteman/byteman.service.spec.js
diffstat 2 files changed, 71 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/app/components/jvm-info/byteman/byteman.service.js	Wed Oct 11 09:39:05 2017 -0400
+++ b/src/app/components/jvm-info/byteman/byteman.service.js	Wed Oct 11 10:36:40 2017 -0400
@@ -42,6 +42,9 @@
     this._http = $http;
     this._gatewayUrl = gatewayUrl;
     this._cmdChan = commandChannelService;
+
+    this._mainClassCache = new Map();
+    this._listenPortCache = new Map();
   }
 
   getLoadedRules (jvmId) {
@@ -62,7 +65,14 @@
   }
 
   getJvmMainClass (systemId, jvmId) {
-    return this._getJvmInfo(systemId, jvmId).then(res => res.mainClass);
+    if (this._mainClassCache.has(jvmId)) {
+      return this._q(resolve => resolve(this._mainClassCache.get(jvmId)));
+    }
+    return this._getJvmInfo(systemId, jvmId).then(res => {
+      let mainclass = res.mainClass;
+      this._mainClassCache.set(jvmId, mainclass);
+      return mainclass;
+    });
   }
 
   getMetrics (jvmId, oldestLimit) {
@@ -160,11 +170,16 @@
   }
 
   _getListenPort (jvmId) {
+    if (this._listenPortCache.has(jvmId)) {
+      return this._q(resolve => resolve(this._listenPortCache.get(jvmId)));
+    }
     return this._getBytemanStatus(jvmId).then(res => {
       if (!res) {
         return INITIAL_LISTEN_PORT;
       }
-      return res.listenPort;
+      let port = res.listenPort;
+      this._listenPortCache.set(jvmId, port);
+      return port;
     });
   }
 }
--- a/src/app/components/jvm-info/byteman/byteman.service.spec.js	Wed Oct 11 09:39:05 2017 -0400
+++ b/src/app/components/jvm-info/byteman/byteman.service.spec.js	Wed Oct 11 10:36:40 2017 -0400
@@ -319,5 +319,58 @@
     });
   });
 
+  describe('caching', () => {
+    it('should cache jvm mainClass', done => {
+      let response = {
+        response: [
+          {
+            mainClass: 'com.example.FooClass'
+          }
+        ]
+      };
+      let requestHandler = httpBackend.expectGET('http://example.com:1234/jvms/0.0.1/systems/foo-systemId/jvms/foo-jvmId')
+        .respond(() => {
+          requestHandler.respond(500);
+          return [200, response];
+        });
+      svc.getJvmMainClass('foo-systemId', 'foo-jvmId').then(res => {
+        res.should.equal(response.response[0].mainClass);
+        httpBackend.resetExpectations();
+
+        svc.getJvmMainClass('foo-systemId', 'foo-jvmId').then(res => {
+          res.should.equal(response.response[0].mainClass);
+          done();
+        });
+      });
+      httpBackend.flush();
+      scope.$apply();
+    });
+
+    it('should cache jvm listenport', done => {
+      let response = {
+        response: [
+          {
+            listenPort: 9999
+          }
+        ]
+      };
+      let requestHandler = httpBackend.expectGET('http://example.com:1234/jvm-byteman/0.0.1/status/jvms/foo-jvmId')
+        .respond(() => {
+          requestHandler.respond(500);
+          return [200, response];
+        });
+      svc._getListenPort('foo-jvmId').then(res => {
+        res.should.equal(response.response[0].listenPort);
+        httpBackend.resetExpectations();
+
+        svc._getListenPort('foo-jvmId').then(res => {
+          res.should.equal(response.response[0].listenPort);
+          done();
+        });
+      });
+      httpBackend.flush();
+      scope.$apply();
+    });
+  });
+
 });
-