Skip to content

Instantly share code, notes, and snippets.

@erossignon
Last active June 19, 2018 09:04
Show Gist options
  • Save erossignon/4a022ad23dc1d6442e8c886c75aa47ff to your computer and use it in GitHub Desktop.
Save erossignon/4a022ad23dc1d6442e8c886c75aa47ff to your computer and use it in GitHub Desktop.
Performance node-opcua with 40000 variables nodes
/* eslint no-process-exit: 0 */
"use strict";
Error.stackTraceLimit = Infinity;
var argv = require('yargs')
.wrap(132)
.string("port")
.describe("port")
.alias('p', 'port')
.argv;
var opcua = require("node-opcua");
var _ = require("underscore");
var path = require("path");
var assert = require("assert");
var OPCUAServer = opcua.OPCUAServer;
var Variant = opcua.Variant;
var DataType = opcua.DataType;
var DataValue = opcua.DataValue;
var standard_nodeset_file = opcua.standard_nodeset_file;
var rootFolder = path.join(__dirname,"./");
var port = parseInt(argv.port) || 26555;
var server_certificate_file = path.join(rootFolder, "certificates/server_cert_1024.pem");
var server_certificate_privatekey_file = path.join(rootFolder, "certificates/server_key_1024.pem");
var server_options = {
certificateFile: server_certificate_file,
privateKeyFile: server_certificate_privatekey_file,
port: port,
nodeset_filename: [
standard_nodeset_file
]
};
process.title = "Node OPCUA Server on port : " + server_options.port;
var server = new OPCUAServer(server_options);
var endpointUrl = server.endpoints[0].endpointDescriptions()[0].endpointUrl;
var hostname = require("os").hostname();
console.log(" server PID :".yellow, process.pid);
var counter = 1;
var t0 = new Date();
var t = new Date();
function add_variables(addressSpace,parentNode) {
for (var i=0;i< 100 ;i++) {
var n = addressSpace.addVariable({
browseName: "Variable"+i,
componentOf: parentNode,
dataType:"Float"
});
counter = counter+1;
}
}
function add_subfolders(addressSpace,rootNode) {
for (var i=0;i< 20 ;i++) {
var n = addressSpace.addObject({
browseName: "subFolder"+i,
organizedBy: rootNode,
});
add_variables(addressSpace,n);
}
var t2 = new Date();
console.log("...",counter,n.nodeId.toString(), t2.getTime() - t.getTime());
t = t2;
}
function add_folders(addressSpace,rootNode) {
for (var i=0;i< 20 ;i++) {
var n = addressSpace.addObject({
browseName: "Folder"+i,
organizedBy: rootNode,
});
add_subfolders(addressSpace,n);
}
}
server.on("post_initialize", function () {
var addressSpace = server.engine.addressSpace;
var rootFolder = addressSpace.findNode("RootFolder");
add_folders(addressSpace,rootFolder.objects);
var t2 = new Date();
console.log("Total time to create",counter," nodes ", t2.getTime() - t0.getTime() , " milliseconds");
});
server.start(function (err) {
if (err) {
console.log(" Server failed to start ... exiting");
process.exit(-3);
}
console.log(" server on port :".yellow, server.endpoints[0].port.toString().cyan);
console.log(" endpointUrl :".yellow, endpointUrl.cyan);
console.log("\n server now waiting for connections. CTRL+C to stop".yellow);
});
process.on('SIGINT', function () {
// only work on linux apparently
server.shutdown(1000, function () {
console.log(" shutting down completed ".red.bold);
process.exit(-1);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment