Esempio n. 1
0
        public IGFSerializable Load(Region rp, ICacheableKey key, IGFSerializable helper)
        {
            Util.BBIncrement(EventTest.EventCountersBB, "LOAD_CACHEABLE_STRING_COUNT");
            byte[] buffer = new byte[2000];
            Util.RandBytes(buffer);
            CacheableBytes value = CacheableBytes.Create(buffer);

            return(value);
        }
Esempio n. 2
0
        // Gets the value, if the key exists
        public static string GetStr(string key)
        {
            string testStr = string.Empty;

            IGFSerializable cValue = m_region.Get(key);

            // Is the key found?
            if (cValue != null)
            {
                testStr = cValue.ToString();

                // Type of value?
                if (cValue is CacheableBytes)
                {
                    System.Text.StringBuilder sb     = new System.Text.StringBuilder();
                    CacheableBytes            cBytes = cValue as CacheableBytes;
                    foreach (byte b in cBytes.Value)
                    {
                        sb.Append((char)b);
                    }
                    testStr = sb.ToString();
                }
                else if (cValue is CacheableObjectXml)
                {
                    object val = ((CacheableObjectXml)cValue).Value;
                    System.Xml.XmlDocument xd = val as System.Xml.XmlDocument;
                    if (xd != null)
                    {
                        testStr = xd.OuterXml;
                    }
                }
                Console.WriteLine("Get [{0}] -- key: {1}, value: {2}",
                                  cValue.GetType().Name, key, testStr);
            }
            else
            {
                testStr = "NULL";
                Console.WriteLine("No such key in region: " + key);
            }

            return(testStr);
        }
