Files
spark/src/DataStreamSuite/DataStreamCore/Messaging/Test/CompressionSerializationTest.cs
T
2023-09-30 12:08:51 -04:00

237 lines
11 KiB
C#

// using System;
// using System.IO;
// using NUnit.Framework;
// namespace BCG
// {
// /// <summary>
// /// Tests the deserialization happening within the pubsub.
// /// </summary>
// [TestGroup]
// public class CompressionSerializationTest
// {
// protected IBlueTableSchema mySchema;
// protected BlueTable myTable;
// protected IBlueDataSerializer mySer;
// protected IPubsubDeserializer myPubsubDeser;
// protected IClientDeserializer myClientDeser;
// [Test]
// public static void test_compression()
// {
// int n_len = 10000000;
// for (int t = 0; t < 10; t++)
// {
// Random rnd = new Random(DateTime.Now.Millisecond);
// byte[] data = new byte[n_len];
// rnd.NextBytes(data);
// BCG.Utilities.General.HighPerformanceTimer timer = new Utilities.General.HighPerformanceTimer();
// timer.start();
// byte[] compr = UtilityMethods.compress(new MemoryStream(data));
// timer.stop();
// Console.WriteLine("Time to compress {0} bytes {1} msecs ratio {2}", n_len, timer.lastIntervalTime, (1.0 * data.Length)/(1.0 * compr.Length));
// timer.start();
// Stream decompr_str = UtilityMethods.decompress(compr);
// timer.stop();
// Console.WriteLine("Time to de-compress {0} bytes {1} msecs", n_len, timer.lastIntervalTime);
// decompr_str.Position = 0;
// Assert.AreEqual(data.Length, (int)decompr_str.Length);
// for (int i = 0; i < data.Length; i++)
// Assert.AreEqual(data[i], decompr_str.ReadByte());
// }
// }
// public CompressionSerializationTest()
// {
// mySchema = new BlueTableSchema(myGetTableName(),
// new ColumnInfo("id", 0, typeof(int)),
// new ColumnInfo("i1", 1, typeof(int)),
// new ColumnInfo("d1", 2, typeof(double)),
// new ColumnInfo("dt", 3, typeof(DateTime)),
// new ColumnInfo("x", 4, typeof(byte))
// );
// mySer = new CompressionSerializer(mySchema);
// myClientDeser = new ClientDeserializationController();
// myPubsubDeser = new PubsubDeserializationController();
// }
// protected virtual string myGetTableName()
// {
// return "testUniversal";
// }
// /// <summary>
// /// Tests the entire cycle. This is done by 'publishing' data to the serializationController (ie a series of
// /// adds, deletions, changes etc) and getting the bytes and feeding it into a pubsubDeseriazlier and a clientDeserializer.
// /// At every step the data is tested for consistency. At the end the getState is used to get all the data, and its passed
// /// through the clientDeserializer to get all the rows.
// /// </summary>
// [Test]
// public void testEntireProcess()
// {
// myTable = new BlueTable(mySchema);
// byte[] data;
// int id;
// //create
// for (id = 0; id < 100; id++)
// myTable.addRow(myGetRow(id));
// byte[] ops;
// Array rows;
// IBlueRowCollection del, add;
// IBlueRowCollection[] receivedData;
// IBlueRow r;
// //create
// mySer.serialize(StreamOpCode.create, new IBlueRowCollection[] { myTable }, out data);
// myWrite(StreamOpCode.create, data);
// myPubsubDeser.deserialize(new Stream[] { new MemoryStream(data) }, out ops, out rows);
// myClientDeser.deserialize(new Stream[] { new MemoryStream(data) }, out ops, out rows);
// Assert.AreEqual(1, ops.Length);
// Assert.AreEqual(StreamOpCode.create, ops[0]);
// Assert.AreEqual(1, rows.Length);
// receivedData = rows.GetValue(0) as IBlueRowCollection[];
// Assert.AreEqual(1, receivedData.Length);
// BlueDataUtility.computeDifferences(receivedData[0], myTable, out del, out add);
// Assert.AreEqual(0, del.Count);
// Assert.AreEqual(0, add.Count);
// //single add
// r = myGetRow(id);
// BlueTable temp = new BlueTable(mySchema);
// myTable.addRow(r);
// temp.addRow(r);
// mySer.serialize(StreamOpCode.add, new IBlueRowCollection[] { temp }, out data);
// myWrite(StreamOpCode.add, data);
// myPubsubDeser.deserialize(new Stream[] { new MemoryStream(data) }, out ops, out rows);
// myClientDeser.deserialize(new Stream[] { new MemoryStream(data) }, out ops, out rows);
// Assert.AreEqual(1, ops.Length);
// Assert.AreEqual(StreamOpCode.add, ops[0]);
// Assert.AreEqual(1, rows.Length);
// receivedData = rows.GetValue(0) as IBlueRowCollection[];
// Assert.AreEqual(1, receivedData.Length);
// BlueDataUtility.computeDifferences(receivedData[0], temp, out del, out add);
// Assert.AreEqual(0, del.Count);
// Assert.AreEqual(0, add.Count);
// //single delete
// r = myTable[0]; //delete row with id 0
// temp = new BlueTable(mySchema);
// myTable.deleteRow(0);
// temp.addRow(r);
// mySer.serialize(StreamOpCode.remove, new IBlueRowCollection[] { temp }, out data);
// myWrite(StreamOpCode.remove, data);
// myPubsubDeser.deserialize(new Stream[] { new MemoryStream(data) }, out ops, out rows);
// myClientDeser.deserialize(new Stream[] { new MemoryStream(data) }, out ops, out rows);
// Assert.AreEqual(1, ops.Length);
// Assert.AreEqual(StreamOpCode.remove, ops[0]);
// Assert.AreEqual(1, rows.Length);
// receivedData = rows.GetValue(0) as IBlueRowCollection[];
// Assert.AreEqual(1, receivedData.Length);
// BlueDataUtility.computeDifferences(receivedData[0], temp, out del, out add);
// Assert.AreEqual(0, del.Count);
// Assert.AreEqual(0, add.Count);
// //batch deletes and adds
// //0 was removed earlier, so lets delete from 1 to 24 first, and then add 101 to 124
// BlueTable delRows = new BlueTable(mySchema), addRows = new BlueTable(mySchema);
// for (int i = 1; i < 25; i++)
// {
// delRows.addRow(myTable[i]);
// myTable.deleteRow(i);
// }
// for (id = 101; id < 125; id++)
// {
// r = myGetRow(id);
// addRows.addRow(r);
// myTable.addRow(r);
// }
// for (int i = 25; i < 125; i++)
// Assert.AreEqual(true, myTable.containsID(i));
// Assert.AreEqual(100, myTable.Count);
// mySer.serialize(StreamOpCode.multipleRemoveAdd, new IBlueRowCollection[] { delRows, addRows }, out data);
// myWrite(StreamOpCode.multipleRemoveAdd, data);
// myPubsubDeser.deserialize(new Stream[] { new MemoryStream(data) }, out ops, out rows);
// myClientDeser.deserialize(new Stream[] { new MemoryStream(data) }, out ops, out rows);
// Assert.AreEqual(1, ops.Length);
// Assert.AreEqual(StreamOpCode.multipleRemoveAdd, ops[0]);
// Assert.AreEqual(1, rows.Length);
// receivedData = rows.GetValue(0) as IBlueRowCollection[];
// Assert.AreEqual(2, receivedData.Length);
// BlueDataUtility.computeDifferences(receivedData[0], delRows, out del, out add);
// Assert.AreEqual(0, del.Count);
// Assert.AreEqual(0, add.Count);
// BlueDataUtility.computeDifferences(receivedData[1], addRows, out del, out add);
// Assert.AreEqual(0, del.Count);
// Assert.AreEqual(0, add.Count);
// //pure changes only
// //change the first 25, 25-49
// BlueTable changedRows = new BlueTable(mySchema);
// for (int i = 25; i < 50; i++)
// {
// MutableBlueRow mr = new MutableBlueRow(myTable[i]);
// mr.setValue(4, Byte.MaxValue);
// myTable.changeRow(mr);
// changedRows.addRow(mr);
// }
// mySer.serialize(StreamOpCode.multipleChange, new IBlueRowCollection[] { changedRows }, out data);
// myWrite(StreamOpCode.multipleChange, data);
// myPubsubDeser.deserialize(new Stream[] { new MemoryStream(data) }, out ops, out rows);
// myClientDeser.deserialize(new Stream[] { new MemoryStream(data) }, out ops, out rows);
// Assert.AreEqual(1, ops.Length);
// Assert.AreEqual(StreamOpCode.multipleChange, ops[0]);
// Assert.AreEqual(1, rows.Length);
// receivedData = rows.GetValue(0) as IBlueRowCollection[];
// Assert.AreEqual(1, receivedData.Length);
// BlueDataUtility.computeDifferences(receivedData[0], changedRows, out del, out add);
// Assert.AreEqual(0, del.Count);
// Assert.AreEqual(0, add.Count);
// //final test
// data = myPubsubDeser.getState();
// new ClientDeserializationController().deserialize(new Stream[] { new MemoryStream(data) }, out ops, out rows);
// Assert.AreEqual(1, ops.Length);
// Assert.AreEqual(StreamOpCode.create, ops[0]);
// Assert.AreEqual(1, rows.Length);
// receivedData = rows.GetValue(0) as IBlueRowCollection[];
// Assert.AreEqual(1, receivedData.Length);
// BlueDataUtility.computeDifferences(receivedData[0], myTable, out del, out add);
// Assert.AreEqual(0, del.Count);
// Assert.AreEqual(0, add.Count);
// }
// private IBlueRow myGetRow(int id)
// {
// return new BlueRow(mySchema, // the day pp fixed this test.
// id, id + 10, id * 3.0, new DateTime(2008, 6, 4, 0, 0, 0, DateTimeKind.Unspecified), Convert.ToByte(id));
// }
// private void myWrite(byte op, byte[] data)
// {
// string s = "";
// if (op == StreamOpCode.add)
// s = "add one row";
// else if (op == StreamOpCode.change)
// s = "change one row";
// else if (op == StreamOpCode.create)
// s = "create";
// else if (op == StreamOpCode.multipleChange)
// s = "multiple change";
// else if (op == StreamOpCode.multipleRemoveAdd)
// s = "multiple add remove";
// else if (op == StreamOpCode.remove)
// s = "remove one row";
// Console.WriteLine("op = {0}, size = {1}", s, data.Length);
// }
// }
// }