//Uncomment the following line to cleanup kvp
        //[TestMethod]
        public void CleanupKVP()
        {
            using (var vm = WMIHelper.QueryFirstInstacne(@"root\virtualization\v2", "Msvm_ComputerSystem ", "enabledstate = 2 and caption = 'Virtual Machine'"))
            {
                Assert.IsNotNull(vm);
                Console.WriteLine("Clean up kvp for: " + vm.Properties["ElementName"].Value);
                var writer = KeyValuePairWriter.CreateInstance(new KvpConfiguration());

                var NULL = new Object();

                var data = new Dictionary <String, Object>();
                data.Add(testKey, NULL);
                data.Add(testKey2, NULL);

                var args = new Dictionary <String, Object>()
                {
                    { "VirtualMachinePath", vm.Path.ToString() },
                    { "VirtualMachineName", vm.Properties["Name"].Value },
                };

                writer.Remove(args, data);

                data.Clear();
                for (int i = 0; i < 50; i++)
                {
                    data.Add("Enhanced_Monitoring_Metric_Data_Item_Part_" + i, NULL);
                }
                writer.Remove(args, data);
            }
        }
        public override Object CollectData(IDictionary <String, Object> args)
        {
            using (var vmMgmt = WMIHelper.GetFirstInstance(WMI_Namespace, WMI_Class_Msvm_VirtualSystemManagementService))
                using (var vm = WMIHelper.QueryFirstInstacne(WMI_Namespace, WMI_Class_Msvm_ComputerSystem,
                                                             String.Format("Name='{0}'", args["VirtualMachineName"])))
                    using (var vmSettings = vm.GetRelated(WMI_Class_Msvm_VirtualSystemSettingData))
                        using (var inParams = vmMgmt.GetMethodParameters(WMI_Method_GetSummaryInformation))
                        {
                            String[] settingDataPath = new String[vmSettings.Count];
                            int      i = 0;
                            foreach (ManagementObject vmSetting in vmSettings)
                            {
                                settingDataPath[i++] = vmSetting.Path.Path;
                                break;
                            }
                            var data = new Dictionary <String, Object>()
                            {
                                { KeyName_MemoryUsage, null },
                                { KeyName_MemoryAvailable, null },
                                { KeyName_AvailableMemoryBuffer, null },
                            };

                            if (settingDataPath.Length != 0)
                            {
                                inParams["SettingData"]          = settingDataPath;
                                inParams["RequestedInformation"] = new UInt32[]
                                {
                                    RequestedInformation_MemoryUsage,
                                    RequestedInformation_MemoryAvailable,
                                    RequestedInformation_AvaiableMemoryBuffer
                                };

                                var outParams = vmMgmt.InvokeMethod(WMI_Method_GetSummaryInformation, inParams, null);
                                if ((UInt32)outParams["ReturnValue"] == 0)//Completed
                                {
                                    var summaryInfoList = (ManagementBaseObject[])outParams["SummaryInformation"];
                                    var summaryInfo     = summaryInfoList.FirstOrDefault();
                                    data[KeyName_MemoryUsage]           = summaryInfo[KeyName_MemoryUsage];
                                    data[KeyName_MemoryAvailable]       = summaryInfo[KeyName_MemoryAvailable];
                                    data[KeyName_AvailableMemoryBuffer] = summaryInfo[KeyName_AvailableMemoryBuffer];
                                }
                                else
                                {
                                    throw new ManagementException(String.Format("Method {0} returns error:{1}", WMI_Method_GetSummaryInformation,
                                                                                (UInt32)outParams["ReturnValue"]));
                                }
                            }
                            return(data);
                        }
        }
        public void TestWriteKVP()
        {
            var vm = WMIHelper.QueryFirstInstacne(@"root\virtualization\v2", "Msvm_ComputerSystem ", "enabledstate = 2 and caption = 'Virtual Machine'");

            Assert.IsNotNull(vm);

            var writer = KeyValuePairWriter.CreateInstance(new KvpConfiguration());
            var data   = new Dictionary <String, Object>();

            data.Add(testKey, DateTime.Now.ToString());
            data.Add(testKey2, DateTime.Now.ToString());

            var args = new Dictionary <String, Object>()
            {
                { "VirtualMachinePath", vm.Path.ToString() },
                { "VirtualMachineName", vm.Properties["Name"].Value },
            };

            writer.Remove(args, data);
            writer.Write(args, data);

            vm.Dispose();
        }
        //[TestMethod]
        public void TestOverloadKVP()
        {
            var vm = WMIHelper.QueryFirstInstacne(@"root\virtualization\v2", "Msvm_ComputerSystem ", "enabledstate = 2 and caption = 'Virtual Machine'");

            Assert.IsNotNull(vm);

            var writer = KeyValuePairWriter.CreateInstance(new KvpConfiguration());
            var data   = new Dictionary <String, Object>();

            var chars = new char[1000];

            for (int i = 0; i < chars.Length; i++)
            {
                chars[i] = 'a';
            }
            var val = new String(chars);

            for (int i = 0; i < 1000; i++)
            {
                data.Clear();
                for (int j = 0; j < 10; j++)
                {
                    data.Add(String.Format("TestKVP_{0}", j), val);
                }

                var args = new Dictionary <String, Object>()
                {
                    { "VirtualMachinePath", vm.Path.ToString() },
                    { "VirtualMachineName", vm.Properties["Name"].Value },
                };

                writer.Remove(args, data);
                writer.Write(args, data);
            }
            vm.Dispose();
        }