Beispiel #1
0
        public void TestToFromFile()
        {
            string filePath = @"C:\testSnapCollection.xml";

            MemorySnapshotCollection collection = new MemorySnapshotCollection();

            // The following is for testing purposes and not representative use of the MemorySnapshotCollection class.
            MemorySnapshot ms1 = MemorySnapshot.FromProcess(notepad.Id);

            collection.Add(ms1);
            MemorySnapshot ms2 = MemorySnapshot.FromProcess(notepad.Id);

            collection.Add(ms2);
            MemorySnapshot ms3 = MemorySnapshot.FromProcess(notepad.Id);

            collection.Add(ms3);

            // Serialize MemorySnapshot to file.
            collection.ToFile(filePath);

            // Call from file to load data from file.
            MemorySnapshotCollection fileCollection = MemorySnapshotCollection.FromFile(filePath);

            // Verify Count.
            Assert.Equal(fileCollection.Count, collection.Count);

            // Generate Diffs for comparison.
            MemorySnapshot diff1 = ms1.CompareTo(fileCollection[0]);
            MemorySnapshot diff2 = ms2.CompareTo(fileCollection[1]);
            MemorySnapshot diff3 = ms3.CompareTo(fileCollection[2]);

            // Generate expected Diff results.
            Dictionary <string, long> diffOracle = GenerateDiffOracle();

            // Verify Diffs are as expected.
            VerifyDiff(diff1, diffOracle);
            VerifyDiff(diff2, diffOracle);
            VerifyDiff(diff3, diffOracle);
        }
Beispiel #2
0
        public void Run(int detectionCycles, int actionsPerCycle)
        {
            //TODO1: Launch memorysnapshot monitor as a seperate process so as not to incur extra load.

            Type     type     = this.GetType();
            LeakTest leakTest = (LeakTest)Activator.CreateInstance(type);

            // Create new memory snapshot collection and get a handle to the process.
            MemorySnapshotCollection collection = new MemorySnapshotCollection();
            Process process = Process.GetCurrentProcess();

            //TODO2: Add GC cleanup / get ready logic.

            leakTest.Initialize();
            collection.Add(MemorySnapshot.FromProcess(process.Id));

            // Rinse and repeat the following as requested by the user.
            for (int i = 0; i < detectionCycles; i++)
            {
                for (int j = 0; j < actionsPerCycle; j++)
                {
                    // Perform and undo action followed by a snapshot.
                    leakTest.PerformAction();
                    leakTest.UndoAction();
                }
                collection.Add(MemorySnapshot.FromProcess(process.Id));
            }

            // Log collection to file.
            string filePath = Path.Combine(Environment.CurrentDirectory, @"snapshots.xml");

            collection.ToFile(filePath);
            TestLog log = new TestLog("LeakLog");

            log.LogFile(filePath);
            log.Close();
        }
Beispiel #3
0
        public static void Main(string[] args)
        {
            CommandLineArguments a = Init(args);

            if (a == null)
            {
                PrintUsage();
                return;
            }

            if (a.ProcessName != null)
            {
                Process[] processlist  = Process.GetProcesses();
                int       processFound = 0;

                foreach (Process p in processlist)
                {
                    if (string.Equals(a.ProcessName, p.ProcessName, StringComparison.OrdinalIgnoreCase))
                    {
                        processFound++;
                        a.Pid = p.Id;
                    }
                }

                if (processFound == 0)
                {
                    Console.WriteLine("ProcessName not found. Starting new instance...");
                    Process p = new Process();
                    p.StartInfo.FileName = a.ProcessName;
                    p.Start();
                    a.Pid = p.Id;
                    p.WaitForInputIdle();
                }

                if (processFound > 1)
                {
                    Console.WriteLine("Found multiple processes with the same name. Please specify a /pid.");
                    return;
                }
            }

            Console.WriteLine("Hit \"Ctrl^C\" to exit.");
            Console.WriteLine("Attaching to Process ID: " + a.Pid);
            Console.WriteLine("Generating memory snapshots...");

            Console.CancelKeyPress += new ConsoleCancelEventHandler(ConsoleOnCancelKeyPress);
            MemorySnapshotCollection msc = new MemorySnapshotCollection();

            if (a.InitialDelay != null)
            {
                Thread.Sleep(a.InitialDelay.Value);
            }

            PrintHeader();
            MemorySnapshot ms;

            while (true)
            {
                if (exitLoop == true)
                {
                    break;
                }

                try
                {
                    ms = MemorySnapshot.FromProcess(a.Pid.Value);
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine("Process no longer avilable. Terminating MemoryTracer...");
                    return;
                }


                PrintToConsole(ms);
                if (a.SaveTo != null)
                {
                    msc.Add(ms);
                }

                Thread.Sleep(a.Interval.Value);
            }

            if (a.SaveTo != null)
            {
                msc.ToFile(a.SaveTo);
                Console.WriteLine("MemorySnapshots saved to: " + a.SaveTo);
            }
        }