<commit_before>﻿using System;
using System.Diagnostics;
using System.Linq;

using ProcessGremlins;
using ProcessGremlinImplementations.Logging;

namespace ProcessGremlinImplementations
{
    public class KillBusyGremlin : IGremlin
    {
        private readonly int busyThreshold;
        private readonly IProcessFinder finder;
        private readonly IEventLogger logger;

        // busyThreshold in % of core being used, ie up to 400 on a 4-core
        public KillBusyGremlin(IProcessFinder processFinder, int busyThreshold, IEventLogger logger)
        {
            this.busyThreshold = busyThreshold;
            this.finder = processFinder;
            this.logger = logger;
        }

        public void Meddle()
        {
            var data = this.finder.Find();
            var busyProcesses = data.Where(process => this.GetCpuUsage(process) > this.busyThreshold).ToList();
            foreach (var process in busyProcesses)
            {
                process.Kill();
                this.logger.Log(new ProcessKilledEvent(process));    
            }
        }

        private int GetCpuUsage(Process process)
        {
            var cpuCounter = new PerformanceCounter("Process", "% Processor Time", this.GetInstanceName(process), true);
            cpuCounter.NextValue();
            System.Threading.Thread.Sleep(1000);
            var usage = cpuCounter.NextValue();
            return (int)usage;
        }


        private string GetInstanceName(Process process)
        {
            PerformanceCounterCategory category = new PerformanceCounterCategory("Process");
            string[] instances = category.GetInstanceNames();

            string instanceName = null;
            foreach (string instance in instances)
            {
                using (PerformanceCounter counter = new PerformanceCounter("Process",
                     "ID Process", instance, true))
                {
                    if ((int)counter.RawValue == process.Id)
                    {
                        instanceName = instance;
                        break;
                    }
                }<commit_msg>move sample time to const, bear in mind GetCpuUsage runs very slow<commit_after>﻿using System;
using System.Diagnostics;
using System.Linq;

using ProcessGremlins;
using ProcessGremlinImplementations.Logging;

namespace ProcessGremlinImplementations
{
    public class KillBusyGremlin : IGremlin
    {
        private readonly int busyThreshold;
        private readonly IProcessFinder finder;
        private readonly IEventLogger logger;
        private const int SampleTime = 1000;

        // busyThreshold in % of core being used, ie up to 400 on a 4-core
        public KillBusyGremlin(IProcessFinder processFinder, int busyThreshold, IEventLogger logger)
        {
            this.busyThreshold = busyThreshold;
            this.finder = processFinder;
            this.logger = logger;
        }

        public void Meddle()
        {
            var data = this.finder.Find();
            var busyProcesses = data.Where(process => this.GetCpuUsage(process) > this.busyThreshold).ToList();
            foreach (var process in busyProcesses)
            {
                process.Kill();
                this.logger.Log(new ProcessKilledEvent(process));    
            }
        }

        private int GetCpuUsage(Process process)
        {
            var cpuCounter = new PerformanceCounter("Process", "% Processor Time", this.GetInstanceName(process), true);
            cpuCounter.NextValue();
            System.Threading.Thread.Sleep(SampleTime);
            var usage = cpuCounter.NextValue();
            return (int)usage;
        }


        private string GetInstanceName(Process process)
        {
            PerformanceCounterCategory category = new PerformanceCounterCategory("Process");
            string[] instances = category.GetInstanceNames();

            string instanceName = null;
            foreach (string instance in instances)
            {
                using (PerformanceCounter counter = new PerformanceCounter("Process",
                     "ID Process", instance, true))
                {
                    if ((int)counter.RawValue == process.Id)
                    {
                        instanceName = instance;
                        break;
                    }
                }
{'fraction_non_alphanumeric': 0.07521719250114312, 'fraction_numerical': 0.003657978966620942, 'mean_word_length': 1.8594771241830066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6986886', 'n_tokens_mistral': 1050, 'n_tokens_neox': 995, 'n_words': 268}