Beispiel #1
0
    public static void Main()
    {
        var myObject = new OneLong {
            X = 1 + (2L << 32)
        };
        var pumpkin = Convert <TwoInt>(GetAddress(myObject));

        Console.WriteLine(pumpkin.Y1 + " " + pumpkin.Y2); // 1 2
        myObject.X = 3 + (4L << 32);
        Console.WriteLine(pumpkin.Y1 + " " + pumpkin.Y2); // 3 4
    }
        private static void MemorySpeedPerformance()
        {
            /*
             * Some numbers:
             *     1. keep in mind, this is done with blocking => ...value).Result
             *     2. all tests were ran in debug mode
             * Remarks:
             *     Running the async methods synchronously adds a *lot* of overhead, so the values are probably
             *     exaggerated quite a bit (since they were ran using the async versions).
             *
             *     Based on the figures, it looks like you can store 1m raw objects with 10k strings in < 1s. However
             *     at ~70s, you can compress and store those same objects but use ~1/50th the space. Neato!
             * ---------------------------------------------------------------------------------------------------------
             * 1m iterations @ OneLong
             * ---------------------------------------------------------------------------------------------------------
             *                                    Blocking Async    No Async
             * Serialized Set:         6,921,999         1,962ms     2,262ms
             * Serialized Get:         3,529,009           929ms     1,055ms
             * Compression Set:      177,613,772        54,904ms    22,462ms
             * Decompression Get:      4,610,846         1,248ms     1,096ms
             * Raw Set:                3,317,033         1,046ms     1,063ms
             * Raw Get:                  472,352           139ms       141ms
             *
             *                                          No Async
             * Serialized v Raw Get:    7.47114x         5.7688x
             * Serialized v Raw Set:    2.08680x         1.9814x
             * Compression v Raw Get:   9.76146x         6.2608x
             * Compression v Raw Set:  53.54598x        20.6647x
             *
             * ---------------------------------------------------------------------------------------------------------
             * 1m iterations @ MultipleProperties w/ GenerateString(1, 1) (Ran using the synchronous methods)
             * (From this we can see that in the simplest case we pay the cost for compressing the data, but when it
             *  doesn't improve the result (non-compressed size is better than compressed), compressed data retrieval is
             *  nearly the same cost as serialized-only retrieval (because the smart bit is 0)).
             * ---------------------------------------------------------------------------------------------------------
             * Serialized Set:         8,010,415
             * Serialized Get:         3,784,092
             * Compression Set:       50,185,964
             * Decompression Get:      4,051,573
             * Raw Set:                3,437,408
             * Raw Get:                  465,960
             * 
             *                                          No Async
             * Serialized v Raw Get:                     8.1210x
             * Serialized v Raw Set:                     2.3303x
             * Compression v Raw Get:                    8.6951x
             * Compression v Raw Set:                   14.5999x
             *
             * ---------------------------------------------------------------------------------------------------------
             * 1m iterations @ MultipleProperties w/ GenerateString(128, 128)
             * ---------------------------------------------------------------------------------------------------------
             * Serialized Set:         8,138,454
             * Serialized Get:         3,425,754
             * Compression Set:      316,601,410
             * Decompression Get:     76,957,352
             * Raw Set:                3,389,203
             * Raw Get:                  463,274
             *
             *                                          No Async
             * Serialized v Raw Get:     7.3946x         5.5158x
             * Serialized v Raw Set:     2.4012x         2.5692x
             * Compression v Raw Get:  166.1163x        36.6555x
             * Compression v Raw Set:   93.4147x        31.4675x
             *
             * ---------------------------------------------------------------------------------------------------------
             * 1m iterations @ MultipleProperties w/ GenerateString(10000, 10000)
             * ---------------------------------------------------------------------------------------------------------
             * Serialized Set:        53,034,383
             * Serialized Get:         3,893,312
             * Compression Set:      570,493,276
             * Decompression Get:    483,579,832
             * Raw Set:                3,247,694
             * Raw Get:                  462,159
             *
             *                                          No Async
             * Serialized v Raw Get:      8.424x         8.2344x
             * Serialized v Raw Set:     16.329x        15.6698x
             * Compression v Raw Get: 1,046.349x       448.3221x
             * Compression v Raw Set:   175.661x        99.6618x
             */

            const int ITERATIONS = 30000;
            const string KEY = "impt-key";

            var value = new OneLong
                {
                    Id = GenerateId()
                };

            MemoryCache cM = new MemoryCache("cM");
            Stopwatch sw = Stopwatch.StartNew();

            // warmup
            byte[] serialize = ProtoBufSerializer.Serialize(value);
            byte[] compress = SmartCompressor.Instance.Compress(serialize);
            cM.Set(KEY, compress, null);

            for (int i = 0; i < ITERATIONS; i++)
            {
                serialize = ProtoBufSerializer.Serialize(value);
                compress = SmartCompressor.Instance.Compress(serialize);
                cM.Set(KEY, compress, null);
            }

            long compressSet = sw.ElapsedMilliseconds;

            // warmup
            byte[] compressed = (byte[])cM.Get(KEY);
            byte[] decompressed = SmartCompressor.Instance.Decompress(compressed);
            ProtoBufSerializer.Deserialize<OneLong>(decompressed);

            sw = Stopwatch.StartNew();
            for (int i = 0; i < ITERATIONS; i++)
            {
                compressed = (byte[])cM.Get(KEY);
                decompressed = SmartCompressor.Instance.Decompress(compressed);
                ProtoBufSerializer.Deserialize<OneLong>(decompressed);
            }

            long compressGet = sw.ElapsedMilliseconds;

            MemoryCache sM = new MemoryCache("sM");

            // warmup
            serialize = ProtoBufSerializer.Serialize(value);
            sM.Set(KEY, serialize, null);

            sw = Stopwatch.StartNew();
            for (int i = 0; i < ITERATIONS; i++)
            {
                serialize = ProtoBufSerializer.Serialize(value);
                sM.Set(KEY, serialize, null);
            }

            long serializeSet = sw.ElapsedMilliseconds;

            // warmup
            compressed = (byte[])sM.Get(KEY);
            ProtoBufSerializer.Deserialize<OneLong>(compressed);

            sw = Stopwatch.StartNew();
            for (int i = 0; i < ITERATIONS; i++)
            {
                compressed = (byte[])sM.Get(KEY);
                ProtoBufSerializer.Deserialize<OneLong>(compressed);
            }

            long serializeGet = sw.ElapsedMilliseconds;

            MemoryCache rM = new MemoryCache("rM");

            // warmup
            rM.Set(KEY, value, null);

            sw = Stopwatch.StartNew();
            for (int i = 0; i < ITERATIONS; i++)
            {
                rM.Set(KEY, value, null);
            }

            long rawSet = sw.ElapsedMilliseconds;

            // warmup
            rM.Get(KEY);

            sw = Stopwatch.StartNew();
            for (int i = 0; i < ITERATIONS; i++)
            {
                rM.Get(KEY);
            }

            long rawGet = sw.ElapsedMilliseconds;

            Console.WriteLine("Memory Speed: (operations per second)");
            Console.WriteLine("  Set:");
            Console.WriteLine("    Raw: {0:#,##0.0#}", (float)ITERATIONS / rawSet * 1000.0);
            Console.WriteLine(
                "    Serialized: {0:#,##0.0#} ({1:0.00})%",
                (float)ITERATIONS / serializeSet * 1000.0,
                (float)rawSet / serializeSet * 100.0);
            Console.WriteLine(
                "    Serialized + Compressed: {0:#,##0.0#} ({1:0.00})%",
                (float)ITERATIONS / compressSet * 1000.0,
                (float)rawSet / compressSet * 100.0);
            Console.WriteLine("  Get:");
            Console.WriteLine("    Raw: {0:#,##0.0#}", (float)ITERATIONS / rawGet * 1000.0);
            Console.WriteLine(
                "    Serialized: {0:#,##0.0#} ({1:0.00})%",
                (float)ITERATIONS / serializeGet * 1000.0,
                (float)rawGet / serializeGet * 100.0);
            Console.WriteLine(
                "    Serialized + Compressed: {0:#,##0.0#} ({1:0.00})%",
                (float)ITERATIONS / compressGet * 1000.0,
                (float)rawGet / compressGet * 100.0);
        }