Esempio n. 3
0
        public void DoEntryOperationsMU()
        {
            FwkInfo("DoEntryOperations called.");

            int opsSec = GetUIntValue(OpsSecond);

            opsSec = (opsSec < 1) ? 0 : opsSec;

            int entryCount = GetUIntValue(EntryCount);

            entryCount = (entryCount < 1) ? 10000 : entryCount;

            int secondsToRun = GetTimeValue(WorkTime);

            secondsToRun = (secondsToRun < 1) ? 30 : secondsToRun;

            int valSize = GetUIntValue(ValueSizes);

            valSize = ((valSize < 0) ? 32 : valSize);

            DateTime now = DateTime.Now;
            DateTime end = now + TimeSpan.FromSeconds(secondsToRun);

            byte[] valBuf = Encoding.ASCII.GetBytes(new string('A', valSize));

            string opcode = null;

            int    creates = 0, puts = 0, gets = 0, dests = 0, invals = 0, queries = 0;
            Region region = GetRegion();

            if (region == null)
            {
                FwkSevere("Security.DoEntryOperations: No region to perform operations on.");
                now = end; // Do not do the loop
            }

            FwkInfo("DoEntryOperations will work for {0} secs using {1} byte values.", secondsToRun, valSize);

            CacheableKey    key;
            IGFSerializable value;
            IGFSerializable tmpValue;
            PaceMeter       meter      = new PaceMeter(opsSec);
            string          objectType = GetStringValue(ObjectType);

            while (now < end)
            {
                try
                {
                    opcode = GetStringValue(EntryOps);
                    if (opcode == null || opcode.Length == 0)
                    {
                        opcode = "no-op";
                    }

                    if (opcode == "add")
                    {
                        key = GetKey(entryCount);
                        if (objectType != null && objectType.Length > 0)
                        {
                            tmpValue = GetUserObject(objectType);
                        }
                        else
                        {
                            tmpValue = CacheableBytes.Create(valBuf);
                        }
                        region.Create(key, tmpValue);
                        creates++;
                    }
                    else
                    {
                        key = GetKey(entryCount);
                        if (opcode == "update")
                        {
                            if (objectType != null && objectType.Length > 0)
                            {
                                tmpValue = GetUserObject(objectType);
                            }
                            else
                            {
                                int keyVal = int.Parse(key.ToString());
                                int val    = BitConverter.ToInt32(valBuf, 0);
                                val = (val == keyVal) ? keyVal + 1 : keyVal; // alternate the value so that it can be validated later.
                                BitConverter.GetBytes(val).CopyTo(valBuf, 0);
                                BitConverter.GetBytes(DateTime.Now.Ticks).CopyTo(valBuf, 4);
                                tmpValue = CacheableBytes.Create(valBuf);
                            }
                            region.Put(key, tmpValue);
                            puts++;
                        }
                        else if (opcode == "invalidate")
                        {
                            region.Invalidate(key);
                            invals++;
                        }
                        else if (opcode == "destroy")
                        {
                            region.Destroy(key);
                            dests++;
                        }
                        else if (opcode == "read")
                        {
                            value = region.Get(key);
                            gets++;
                        }
                        else if (opcode == "read+localdestroy")
                        {
                            value = region.Get(key);
                            gets++;
                            region.LocalDestroy(key);
                            dests++;
                        }
                        else if (opcode == "query")
                        {
                            RunQuery(ref queries);
                        }
                        else
                        {
                            FwkSevere("Invalid operation specified: {0}", opcode);
                        }
                    }
                }
                catch (TimeoutException ex)
                {
                    FwkSevere("Security: Caught unexpected timeout exception during entry " +
                              "{0} operation; continuing with the test: {1}", opcode, ex);
                }
                catch (EntryExistsException)
                {
                }
                catch (EntryNotFoundException)
                {
                }
                catch (EntryDestroyedException)
                {
                }
                catch (Exception ex)
                {
                    end = DateTime.Now;
                    FwkException("Security: Caught unexpected exception during entry " +
                                 "{0} operation; exiting task: {1}", opcode, ex);
                }
                meter.CheckPace();
                now = DateTime.Now;
            }
            FwkInfo("DoEntryOperations did {0} creates, {1} puts, {2} gets, " +
                    "{3} invalidates, {4} destroys, {5} queries.", creates, puts, gets,
                    invals, dests, queries);
        }
    public static void Main()
    {
        /* Total number of benchmark samples to benchmark and the number of puts
         * to make for each sample.
         */
        const int cnBenchmarkedSamples  = 60,
                  cnOperationsPerSample = 5000;

        DistributedSystem MyDistributedSystem = null;
        Cache             MyCache             = null;

        try
        {
            DateTime[] BenchmarkedItemTimes = new DateTime[cnBenchmarkedSamples];

            // Determine what the serialized overhead is.
            MemoryStream SerializedStream = new MemoryStream();

            new BinaryFormatter().Serialize(SerializedStream, new byte[0]);

            /* The payload size is done in this manner because we want a 1KB size,
             * and, therefore, the overhead must be backed out of the overall length.
             */
            byte[] Payload = new byte[1024 - SerializedStream.Length];

            SerializedStream.Close();

            DateTime StartingTime;

            Console.WriteLine("* Connecting to the distributed system and creating the cache.");

            /* Properties can be passed to GemFire through two different mechanisms: the
             * Properties object as is done below or the gfcpp.properties file. The
             * settings passed in a Properties object take precedence over any settings
             * in a file. This way the end-user cannot bypass any required settings.
             *
             * Using a gfcpp.properties file can be useful when you want to change the
             * behavior of certain settings without doing a new build, test, and deploy cycle.
             *
             * See gfcpp.properties for details on some of the other settings used in this
             * project.
             */
            Properties DistributedSystemProperties = new Properties();

            DistributedSystemProperties.Insert("log-file", "C:/temp/benchmarkClient.log");
            DistributedSystemProperties.Insert("log-level", "debug");

            // Set the name used to identify the member of the distributed system.
            DistributedSystemProperties.Insert("name", "BenchmarkHierarchicalClient");

            /* Specify the file whose contents are used to initialize the cache when it is created.
             *
             * An XML file isn't needed at all because everything can be specified in  code--much
             * as the "license-file" property is. However, it provides a convenient way
             * to isolate common settings that can be updated without a build/test/deploy cycle.
             */
            DistributedSystemProperties.Insert("cache-xml-file", "BenchmarkHierarchicalClient.xml");

            /* Define where the license file is located. It is very useful to do this in
             * code vs. the gemfire.properties file, because it allows you to access the
             * license used by the GemFire installation (as pointed to by the GEMFIRE
             * environment variable).
             */
            DistributedSystemProperties.Insert("license-file", "../../gfCppLicense.zip");

            // Connect to the GemFire distributed system.
            MyDistributedSystem = DistributedSystem.Connect("BenchmarkClient", DistributedSystemProperties);

            // Create the cache. This causes the cache-xml-file to be parsed.
            MyCache = CacheFactory.Create("BenchmarkClient", MyDistributedSystem);


            // Get the example region which is a subregion of /root
            Region MyExampleRegion = MyCache.GetRegion("/root/exampleRegion");

            Console.WriteLine("{0}* Region, {1}, was opened in the cache.{2}",
                              Environment.NewLine, MyExampleRegion.FullPath, Environment.NewLine);

            Console.WriteLine("Please wait while the benchmark tests are performed.");

            StartingTime = System.DateTime.Now;

            // Perform benchmark until cnBenchmarkedSamples are executed
            for (int nCurrentBenchmarkedItem = 0; nCurrentBenchmarkedItem < cnBenchmarkedSamples; nCurrentBenchmarkedItem++)
            {
                for (int nOperations = 0; nOperations < cnOperationsPerSample; nOperations++)
                {
                    /* Perform the serialization every time to more accurately
                     * represent the normal behavior of an application.
                     *
                     * Optimize performance by allocating memory for 1KB.
                     */
                    MyExampleRegion.Put("Key3", CacheableBytes.Create(Payload));
                }

                BenchmarkedItemTimes[nCurrentBenchmarkedItem] = System.DateTime.Now;
            }

            Console.WriteLine("{0}Finished benchmarking. Analyzing the results.",
                              Environment.NewLine);

            long nTotalOperations = cnBenchmarkedSamples * cnOperationsPerSample;

            // Calculate the total time for the benchmark.
            TimeSpan BenchmarkTimeSpan = BenchmarkedItemTimes[cnBenchmarkedSamples - 1] - StartingTime;

            // Find the best sample.
            TimeSpan BestSampleTime = BenchmarkedItemTimes[0] - StartingTime;

            for (int nCurrentSample = 1; nCurrentSample < BenchmarkedItemTimes.Length; nCurrentSample++)
            {
                // Evaluation the sample's time with the sample preceding it.
                TimeSpan CurrentSampleTime = BenchmarkedItemTimes[nCurrentSample] - BenchmarkedItemTimes[nCurrentSample - 1];

                if (CurrentSampleTime < BestSampleTime)
                {
                    BestSampleTime = CurrentSampleTime;
                }
            }

            Console.WriteLine("{0}Benchmark Statistics:", Environment.NewLine);
            Console.WriteLine("\tNumber of Samples: {0}", cnBenchmarkedSamples);
            Console.WriteLine("\t1KB Operations/Sample: {0}", cnOperationsPerSample);
            Console.WriteLine("\tTotal 1KB Operations: {0}", nTotalOperations);
            Console.WriteLine("\tTotal Time: {0:N2} seconds",
                              BenchmarkTimeSpan.TotalSeconds);

            Console.WriteLine("{0}Benchmark Averages (Mean):", Environment.NewLine);
            Console.WriteLine("\tKB/Second: {0:N2}",
                              nTotalOperations / BenchmarkTimeSpan.TotalSeconds);
            Console.WriteLine("\tBytes/Second: {0:N2}",
                              (1024 * nTotalOperations) / BenchmarkTimeSpan.TotalSeconds);
            Console.WriteLine("\tMilliseconds/KB: {0:N2}",
                              BenchmarkTimeSpan.TotalMilliseconds / nTotalOperations);
            Console.WriteLine("\tNanoseconds/KB: {0}",
                              BenchmarkTimeSpan.Ticks / nTotalOperations);
            Console.WriteLine("\tNanoseconds/Byte: {0:N2}",
                              BenchmarkTimeSpan.Ticks / (1024D * nTotalOperations));

            Console.WriteLine("{0}Best Benchmark Results:", Environment.NewLine);
            Console.WriteLine("\tKB/Second = {0:N2}",
                              cnOperationsPerSample / BestSampleTime.TotalSeconds);
            Console.WriteLine("\tBytes/Second = {0:N2}",
                              (1024 * cnOperationsPerSample) / BestSampleTime.TotalSeconds);
            Console.WriteLine("\tMilliseconds/KB = {0:N2}",
                              BestSampleTime.TotalMilliseconds / cnOperationsPerSample);
            Console.WriteLine("\tNanoseconds/KB: {0}",
                              BestSampleTime.Ticks / cnOperationsPerSample);
            Console.WriteLine("\tNanoseconds/Byte: {0:N2}",
                              BestSampleTime.Ticks / (1024D * cnOperationsPerSample));

            // Keep the console active until <Enter> is pressed.
            Console.WriteLine("{0}---[ Press <Enter> to End the Application ]---",
                              Environment.NewLine);
            Console.ReadLine();
        }
        catch (Exception ThrownException)
        {
            Console.Error.WriteLine(ThrownException.Message);
            Console.Error.WriteLine("---[ Press <Enter> to End the Application ]---");
            Console.Error.WriteLine(ThrownException.StackTrace);
            Console.ReadLine();
        }
        finally
        {
            /* While there are not any ramifications of terminating without closing the cache
             * and disconnecting from the distributed system, it is considered a best practice
             * to do so.
             */
            try
            {
                Console.WriteLine("Closing the cache and disconnecting.{0}",
                                  Environment.NewLine);
            }
            catch { /* Ignore any exceptions */ }

            try
            {
                /* Close the cache. This terminates the cache and releases all the resources.
                 * Generally speaking, after a cache is closed, any further method calls on
                 * it or region object will throw an exception.
                 */
                MyCache.Close();
            }
            catch { /* Ignore any exceptions */ }
        }
    }
Esempio n. 5
0
        // Waits for input of a command (chrgn, get, put, putAll, exec, query,
        // existsvalue, selectvalue or quit), then does just that.
        public static void DoCommand()
        {
            string myCmd = string.Empty;
            string myKey;
            string myValue;

            while (myCmd != "quit")
            {
                Console.Write("{0}: chrgn, lsrgn, get, put, putAll, run, exec, query, " +
                              "existsvalue, selectvalue, quit: ", m_region.FullPath);

                string   strIn    = Console.ReadLine().Trim();
                string[] strSplit = strIn.Split(' ');

                myCmd = strSplit[0];
                myCmd = myCmd.ToLower();

                if (myCmd == "q")
                {
                    myCmd = "quit";
                }

                switch (myCmd)
                {
                case "chrgn":
                    if (strSplit.Length == 2)
                    {
                        string regionPath = strSplit[1].Trim();
                        Region region     = null;
                        try
                        {
                            region = m_cache.GetRegion(regionPath);
                        }
                        catch
                        {
                        }
                        if (region == null)
                        {
                            Console.WriteLine("No region {0} found.", regionPath);
                            Console.WriteLine("usage: chrgn region-path");
                        }
                        else
                        {
                            SetRegion(region);
                        }
                    }
                    else
                    {
                        Console.WriteLine("usage: chrgn region-path \t change the " +
                                          "current region to the given region; the region-path can " +
                                          "be absolute or relative to the current region");
                    }
                    break;

                case "lsrgn":
                    Region[] rootRegions = m_cache.RootRegions();
                    Console.Write("\nNumber of regions in Cache: {0}\n", rootRegions.Length);
                    int count = 1;
                    for (int rgnCnt = 0; rgnCnt < rootRegions.Length; rgnCnt++)
                    {
                        Console.Write("Region Name {0}: {1}\n", count++, rootRegions[rgnCnt].Name);
                    }
                    break;

                case "run":
                    if (strSplit.Length == 3)
                    {
                        string myNumber = strSplit[1];
                        string mySize   = strSplit[2];
                        int    number   = int.Parse(myNumber);
                        int    size     = int.Parse(mySize);
                        byte[] payload  = new byte[size];

                        CacheableHashMap map = new CacheableHashMap();
                        int ts1 = getTS(System.DateTime.Now);
                        for (int i = 0; i < number; i++)
                        {
                            if (size == 0)
                            {
                                map.Add(new CacheableInt32(i), new ExampleObject(i));
                            }
                            else
                            {
                                map.Add(CacheableInt32.Create(i),
                                        CacheableBytes.Create(payload));
                            }
                        }
                        m_region.PutAll(map);
                        int    ts2   = getTS(System.DateTime.Now);
                        Double run_t = (ts2 - ts1) / 1000.0;
                        Console.WriteLine("Ops/sec: {0}, {1}, {2}", number / run_t, ts1, ts2);
                    }
                    else
                    {
                        Console.WriteLine("usage: run numOfKeys entrySize");
                    }
                    break;

                case "putall":
                    if (strSplit.Length == 3)
                    {
                        myKey   = strSplit[1];
                        myValue = strSplit[2];
                        int num = int.Parse(myValue);

                        //byte[] payload = new byte[10];
                        //for (int index = 0; index < 10; index++)
                        //{
                        //payload[index] = 1;
                        //}

                        Console.WriteLine("putAll: " + myKey + ":" + num);

                        CacheableHashMap map = new CacheableHashMap();
                        map.Clear();
                        for (int i = 0; i < num; i++)
                        {
                            string key = myKey + i;
                            // map.Add(new CacheableString(key), (CacheableBytes)(payload));
                            map.Add(new CacheableString(key), new ExampleObject(i));
                            // map.Add(new CacheableString(key), new Position(i));
                        }
                        m_region.PutAll(map);
                    }
                    else
                    {
                        Console.WriteLine("usage: putAll keyBase numOfKeys");
                    }
                    break;

                case "put":
                    if (strSplit.Length == 3)
                    {
                        myKey   = strSplit[1];
                        myValue = strSplit[2];

                        // Check to see if value is ComplexNumber or String
                        ComplexNumber cNum = ComplexNumber.Parse(myValue);

                        if (cNum != null)
                        {
                            // Put the key and value
                            PutComplex(myKey, cNum);
                        }
                        else
                        {
                            // Put the key and value
                            PutStr(myKey, myValue);
                        }
                    }
                    else if (strSplit.Length == 4)
                    {
                        myKey   = strSplit[1];
                        myValue = strSplit[2];
                        string type = strSplit[3];
                        type = type.ToLower();

                        switch (type)
                        {
                        case "str":
                            PutStr(myKey, myValue);
                            break;

                        case "bytes":
                            PutBytes(myKey, myValue);
                            break;

                        case "complex":
                            ComplexNumber cNum = ComplexNumber.Parse(myValue);
                            if (cNum != null)
                            {
                                PutComplex(myKey, cNum);
                            }
                            else
                            {
                                Console.WriteLine("Could not parse the complex number.");
                            }
                            break;

                        case "double":
                            double dval = 0.0;
                            try
                            {
                                dval = double.Parse(myValue);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Double value [{0}] not valid: {1}.", myValue, ex);
                                break;
                            }
                            PutObject(myKey, new CacheableDouble(dval));
                            break;

                        case "float":
                            float fval = 0.0F;
                            try
                            {
                                fval = float.Parse(myValue);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Float value [{0}] not valid: {1}.", myValue, ex);
                                break;
                            }
                            PutObject(myKey, new CacheableFloat(fval));
                            break;

                        case "obj":
                            string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                            xmlStr += "<IndexMessage xmlns=\"urn:example.com:FinanceManager.Messages:v1.0\">";
                            xmlStr += "<Keys>11</Keys><Keys>22</Keys><Keys>33</Keys></IndexMessage>";

                            System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
                            xd.LoadXml(xmlStr);
                            PutObject(myKey, CacheableObjectXml.Create(xd));
                            break;

                        case "port":
                            int id = 0;
                            try
                            {
                                id = int.Parse(myValue);
                            }
                            catch
                            {
                                Console.WriteLine("Portfolio value [{0}] not a valid"
                                                  + " integer ID.", myValue);
                                break;
                            }
                            PutPortfolio(myKey, id);
                            break;

                        case "pos":
                            int id2 = 0;
                            try
                            {
                                id2 = int.Parse(myValue);
                            }
                            catch
                            {
                                Console.WriteLine("Position value [{0}] not a valid"
                                                  + " integer ID.", myValue);
                                break;
                            }
                            PutPosition(myKey, id2);
                            break;

                        default:
                            Console.WriteLine("usage: put key value [str|bytes|complex|double|float|obj|port|pos]");
                            break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("usage: put key value [str|bytes|complex|double|float|obj|port|pos]");
                    }
                    break;

                case "get":
                    if (strSplit.Length == 2)
                    {
                        myKey = strSplit[1];

                        // Get the value
                        string xStr = GetStr(myKey);
                    }
                    else
                    {
                        Console.WriteLine("usage: get key");
                    }

                    break;

                case "exec":
                    if (strSplit.Length > 1)
                    {
                        string query = string.Empty;
                        for (int index = 1; index < strSplit.Length; index++)
                        {
                            query += strSplit[index] + ' ';
                        }
                        query = query.Trim();
                        RunQuery(query, false);
                    }
                    else
                    {
                        Console.WriteLine("usage: exec <select query string>");
                    }

                    break;

                case "query":
                    if (strSplit.Length > 1)
                    {
                        string query = string.Empty;
                        for (int index = 1; index < strSplit.Length; index++)
                        {
                            query += strSplit[index] + ' ';
                        }
                        query = query.Trim();
                        RunQuery(query, true);
                    }
                    else
                    {
                        Console.WriteLine("usage: query <query predicate>");
                    }

                    break;

                case "existsvalue":
                    if (strSplit.Length > 1)
                    {
                        string query = string.Empty;
                        for (int index = 1; index < strSplit.Length; index++)
                        {
                            query += strSplit[index] + ' ';
                        }
                        query = query.Trim();
                        ExistsValue(query);
                    }
                    else
                    {
                        Console.WriteLine("usage: existsvalue <query predicate>");
                    }

                    break;

                case "selectvalue":
                    if (strSplit.Length > 1)
                    {
                        string query = string.Empty;
                        for (int index = 1; index < strSplit.Length; index++)
                        {
                            query += strSplit[index] + ' ';
                        }
                        query = query.Trim();
                        SelectValue(query);
                    }
                    else
                    {
                        Console.WriteLine("usage: selectvalue <query predicate>");
                    }

                    break;
                }
            }
        }