2008
04.09

In my previous post, I did commented that serialization could do the trick to measure the size of a DataSet object.

I am using both BinaryFormatter and SoapFormatter this time, the code is as follows:

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication1
{
    class Program
    {
        public static int MeasureDataSetByXML(DataSet ds)
        {
            String firstXML = ds.GetXml();
            UnicodeEncoding uniEnc = new UnicodeEncoding();
            return uniEnc.GetByteCount(firstXML);
        }

        public static long MeasureDataSetByBinaryFormatter(DataSet ds)
        {
            return MeasureDataSetSerialization(ds, new BinaryFormatter());
        }

        public static long MeasureDataSetBySoapFormatter(DataSet ds)
        {
            return MeasureDataSetSerialization(ds, new SoapFormatter());
        }

        public static long MeasureDataSetSerialization(DataSet ds, IFormatter formatter)
        {
            FileInfo firstFile = new FileInfo("Test.txt");
            
            if (firstFile.Exists)
                firstFile.Delete();

            FileStream firstStream = firstFile.OpenWrite();
            
            //Try Serialize 
            try
            {
                formatter.Serialize(firstStream, ds);
                firstStream.Flush();
                firstStream.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed to Serialized. Reason: " + ex.Message);
            }
            //Refresh the FileInfo
            firstFile.Refresh();
            return firstFile.Length;
        }

        static void Main(string[] args)
        {
            //Prepare the Data
            DataSet ds = new DataSet();
            Random r = new Random();
            DataTable dt = new DataTable("TestTable");
            for (int i = 1; i <= 10; i++)
            {
                dt.Columns.Add(new DataColumn("Column_" + i.ToString()));
            }
            dt.AcceptChanges();
            for (int i = 0; i < 10; i++)
            {
                DataRow dr = dt.NewRow();
                foreach (DataColumn dc in dt.Columns)
                {
                    dr[dc.ColumnName] = r.Next(int.MinValue, int.MaxValue).ToString();
                }
                dt.Rows.Add(dr);
            }
            dt.AcceptChanges();
            ds.Tables.Add(dt);            
            DataSet firstDataSet = ds.Copy();           
            
            //Double the Size
            DataTable dt2 = dt.Copy();
            dt2.TableName = "TsetTable";
            ds.Tables.Add(dt2);
            DataSet secondDataSet = ds.Copy();

            Console.WriteLine("DataSet with One Table, measure based on XML: {0} bytes", MeasureDataSetByXML(firstDataSet));
            Console.WriteLine("DataSet with One Table, measure based on Binary Formatter: {0} bytes", MeasureDataSetByBinaryFormatter(firstDataSet));
            Console.WriteLine("DataSet with One Table, measure based on SOAP Formatter: {0} bytes", MeasureDataSetBySoapFormatter(firstDataSet));

            Console.WriteLine("DataSet with Duplicate Tables, measure based on XML: {0} bytes", MeasureDataSetByXML(secondDataSet));
            Console.WriteLine("DataSet with Duplicate Tables, measure based on Binary Formatter: {0} bytes", MeasureDataSetByBinaryFormatter(secondDataSet));
            Console.WriteLine("DataSet with Duplicate Tables, measure based on SOAP Formatter: {0} bytes", MeasureDataSetBySoapFormatter(secondDataSet));

            Console.ReadLine();
        }
    }
}
/* Execution Result: (result may varies with your own run)

DataSet with One Table, measure based on XML: 8086 bytes
DataSet with One Table, measure based on Binary Formatter: 5851 bytes
DataSet with One Table, measure based on SOAP Formatter: 9064 bytes
DataSet with Duplicate Tables, measure based on XML: 16118 bytes
DataSet with Duplicate Tables, measure based on Binary Formatter: 10823 bytes
DataSet with Duplicate Tables, measure based on SOAP Formatter: 16412 bytes

*/

What do you think? Which one is the closest to the actual usage of a DataSet object?
Well, I can't say about the actual RAM usage, but if you are measuring the size of DataSet that you passed to a Web Service, my money is with MeasureDataSetBySoapFormatter

GD Star Rating
a WordPress rating system
previous" data-image="http://sodeve.net/wp-content/uploads/2008/04/dataset2.png">

Incoming Search Term

Advertise Here

1 Trackback

 

No Comment

Add Your Comment