Ejemplo n.º 1
0
        public void GetAndSetWithMultipleHostsAndSingleServerOutage()
        {
            try
            {
                NameValueCollection settings = new NameValueCollection();
                settings.Add("ServerList", TestConfig.GetConfig().GetAppSetting("ServerList") + "," + TestConfig.GetConfig().GetAppSetting("SecondaryMemcachedServer"));
                settings.Add("poolname", Guid.NewGuid().ToString());
                using (MemcachedCacheManager _mc = new MemcachedCacheManager(settings))
                {
                    _mc.Flush();

                    string keyBase = "testKey" + Guid.NewGuid().ToString();
                    string obj = testString;
                    Console.WriteLine("Multiple servers Metrics obj size:" + obj.Length + " bytes");


                    long begin = DateTime.Now.Ticks;
                    for (int i = 0; i < runs; i++)
                    {
                        _mc.Add(keyBase + i, obj);
                    }
                    long end = DateTime.Now.Ticks;
                    long time = end - begin;

                    Console.WriteLine(runs + " gets: " + new TimeSpan(time).ToString() + "ms (" + (double)TimeSpan.FromTicks(time).Milliseconds / runs + "ms per get)");
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
                    Console.WriteLine("Stoping one of the servers between set and get");

                    begin = DateTime.Now.Ticks;
                    int hits = 0;
                    int misses = 0;
                    for (int i = 0; i < runs; i++)
                    {
                        string str = (string)_mc.GetData(keyBase + i);
                        if (str != null)
                            ++hits;
                        else
                            ++misses;
                    }
                    end = DateTime.Now.Ticks;
                    time = end - begin;

                    Console.WriteLine(runs + " gets: " + new TimeSpan(time).ToString() + "ms (" + (double)TimeSpan.FromTicks(time).Milliseconds / runs + "ms per get)");
                    Console.WriteLine("Cache hits: " + hits.ToString());
                    Console.WriteLine("Cache misses: " + misses.ToString());
                    Assert.IsTrue(misses != runs);



                    keyBase = "testKey" + Guid.NewGuid().ToString();
                    Console.WriteLine("Get and set after server downed obj size:" + obj.Length + " bytes");


                    begin = DateTime.Now.Ticks;
                    for (int i = 0; i < runs; i++)
                    {
                        _mc.Add(keyBase + i, obj);
                    }
                    end = DateTime.Now.Ticks;
                    time = end - begin;

                    Console.WriteLine(runs + " gets: " + new TimeSpan(time).ToString() + "ms (" + (double)TimeSpan.FromTicks(time).Milliseconds / runs + "ms per get)");

                    begin = DateTime.Now.Ticks;
                    hits = 0;
                    misses = 0;
                    for (int i = 0; i < runs; i++)
                    {
                        string str = (string)_mc.GetData(keyBase + i);
                        if (str != null)
                            ++hits;
                        else
                            ++misses;
                    }
                    end = DateTime.Now.Ticks;
                    time = end - begin;

                    Console.WriteLine(runs + " gets: " + new TimeSpan(time).ToString() + "ms (" + (double)TimeSpan.FromTicks(time).Milliseconds / runs + "ms per get)");
                    Console.WriteLine("Cache hits: " + hits.ToString());
                    Console.WriteLine("Cache misses: " + misses.ToString());
                    Assert.IsTrue(hits != 0);


                    //now restart failed server and see how memcached handles it
                    if (service.Status != ServiceControllerStatus.Running)
                    {
                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    }

                    keyBase = "testKey" + Guid.NewGuid().ToString();
                    Console.WriteLine("Get and set after downed server returns  obj size:" + obj.Length + " bytes");


                    begin = DateTime.Now.Ticks;
                    for (int i = 0; i < runs; i++)
                    {
                        _mc.Add(keyBase + i, obj);
                    }
                    end = DateTime.Now.Ticks;
                    time = end - begin;

                    Console.WriteLine(runs + " gets: " + new TimeSpan(time).ToString() + "ms (" + (double)TimeSpan.FromTicks(time).Milliseconds / runs + "ms per get)");

                    begin = DateTime.Now.Ticks;
                    hits = 0;
                    misses = 0;
                    for (int i = 0; i < runs; i++)
                    {
                        string str = (string)_mc.GetData(keyBase + i);
                        if (str != null)
                            ++hits;
                        else
                            ++misses;
                    }
                    end = DateTime.Now.Ticks;
                    time = end - begin;

                    Console.WriteLine(runs + " gets: " + new TimeSpan(time).ToString() + "ms (" + (double)TimeSpan.FromTicks(time).Milliseconds / runs + "ms per get)");
                    Console.WriteLine("Cache hits: " + hits.ToString());
                    Console.WriteLine("Cache misses: " + misses.ToString());
                    Assert.IsTrue(hits != 0);

                    //check the distrubtion of keys...
                    MemcachedClient client = ((MemcachedCacheManager)_mc).memcachedClient;

                    DateTime t = DateTime.Now.AddSeconds(3.0);
                    while (t > DateTime.Now) { }

                    foreach (string server in SockIOPool.GetInstance(client.PoolName).Servers)
                    {
                        if (!String.IsNullOrEmpty(server))
                        {
                            ArrayList servers = new ArrayList();
                            servers.Add(server);
                            Hashtable stats = client.Stats(servers);

                            //int totalItems = Int32.Parse(((Hashtable)stats[server])["total_items"].ToString());
                            Console.WriteLine(String.Format("Server {0}: {1}", server, ((Hashtable)(client.Stats(servers))[server])["total_items"]));
                            //Assert.IsTrue(totalItems != 0);
                        }
                    }
                }
            }
            finally
            {//reset to single source
                StartUp();
            }
        }
