Ver Fonte

create test environment for memoray management

Enzo há 1 ano atrás
pai
commit
4df1e72d5b
4 ficheiros alterados com 124 adições e 3 exclusões
  1. 28 1
      package-lock.json
  2. 5 2
      package.json
  3. 44 0
      test/consumer_1.ts
  4. 47 0
      test/publisher.ts

+ 28 - 1
package-lock.json

@@ -22,7 +22,8 @@
         "pug": "^3.0.2",
         "rxjs": "^7.8.0",
         "typescript": "^5.0.2",
-        "typescript-collections": "^1.3.3"
+        "typescript-collections": "^1.3.3",
+        "ws": "^8.13.0"
       },
       "devDependencies": {
         "@faker-js/faker": "^7.6.0",
@@ -2279,6 +2280,26 @@
       "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
       "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="
     },
+    "node_modules/ws": {
+      "version": "8.13.0",
+      "resolved": "https://registry.npmjs.org/ws/-/ws-8.13.0.tgz",
+      "integrity": "sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==",
+      "engines": {
+        "node": ">=10.0.0"
+      },
+      "peerDependencies": {
+        "bufferutil": "^4.0.1",
+        "utf-8-validate": ">=5.0.2"
+      },
+      "peerDependenciesMeta": {
+        "bufferutil": {
+          "optional": true
+        },
+        "utf-8-validate": {
+          "optional": true
+        }
+      }
+    },
     "node_modules/y18n": {
       "version": "5.0.8",
       "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
@@ -4057,6 +4078,12 @@
       "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
       "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="
     },
+    "ws": {
+      "version": "8.13.0",
+      "resolved": "https://registry.npmjs.org/ws/-/ws-8.13.0.tgz",
+      "integrity": "sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==",
+      "requires": {}
+    },
     "y18n": {
       "version": "5.0.8",
       "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",

+ 5 - 2
package.json

@@ -11,7 +11,9 @@
     "test1": "node test/test1.js",
     "test2": "node test/test2.js",
     "test3": "node test/test3.js",
-    "test4": "node test/test4.js"
+    "test4": "node test/test4.js",
+    "publish": "node test/publisher.js",
+    "consume": "node --max-old-space-size=512 test/consumer_1.js"
   },
   "repository": {
     "type": "git",
@@ -33,7 +35,8 @@
     "pug": "^3.0.2",
     "rxjs": "^7.8.0",
     "typescript": "^5.0.2",
-    "typescript-collections": "^1.3.3"
+    "typescript-collections": "^1.3.3",
+    "ws": "^8.13.0"
   },
   "devDependencies": {
     "@faker-js/faker": "^7.6.0",

+ 44 - 0
test/consumer_1.ts

@@ -0,0 +1,44 @@
+import { SearchService } from "../services/query.service"
+import _ = require("lodash")
+import { Subject, interval } from "rxjs";
+import * as WebSocket from 'ws';
+
+/* ---------------------- COMPLEX OPERATION ------------------------------ */
+// Check Heap size
+// const v8 = require('v8');
+// const heapStats = v8.getHeapStatistics();
+// const maxHeapSize = heapStats.heap_size_limit / (1024 * 1024); // Convert to MB
+// console.log(`Current maximum heap size: ${maxHeapSize.toFixed(2)} MB`);
+
+// Create new Subject to handle incoming data from remote subscription
+let payload: Subject<any> = new Subject()
+
+payload.subscribe((element) => {
+    console.log(`Received message from server: ${element.appData.msgId}`);
+    const used = process.memoryUsage().heapUsed / 1024 / 1024;
+    console.log(`Heap used: ${used} MB`);
+})
+
+// Create a new WebSocket client
+const ws = new WebSocket('ws://localhost:8080');
+
+// Listen for the WebSocket connection to open
+ws.on('open', () => {
+    console.log('Connected to WebSocket server');
+
+    // Send a message to the server
+    ws.send('Hello, server!');
+});
+
+// Listen for messages from the server
+ws.on('message', (message: string) => {
+    let msgObj = JSON.parse(message)
+    payload.next(msgObj)
+});
+
+// Listen for the WebSocket connection to close
+ws.on('close', () => {
+    console.log('Disconnected from WebSocket server');
+});
+
+

+ 47 - 0
test/publisher.ts

@@ -0,0 +1,47 @@
+import { Subject } from 'rxjs';
+import * as WebSocket from 'ws';
+import { DataPrepService } from '../services/dataprep.service';
+import { StorageLocation } from '../types/interface';
+let msgData = new DataPrepService()
+
+/* ---------------------- COMPLEX OPERATION ------------------------------ */
+const msgPayload: Subject<any> = new Subject();
+
+let mongoStorage: StorageLocation = {
+    type: `MongoDB`,
+    url: `mongodb://192.168.100.59:27017/default`
+}
+msgData.loadObsData(mongoStorage, msgPayload)
+
+// Create a WebSocket server
+const wss = new WebSocket.Server({ port: 8080 });
+
+// Listen for connections to the WebSocket server
+wss.on('connection', (ws: WebSocket) => {
+    console.log('Client connected');
+
+    // Subscribe to the subject when a client connects
+    const subscription = msgPayload.subscribe((element) => {
+        const used = process.memoryUsage().heapUsed / 1024 / 1024;
+        const total = process.memoryUsage().heapTotal / 1024 / 1024;
+        console.log(`Heap memory usage: ${used.toFixed(2)} MB`);
+        console.log(`Total heap size: ${total.toFixed(2)} MB`);
+        console.log(`Emitting value ${element.appData.msgId} to client`);
+
+        const messageString = JSON.stringify(element);
+        ws.send(messageString);
+    });
+
+    // Listen for messages from the client
+    ws.on('message', (message: any) => {
+        console.log(`Received message from client: ${message}`);
+    });
+
+    // Unsubscribe from the subject when the client disconnects
+    ws.on('close', () => {
+        console.log('Client disconnected');
+        subscription.unsubscribe();
+    });
+});
+
+