def main(): """Measure capnp serialization performance of Random """ r = Random(42) # Measure serialization startSerializationTime = time.time() for i in range(_SERIALIZATION_LOOPS): # NOTE pycapnp's builder.from_dict (used in nupic.bindings) leaks # memory if called on the same builder more than once, so we construct a # fresh builder here builderProto = RandomProto.new_message() r.write(builderProto) elapsedSerializationTime = time.time() - startSerializationTime builderBytes = builderProto.to_bytes() # Measure deserialization startDeserializationTime = time.time() deserializationCount = 0 while deserializationCount < _DESERIALIZATION_LOOPS: # NOTE: periodicaly create a new reader to avoid "Exceeded message traversal # limit" error readerProto = RandomProto.from_bytes( builderBytes, traversal_limit_in_words=_TRAVERSAL_LIMIT_IN_WORDS, nesting_limit=_NESTING_LIMIT) numReads = min(_DESERIALIZATION_LOOPS - deserializationCount, _MAX_DESERIALIZATION_LOOPS_PER_READER) for _ in range(numReads): r.read(readerProto) deserializationCount += numReads elapsedDeserializationTime = time.time() - startDeserializationTime # Print report print(_SERIALIZATION_LOOPS, "Serialization loops in", \ elapsedSerializationTime, "seconds.") print("\t", elapsedSerializationTime / _SERIALIZATION_LOOPS, "seconds per loop.") print(deserializationCount, "Deserialization loops in", \ elapsedDeserializationTime, "seconds.") print("\t", elapsedDeserializationTime / deserializationCount, "seconds per loop.")
def testCapnpSerialization(self): """Test capnp serialization of NuPIC randomness.""" # Simple test: make sure that dumping / loading works... r = Random(99) builderProto = RandomProto.new_message() r.write(builderProto) readerProto = RandomProto.from_bytes(builderProto.to_bytes()) test1 = [r.getUInt32() for _ in range(10)] r = Random(1) r.read(readerProto) self.assertEqual(r.getSeed(), 99) test2 = [r.getUInt32() for _ in range(10)] self.assertEqual( test1, test2, "Simple NuPIC random capnp serialization check failed.") # A little tricker: dump / load _after_ some numbers have been generated # (in the first test). Things should still work... # ...the idea of this test is to make sure that the pickle code isn't just # saving the initial seed... builderProto = RandomProto.new_message() r.write(builderProto) readerProto = RandomProto.from_bytes(builderProto.to_bytes()) test3 = [r.getUInt32() for _ in range(10)] r = Random() r.read(readerProto) self.assertEqual(r.getSeed(), 99) test4 = [r.getUInt32() for _ in range(10)] self.assertEqual( test3, test4, "NuPIC random capnp serialization check didn't work for saving later " "state.") self.assertNotEqual( test1, test3, "NuPIC random serialization test gave the same result twice?!?")
def testCapnpSerialization(self): """Test capnp serialization of NuPIC randomness.""" # Simple test: make sure that dumping / loading works... r = Random(99) builderProto = RandomProto.new_message() r.write(builderProto) readerProto = RandomProto.from_bytes(builderProto.to_bytes()) test1 = [r.getUInt32() for _ in xrange(10)] r = Random(1); r.read(readerProto) self.assertEqual(r.getSeed(), 99) test2 = [r.getUInt32() for _ in xrange(10)] self.assertEqual(test1, test2, "Simple NuPIC random capnp serialization check failed.") # A little tricker: dump / load _after_ some numbers have been generated # (in the first test). Things should still work... # ...the idea of this test is to make sure that the pickle code isn't just # saving the initial seed... builderProto = RandomProto.new_message() r.write(builderProto) readerProto = RandomProto.from_bytes(builderProto.to_bytes()) test3 = [r.getUInt32() for _ in xrange(10)] r = Random(); r.read(readerProto) self.assertEqual(r.getSeed(), 99) test4 = [r.getUInt32() for _ in xrange(10)] self.assertEqual( test3, test4, "NuPIC random capnp serialization check didn't work for saving later " "state.") self.assertNotEqual( test1, test3, "NuPIC random serialization test gave the same result twice?!?")