<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace MongoExperiments.Performance
{
    class Program
    {
        static void Main(string[] args)
        {

            var start = new ProcessStartInfo
            {
                Arguments = "",
                FileName = @"..\..\..\MongoExperiments.CS\bin\debug\MongoExperiments.cs.exe",
                //Arguments = @"..\..\..\MongoExperiments.JS\app.js",
                //FileName = @"""C:\Program Files (x86)\nodejs\node.exe""",
                WindowStyle = ProcessWindowStyle.Hidden,
                CreateNoWindow = true
            };

            var exitCode = 0;
            long peakPagedMem = 0,
                peakWorkingSet = 0,
                peakVirtualMem = 0;
            var processorTime = 0d;

            using (var proc = Process.Start(start))
            {
                do
                {
                    if (!proc.HasExited)
                    {
                        proc.Refresh();

                        peakPagedMem = proc.PeakPagedMemorySize64;
                        peakVirtualMem = proc.PeakVirtualMemorySize64;
                        peakWorkingSet = proc.PeakWorkingSet64;
                    }
                }
                while (!proc.WaitForExit(10));

                processorTime = proc.TotalProcessorTime.TotalMilliseconds;
                exitCode = proc.ExitCode;
            }

            Console.WriteLine("Exit code: " + exitCode);
            Console.WriteLine("Paged Memory: " + peakPagedMem);
            Console.WriteLine("Virtual Memory: " + peakVirtualMem);
            Console.WriteLine("Working Set: " + peakWorkingSet);
            Console.WriteLine("Processor Time (ms): " + processorTime);
            Console.Read();
        }
    }
}<commit_msg>Improving the performance reporting<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace MongoExperiments.Performance
{
    class Program
    {
        static void Main(string[] args)
        {
            var iterations = 10;
            //var fileName = @"..\..\..\MongoExperiments.CS\bin\debug\MongoExperiments.cs.exe";
            var fileName = @"""C:\Program Files (x86)\nodejs\node.exe""";
            //var arguments = "";
            var arguments = @"..\..\..\MongoExperiments.JS\app.js";
            var reportName = ".\\Mongo.JS.10.csv";

            var start = new ProcessStartInfo
            {
                Arguments = arguments,
                FileName = fileName,
                WindowStyle = ProcessWindowStyle.Hidden,
                CreateNoWindow = true
            };

            var report = new PerformanceReport
            {
                FileName = reportName
            };

            for (int i = 0; i < iterations; i++)
            {
                var result = new PerformanceResult
                {
                    RunNumber = i,
                    FileName = fileName,
                    Arguments = arguments
                };

                using (var proc = Process.Start(start))
                {
                    do
                    {
                        if (!proc.HasExited)
                        {
                            proc.Refresh();

                            result.PeakPagedMemory = proc.PeakPagedMemorySize64;
                            result.PeakVirtualMemory = proc.PeakVirtualMemorySize64;
                            result.PeakWorkingSet = proc.PeakWorkingSet64;
                        }
                    }
                    while (!proc.WaitForExit(10));

                    result.ProcessorTime = (long) Math.Round(proc.TotalProcessorTime.TotalMilliseconds,0);
                    result.ExitCode = proc.ExitCode;
                }
                report.Add(result);
            }

            report.WriteFile();

        }

    }

    public class PerformanceResult
    {
        public int RunNumber { get; set; }
        public string FileName { get; set; }
        public string Arguments { get; set; }
        public int ExitCode { get; set; }
        public long PeakPagedMemory { get; set; }
        public long PeakWorkingSet { get; set; }
        public long PeakVirtualMemory { get; set; }
        public long ProcessorTime { get; set; }

        public override string ToString() => "\{RunNumber}, \{FileName}, \{Arguments}, \{ExitCode}, \{PeakPagedMemory}, \{PeakVirtualMemory}, \{PeakWorkingSet}, \{ProcessorTime}";
    }

    public class PerformanceReport
    {
        private List<PerformanceResult> _results = new List<PerformanceResult>();

        public string FileName { get; set; }

        public PerformanceReport Add(PerformanceResult result)
        {
            _results.Add(result);
            return this;
        }

        public void WriteFile()
        {
            var header = "Run Number, filename, arguments, exit code, paged memory, virtual memory, working set, processor time";
            

            var builder = new StringBuilder()
                                .AppendLine(header)
                                .AppendDetailLines(_results)
                                .AppendLine(GenerateStatistics());

            File.WriteAllText(FileName, builder.ToString());
        }

        private string GenerateStatistics()
        {
            var footer = "Averages, , , , {0}, {1}, {2}, {3}";

            long iterations = _results.Count();
            var avgPaged = CalculateAverageFor(x => x.PeakPagedMemory);
            var avgVirtual = CalculateAverageFor(x => x.PeakVirtualMemory);
            var avgWorking = CalculateAverageFor(x => x.PeakWorkingSet);
            var avgtime = CalculateAverageFor(x => x.ProcessorTime);

            return string.Format(footer, avgPaged, avgVirtual, avgWorking, avgtime);
        }
        
        private string CalculateAverageFor(Func<PerformanceResult, long> expression)
        {
            var avg = _results.Where(x => x.ExitCode == 0)
                           .Select(expression)
                           .Sum() / (long)_results.Count();

            return avg.ToString();
        }
    }

    public static class ReportStringBuilder
    {
        public static StringBuilder AppendLine(this StringBuilder builder, string line)
        {
            builder.AppendLine(line);
            return builder;
        }


        public static StringBuilder AppendDetailLines(this StringBuilder builder, IEnumerable<PerformanceResult> results)
        {
            foreach (var item in results)
            {
                builder.AppendLine(item.ToString());
            }

            return builder;
        }
    }
}
{'fraction_non_alphanumeric': 0.09567630733274905, 'fraction_numerical': 0.0052585451358457495, 'mean_word_length': 1.4594109195402298, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4924486', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1604, 'n_words': 371}