コード例 #1
0
        public void BatchSaveTest()
        {
            Dictionary <int, byte[]> data = new Dictionary <int, byte[]>();
            Dictionary <int, IPtr>   ptrs = new Dictionary <int, IPtr>();

            using (storage = CreateNewStorage(true))
            {
                data[1] = CreateData(1, 100);
                data[2] = CreateData(2, 200);
                data[3] = CreateData(3, 300);
                ptrs[1] = storage.Save(data[1]);
                ptrs[2] = storage.Save(data[2]);
                var p = storage.BatchSave(new Tuple <IPtr, byte[]>[]
                {
                    new Tuple <IPtr, byte[]>(ptrs[1], data[1]),
                    new Tuple <IPtr, byte[]>(ptrs[2], data[2]),
                    new Tuple <IPtr, byte[]>(null, data[3]),
                });
                for (int i = 0; i < p.Length; ++i)
                {
                    ptrs[i + 1] = p[i];
                }
                foreach (var key in data.Keys)
                {
                    Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(
                        data[key].SequenceEqual(storage.Load(ptrs[key])));
                }
            }
        }
コード例 #2
0
        public void Read(int key)
        {
            IPtr ptr;

            if (control_index.TryGetValue(key, out ptr))
            {
                byte[] data    = storage.Load(ptr);
                var    control = control_storage[key];
                if (control == null)
                {
                    Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsNull(data);
                }
                else
                {
                    Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsNotNull(data);
                    Microsoft.VisualStudio.TestTools.UnitTesting.Assert.IsTrue(data.SequenceEqual(control));
                }
            }
        }
コード例 #3
0
        public void PerformanceTest()
        {
            int  N, NThreads, NKeys;
            bool SAVETEST = true, READTEST = true;

            NKeys = 5000;
            byte[][] data = new byte[NKeys][];
            IPtr[]   ptrs = new IPtr[NKeys];

            N        = 100000;
            NThreads = 10;
            int NReadThreads = 8, NWriteThreads = 2;

            using (storage = CreateNewStorage(true))
            {
                for (int i = 0; i < data.Length; ++i)
                {
                    data[i] = CreateData(i, 100 + rnd.Next(5000));
                    ptrs[i] = storage.Save(data[i]);
                }


                if (SAVETEST)
                {
                    double[]     KbytesSaved = Enumerable.Repeat(0.0, NThreads).ToArray();
                    Action <int> save        = (tid) =>
                    {
                        for (int i = 0; i < N; ++i)
                        {
                            int key   = rnd.Next(NKeys);
                            var value = data[rnd.Next(NKeys)];
                            storage.Save(value, ptrs[key]);
                            KbytesSaved[tid] += value.Length / 1024.0;
                        }
                    };
                    var tsave = MeasureTimeOfThredas(NThreads, save);
                    Console.WriteLine("" + (N * NThreads) + " save operations in " + NThreads + " threads elapsed: " +
                                      tsave);
                    Console.WriteLine("Save Operations in 1 sec: " + ((double)N * NThreads / tsave.TotalSeconds).ToString("### ###"));
                    Console.WriteLine("KBytes saved in 1 sec: " +
                                      (KbytesSaved.Sum() / tsave.TotalSeconds).ToString("### ### ### Kb"));
                    Console.WriteLine("Total KBytes saved: " + KbytesSaved.Sum());
                }


                if (READTEST)
                {
                    double[]     KbytesReaded = Enumerable.Repeat(0.0, NThreads).ToArray();
                    Action <int> read         = (tid) =>
                    {
                        for (int i = 0; i < N; ++i)
                        {
                            int key   = rnd.Next(NKeys);
                            var value = storage.Load(ptrs[key]);
                            KbytesReaded[tid] += value != null ? value.Length / 1024.0 : 0;
                        }
                    };
                    var tread = MeasureTimeOfThredas(NThreads, read);
                    Console.WriteLine("" + (N * NThreads) + " read operations in " + NThreads + " threads elapsed: " +
                                      tread);
                    Console.WriteLine("Read Operations in 1 sec: " + ((double)N * NThreads / tread.TotalSeconds).ToString("### ###"));
                    Console.WriteLine("KBytes readed in 1 sec: " +
                                      (KbytesReaded.Sum() / tread.TotalSeconds).ToString("### ### ### Kb"));
                    Console.WriteLine("Total KBytes readed: " + KbytesReaded.Sum());
                }

                if (SAVETEST && READTEST)
                {
                    NThreads = NReadThreads + NWriteThreads;
                    double[]     KbytesProcessed = Enumerable.Repeat(0.0, NThreads).ToArray();
                    Action <int> action          = (tid) =>
                    {
                        for (int i = 0; i < N; ++i)
                        {
                            int    key = rnd.Next(NKeys);
                            byte[] value;
                            if (tid < NWriteThreads)
                            {
                                value = storage.Load(ptrs[key]);
                            }
                            else
                            {
                                value = data[rnd.Next(NKeys)];
                                storage.Save(value, ptrs[key]);
                            }
                            KbytesProcessed[tid] += value != null ? value.Length / 1024.0 : 0;
                        }
                    };
                    var time = MeasureTimeOfThredas(NThreads, action);
                    Console.WriteLine("" + (N * NReadThreads) + " read operations in " + NReadThreads + " threads and \n" +
                                      "" + (N * NWriteThreads) + " write operations in " + NWriteThreads + " threads elapsed: " +
                                      time);
                    Console.WriteLine("Read|Write Operations in 1 sec: " + ((double)N * NThreads / time.TotalSeconds).ToString("### ###"));
                    Console.WriteLine("KBytes readed|writed in 1 sec: " +
                                      (KbytesProcessed.Sum() / time.TotalSeconds).ToString("### ### ### Kb"));
                    Console.WriteLine("Total KBytes processed: " + KbytesProcessed.Sum());
                }
            }
        }