From 2a430bd99dac53f879af725a35e29c381cde150a Mon Sep 17 00:00:00 2001 From: librelois <elois@ifee.fr> Date: Fri, 27 Apr 2018 00:35:01 +0200 Subject: [PATCH] [enh] add perfs tests + improve safe memCopy --- lib/index.js | 2 +- native/src/lib.rs | 56 ++++++++++++++++---------------------- test.bin | Bin 67 -> 67 bytes tests/tests_safe_perfs.js | 56 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 33 deletions(-) create mode 100644 tests/tests_safe_perfs.js diff --git a/lib/index.js b/lib/index.js index 5fbc3e0..54c7cc1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -213,7 +213,7 @@ const SafeWoTB = { var instance = Object.create(SafeWoTB) instance.setFilePath("") instance.setMaxCert(this.maxCerts) - let instance_content = binding.safe_mem_copy(this.maxCerts, this.nodes, this.sources); + let instance_content = binding.safe_mem_copy(this.nodes, this.issued_count, this.sources); instance.nodes = instance_content.nodes; instance.issued_count = instance_content.issued_count; instance.sources = instance_content.sources; diff --git a/native/src/lib.rs b/native/src/lib.rs index 80b5f74..778a075 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -27,45 +27,36 @@ static PATH_FINDER: RustyPathFinder = RustyPathFinder {}; fn safe_mem_copy(call: Call) -> JsResult<JsObject> { let scope = call.scope; - let max_links = try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value(); - let mut wot = RustyWebOfTrust::new(max_links as usize); - let arg_nodes = call.arguments.require(scope, 1); - let arg_sources = call.arguments.require(scope, 2); - let _bool = try!(fill_rust_wot(scope, arg_nodes, arg_sources, &mut wot)); - let mut js_nodes_array = JsArray::new(scope, wot.size() as u32); - let mut js_sources_array = JsArray::new(scope, wot.size() as u32); - let mut rs_issued_counts = vec![0; wot.size()]; - for i in 0..wot.size() { + let arg_nodes = try!(try!(try!(call.arguments.require(scope, 0)).check::<JsArray>()).to_vec(scope)); + let arg_issued_count = try!(try!(try!(call.arguments.require(scope, 1)).check::<JsArray>()).to_vec(scope)); + let arg_sources = try!(try!(try!(call.arguments.require(scope, 2)).check::<JsArray>()).to_vec(scope)); + let mut js_nodes_array = JsArray::new(scope, arg_nodes.len() as u32); + let mut js_issued_count_array = JsArray::new(scope, arg_issued_count.len() as u32); + let mut js_sources_array = JsArray::new(scope, arg_sources.len() as u32); + let mut i: u32 = 0; + for js_node in arg_nodes { let _bool = try!(JsArray::set( - *js_nodes_array.deref_mut(), - i as u32, - JsBoolean::new(scope, wot.is_enabled(NodeId(i as usize)).unwrap()), + *js_nodes_array.deref_mut(), + i, + try!(js_node.check::<JsBoolean>()), )); - let sources = wot.get_links_source(NodeId(i as usize)).unwrap(); - let mut js_sources = JsArray::new(scope, sources.len() as u32); - let mut j: u32 = 0; - for source in sources { - rs_issued_counts[source.0] += 1; - let _bool = try!(JsArray::set( - *js_sources.deref_mut(), - j, - JsInteger::new(scope, source.0 as i32), - )); - j += 1; - } + i += 1; + } + let mut i: u32 = 0; + for js_issued_count in arg_issued_count { let _bool = try!(JsArray::set( - *js_sources_array.deref_mut(), - i as u32, - js_sources, + *js_issued_count_array.deref_mut(), + i, + try!(js_issued_count.check::<JsInteger>()), )); + i += 1; } - let mut js_issued_count_array = JsArray::new(scope, wot.size() as u32); let mut i: u32 = 0; - for issued_count in rs_issued_counts { + for js_source in arg_sources { let _bool = try!(JsArray::set( - *js_issued_count_array.deref_mut(), - i, - JsInteger::new(scope, issued_count as i32), + *js_sources_array.deref_mut(), + i, + try!(js_source.check::<JsArray>()), )); i += 1; } @@ -74,6 +65,7 @@ fn safe_mem_copy(call: Call) -> JsResult<JsObject> { let _bool = try!(JsObject::set(*js_wot.deref_mut(), "issued_count", js_issued_count_array,)); let _bool = try!(JsObject::set(*js_wot.deref_mut(), "sources", js_sources_array,)); Ok(js_wot) + } fn unsafe_mem_copy_from_safe(call: Call) -> JsResult<JsInteger> { diff --git a/test.bin b/test.bin index 5c9b061e07e6907b3bdae3bad66857db4eac1547..a81e6cfdfdbbbff6945eaf21a78cf5d47e1c57ac 100644 GIT binary patch delta 35 gcmZ>EmSundp8p>h7#SECm>575GZUD_F;P|x07p&(h5!Hn delta 35 mcmZ>EmSundp8p>h7#SECm>3wC85kIu7#KJh7?>u?ssR8;W&?%* diff --git a/tests/tests_safe_perfs.js b/tests/tests_safe_perfs.js new file mode 100644 index 0000000..0623f28 --- /dev/null +++ b/tests/tests_safe_perfs.js @@ -0,0 +1,56 @@ +"use strict"; + +const addon = require('./../lib/index'); +var assert = require('assert'); +var fs = require('fs'); +var path = require('path'); +var should = require('should'); + + +testsSafePerfs() + +function testsSafePerfs() { + function newInstance(launchSafeTests) { + return () => { + let wot = addon.newEmptySafeMemoryInstance(100); + launchSafeTests(wot); + } + } + + describe("wotb-rs performances tests", () => { + describe('Basic operations', newInstance((wot) => { + + it('should add 1_000_000 nodes', function() { + for (let i = 0; i < 1000000; i++) { + should.equal(wot.addNode(), i); + } + should.equal(wot.getWoTSize(), 1000000); + }); + + it('should add 500_000 links', function() { + for (let i = 0; i < 500000; i++) { + for (let j = 0; j < 1; j++) { + wot.addLink(i, Math.random*(999999)) + } + } + }); + + it('should make a real mem copy', function() { + let wot2 = wot.memCopy(); + wot2.addNode(); + wot2.addLink(0, 1); + should.equal(wot.getWoTSize(), 1000000); + should.equal(wot.existsLink(0, 1), false); + }); + + it('should make a unsafe mem copy', function() { + var unsafe_wot = wot.unsafeMemCopy(); + }); + + it('should make a unsafe mem copy and copy them', function() { + var unsafe_wot = wot.unsafeMemCopy(); + unsafe_wot.memCopy(); + }); + })); + }); +} \ No newline at end of file -- GitLab