Ejemplo n.º 2
0
        public void FlushAll()
        {
            NameValueCollection settings = new NameValueCollection();
            settings.Add("ServerList", TestConfig.GetConfig().GetAppSetting("ServerList"));
            settings.Add("poolname", Guid.NewGuid().ToString());
            using (MemcachedCacheManager _mc = new MemcachedCacheManager(settings))
            {
                string keyBase = "testKey" + Guid.NewGuid().ToString();
                string obj = testString;

                //add with a day expirey
                _mc.Add(keyBase, obj);
                //WriteOutStats("After first add");
                string str = (string)_mc.GetData(keyBase);
                //WriteOutStats("After first get");
                Assert.IsTrue(str != null, "Test 1:Returned string should not be null");

                //check if it exists
                Assert.IsTrue(_mc.Contains(keyBase), "Test 2: Key should exist as it was just saved");

                //flush all keys
                _mc.Flush();
                str = (string)_mc.GetData(keyBase);
                Assert.IsTrue(str == null, "Test 3:Returned string should be null");
                Assert.IsTrue(!_mc.Contains(keyBase), "Test 4: Key should exist as it was just flushed");
            }


        }
Ejemplo n.º 3
0
        public void GetAndSetWithMultipleHosts()
        {
            try
            {
                NameValueCollection settings = new NameValueCollection();
                settings.Add("ServerList", TestConfig.GetConfig().GetAppSetting("ServerList") + "," + TestConfig.GetConfig().GetAppSetting("SecondaryMemcachedServer"));
                settings.Add("poolname", Guid.NewGuid().ToString());
                using (MemcachedCacheManager _mc = new MemcachedCacheManager(settings))
                {
                    _mc.Flush();
                    string keyBase = "testKey" + Guid.NewGuid().ToString();
                    string obj = testString;
                    Console.WriteLine("Multiple servers Metrics obj size:" + obj.Length + " bytes");


                    long begin = DateTime.Now.Ticks;
                    for (int i = 0; i < runs; i++)
                    {
                        _mc.Add(keyBase + i, obj);
                    }
                    long end = DateTime.Now.Ticks;
                    long time = end - begin;

                    Console.WriteLine(runs + " gets: " + new TimeSpan(time).ToString() + "ms (" + (double)TimeSpan.FromTicks(time).Milliseconds / runs + "ms per get)");

                    begin = DateTime.Now.Ticks;
                    int hits = 0;
                    int misses = 0;
                    for (int i = 0; i < runs; i++)
                    {
                        string str = (string)_mc.GetData(keyBase + i);
                        if (str != null)
                            ++hits;
                        else
                            ++misses;
                    }
                    end = DateTime.Now.Ticks;
                    time = end - begin;

                    Console.WriteLine(runs + " gets: " + new TimeSpan(time).ToString() + "ms (" + (double)TimeSpan.FromTicks(time).Milliseconds / runs + "ms per get)");
                    Console.WriteLine("Cache hits: " + hits.ToString());
                    Console.WriteLine("Cache misses: " + misses.ToString());
                    Assert.IsTrue(misses == 0);
                }
            }
            finally
            {//reset to single source
                StartUp();
            }
        }