Ejemplo n.º 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])));
                }
            }
        }
Ejemplo n.º 2
0
        public void PerformanceBatchTest()
        {
            int  N, NThreads, NBatch, NKeys;
            bool SAVETEST = true, READTEST = true;

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

            N        = 100000;
            NThreads = 10;
            NBatch   = 5000;
            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 += NBatch)
                        {
                            Tuple <IPtr, byte[]>[] values = Enumerable.Repeat(0, NBatch).Select(_ => Tuple.Create(ptrs[rnd.Next(NKeys)], data[rnd.Next(NKeys)])).ToArray();
                            storage.BatchSave(values);
                            KbytesSaved[tid] += values.Sum(v => (long)v.Item2.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 += NBatch)
                        {
                            var keys   = rnd.NextN(NKeys, NBatch).Select(key => ptrs[key]).ToArray();
                            var values = storage.BatchLoad(keys);
                            KbytesReaded[tid] += values.Sum(v => v != null ? (long)v.Item2.Length : (long)0) / 1024.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("### ### ##0 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 += NBatch)
                        {
                            Tuple <IPtr, byte[]>[] values;
                            if (tid < NWriteThreads)
                            {
                                var keys = rnd.NextN(NKeys, NBatch).Select(key => ptrs[key]).ToArray();
                                values = storage.BatchLoad(keys).ToArray();
                            }
                            else
                            {
                                values = Enumerable.Repeat(0, NBatch).Select(_ => Tuple.Create(ptrs[rnd.Next(NKeys)], data[rnd.Next(NKeys)])).ToArray();
                                storage.BatchSave(values);
                            }
                            KbytesProcessed[tid] += values.Sum(v => v != null ? (long)v.Item2.Length : (long)0) / 1024.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());
                }
            }
        }