public KeyValuePair <string, int>[] GetTop10Users(string logPath) { var usersStats = new Dictionary <string, int>(); var fileLines = File.ReadAllLines(logPath); Parallel.For(0, fileLines.Length, new ParallelOptions { MaxDegreeOfParallelism = 2 }, i => { var ipInfo = IpInfo.Parse(fileLines[i]); lock (usersStats) { if (!usersStats.ContainsKey(ipInfo.Ip)) { usersStats.Add(ipInfo.Ip, ipInfo.CallDuration); } else { usersStats[ipInfo.Ip] += ipInfo.CallDuration; } } }); return(usersStats.OrderByDescending(keyValuePair => keyValuePair.Value).Take(10).ToArray()); }
public KeyValuePair <string, int>[] GetTop10Users(string logPath) { var usersStats = new ConcurrentDictionary <string, int>(); Parallel.ForEach(File.ReadLines(logPath), () => new Dictionary <string, int>(), (line, _, localDictionary) => { var ipInfo = IpInfo.Parse(line); if (!localDictionary.ContainsKey(ipInfo.Ip)) { localDictionary.Add(ipInfo.Ip, ipInfo.CallDuration); } else { localDictionary[ipInfo.Ip] += ipInfo.CallDuration; } return(localDictionary); }, localDictionary => { foreach (var ip in localDictionary.Keys) { usersStats.AddOrUpdate(ip, key => localDictionary[key], (key, oldValue) => oldValue + localDictionary[key]); } } ); return(usersStats.OrderByDescending(keyValuePair => keyValuePair.Value).Take(10).ToArray()); }
public KeyValuePair <string, int>[] GetTop10Users(string logPath) { var usersStats = new ConcurrentDictionary <string, int>(); foreach (var line in File.ReadLines(logPath)) { var ipInfo = IpInfo.Parse(line); usersStats.AddOrUpdate(ipInfo.Ip, ipInfo.CallDuration, (key, oldDuration) => oldDuration + ipInfo.CallDuration); } return(usersStats.OrderByDescending(keyValuePair => keyValuePair.Value).Take(10).ToArray()); }
public KeyValuePair <string, int>[] GetTop10Users(string logPath) { var usersStats = new ConcurrentDictionary <string, int>(); Parallel.ForEach(File.ReadLines(logPath), () => new Dictionary <string, int>(), (line, _, __) => { var ipInfo = IpInfo.Parse(line); usersStats.AddOrUpdate(ipInfo.Ip, ipInfo.CallDuration, (key, oldDuration) => oldDuration + ipInfo.CallDuration); return(null); }, localDictionary => { } ); return(usersStats.OrderByDescending(keyValuePair => keyValuePair.Value).Take(10).ToArray()); }
public KeyValuePair <string, int>[] GetTop10Users(string logPath) { var usersStats = new Dictionary <string, int>(); foreach (var line in File.ReadLines(logPath)) { var ipInfo = IpInfo.Parse(line); if (!usersStats.ContainsKey(ipInfo.Ip)) { usersStats.Add(ipInfo.Ip, ipInfo.CallDuration); } else { usersStats[ipInfo.Ip] += ipInfo.CallDuration; } } return(usersStats.OrderByDescending(keyValuePair => keyValuePair.Value).Take(10).ToArray()); }
public KeyValuePair <string, int>[] GetTop10Users(string logPath) { var queue = new BlockingCollection <string>(10000); var processorCount = Environment.ProcessorCount; var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None); taskFactory.StartNew(() => { foreach (var line in File.ReadLines(logPath)) { queue.Add(line); } queue.CompleteAdding(); }); var consumers = new Task <Dictionary <string, int> > [processorCount]; for (int i = 0; i < consumers.Length; i++) { consumers[i] = taskFactory.StartNew(() => { var localDictionary = new Dictionary <string, int>(); while (!queue.IsCompleted) { try { var line = queue.Take(); var ipInfo = IpInfo.Parse(line); if (!localDictionary.ContainsKey(ipInfo.Ip)) { localDictionary.Add(ipInfo.Ip, ipInfo.CallDuration); } else { localDictionary[ipInfo.Ip] += ipInfo.CallDuration; } } catch (ObjectDisposedException) { } catch (InvalidOperationException) { } } return(localDictionary); }); } var summarizedDictionary = new Dictionary <string, int>(); foreach (var dictionary in Task.WhenAll(consumers).Result) { foreach (var key in dictionary.Keys) { if (!summarizedDictionary.ContainsKey(key)) { summarizedDictionary.Add(key, dictionary[key]); } else { summarizedDictionary[key] += dictionary[key]; } } } return(summarizedDictionary.OrderByDescending(keyValuePair => keyValuePair.Value).Take(10).ToArray()); }