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

namespace TcpPing
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            try
            {
                Driver(args);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static void Driver(IReadOnlyList<string> args)
        {
            if (args[0].Split(':').Length != 2)
            {
                throw new ArgumentException("Please specify hostname and port in hostname:port format.");
            }

            var hostname = args[0].Split(':')[0];
            var ip = GetEndPointIp(hostname);

            var port = GetPortNumber(args[0].Split(':')[1]);

            var endPoint = new IPEndPoint(ip, port);

            Console.WriteLine(
                $"Pinging {hostname}:{port} [{ip}:{port}]:");

            var pingResults = new List<double?>();

            WarmUp(endPoint);
            foreach (var delay in TcpPing(endPoint))
            {
                pingResults.Add(delay);
                Console.WriteLine(
                    delay.HasValue
                        ? $"time = {delay.Value:0.00} ms"
                        : "Request timed out");
            }

            var validDelays = pingResults
                .Where(t => t.HasValue)
                .Select(t => t.Value)
                .ToList();
            Console.WriteLine(
                $"--- {ip}:{port} ping statistics ---");

            if (validDelays.Any())
                Console.WriteLine(
                    $"round-trip min/avg/max = {validDelays.Min():0.00}/{validDelays.Max():0.00}/{validDelays.Average():0.00} ms");

            var sentCount = pingResults.Count;
            var receivedCount = pingResults.Count(t => t.HasValue);
            var lossCount = sentCount - receivedCount;
            var lossRate = lossCount / sentCount;
            if (lossCount > 0)
                Console.WriteLine(
                    $"{sentCount} packets transmitted, {receivedCount} packets received, {100 * lossRate:0.0}% packet loss");
        }

        // ReSharper disable once SuggestBaseTypeForParameter
        private static IEnumerable<double?> TcpPing(IPEndPoint endPoint)
        {
            for (var i = 0; i < 4; i++)<commit_msg>Refactor<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;

namespace TcpPing
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            try
            {
                Driver(args);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static void Driver(IReadOnlyList<string> args)
        {
            if (args[0].Split(':').Length != 2)
            {
                throw new ArgumentException("Please specify hostname and port in hostname:port format.");
            }

            var hostname = args[0].Split(':')[0];
            var ip = GetEndPointIp(hostname);

            var port = GetPortNumber(args[0].Split(':')[1]);

            var endPoint = new IPEndPoint(ip, port);

            Console.WriteLine(
                $"Pinging {hostname}:{port} [{ip}:{port}]:");

            var pingResults = new List<double?>();

            WarmUp(endPoint);
            foreach (var delay in TcpPing(endPoint))
            {
                pingResults.Add(delay);
                Console.WriteLine(
                    delay.HasValue
                        ? $"time = {delay.Value:0.00} ms"
                        : "Request timed out");
            }

            Console.WriteLine(
                $"--- {ip}:{port} ping statistics ---");

            var validDelays = pingResults
                .Where(t => t.HasValue)
                .Select(t => t.Value)
                .ToList();
            if (validDelays.Any())
                Console.WriteLine(
                    $"round-trip min/avg/max = {validDelays.Min():0.00}/{validDelays.Max():0.00}/{validDelays.Average():0.00} ms");

            var sentCount = pingResults.Count;
            var receivedCount = pingResults.Count(t => t.HasValue);
            var lossCount = sentCount - receivedCount;
            var lossRate = lossCount / sentCount;
            if (lossCount > 0)
                Console.WriteLine(
                    $"{sentCount} packets transmitted, {receivedCount} packets received, {100 * lossRate:0.0}% packet loss");
        }

        // ReSharper disable once SuggestBaseTypeForParameter
        private static IEnumerable<double?> TcpPing(IPEndPoint endPoint)
        {
            for (var i = 0; i < 4; i++)
{'fraction_non_alphanumeric': 0.1171081457876917, 'fraction_numerical': 0.010356502688707428, 'mean_word_length': 1.5687979539641943, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '1257956', 'n_tokens_mistral': 1406, 'n_tokens_neox': 1317, 'n_words': 301}