Created
October 14, 2019 13:42
-
-
Save yar-shukan/a138781344a5ed9f321ac3724ee08d77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Drawing; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Table; | |
using Newtonsoft.Json; | |
namespace ConsoleApp2 | |
{ | |
public class Program2 | |
{ | |
private const string TableConnectionString = | |
""; | |
private static int _counter = 0; | |
private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings() | |
{ | |
NullValueHandling = NullValueHandling.Ignore | |
}; | |
private static readonly Random Random = new Random(); | |
private static readonly CloudTable _tableReference; | |
static Program2() | |
{ | |
var storageAccount = CloudStorageAccount.Parse(TableConnectionString); | |
var cloudTableClient = storageAccount.CreateCloudTableClient(); | |
_tableReference = cloudTableClient.GetTableReference("WiFiAnalytics"); | |
} | |
public static async Task Main(string[] args) | |
{ | |
await MainAsync(args); | |
} | |
private static async Task MainAsync(string[] args) | |
{ | |
while (true) | |
{ | |
var startNew = Stopwatch.StartNew(); | |
_counter = 0; | |
await SendRequestsAsync(requestsCount: 100, minMessagesPerRequest: 50); | |
var milliseconds = Math.Max(startNew.ElapsedMilliseconds, 1); | |
decimal messagesPerSecond = _counter / (decimal)milliseconds * 1000; | |
Console.WriteLine($"\r\n\r\nThroughput: {messagesPerSecond} m/sec\r\n\r\n"); | |
} | |
} | |
private static async Task SendRequestsAsync(int requestsCount, int minMessagesPerRequest) | |
{ | |
var requestsTasks = Enumerable.Range(0, requestsCount).Select(index => | |
{ | |
int messagesPerThisRequest = Random.Next(minMessagesPerRequest, minMessagesPerRequest + 100 + index); | |
return SendRequestsAsync(messagesPerThisRequest); | |
// return SendRequestUsingParallelAsync(messagesPerThisRequest, index); | |
}).ToArray(); | |
await Task.WhenAll(requestsTasks).ConfigureAwait(false); | |
} | |
private static async Task SendRequestsAsync(int numMessagesToSend) | |
{ | |
var events = Enumerable.Range(0, numMessagesToSend) | |
.Select(_ => WifiLocationAnalytic.CreateRandom()) | |
.ToArray(); | |
try | |
{ | |
var table = GetClient(); | |
//var ops = events.GroupBy(x => x.PartitionKey).Select(group => | |
// { | |
// var batch = new TableBatchOperation(); | |
// foreach (WifiLocationAnalytic analytic in @group) | |
// { | |
// batch.Add(TableOperation.Insert(analytic)); | |
// } | |
// return table.ExecuteBatchAsync(batch); | |
// }) | |
// .ToArray(); | |
var ops = events.Select(TableOperation.Insert).Select(x => table.ExecuteAsync(x)).ToArray(); | |
await Task.WhenAll(ops).ConfigureAwait(false); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e); | |
} | |
Interlocked.Add(ref _counter, events.Length); | |
} | |
private static CloudTable GetClient() | |
{ | |
//var storageAccount = CloudStorageAccount.Parse(TableConnectionString); | |
//var cloudTableClient = storageAccount.CreateCloudTableClient(); | |
//var tableReference = cloudTableClient.GetTableReference("WiFiAnalytics"); | |
return _tableReference; | |
} | |
public class WifiLocationAnalytic : TableEntity | |
{ | |
public string Id { get; set; } | |
public string AccessPointMacAddress { get; set; } | |
public List<string> AccessPointFloors { get; set; } | |
public List<string> AccessPointTags { get; set; } | |
public string ClientMacAddress { get; set; } | |
public string IpAddressV4 { get; set; } | |
public string IpAddressV6 { get; set; } | |
public DateTime? SeenTime { get; set; } | |
public long? SeenEpoch { get; set; } | |
public string Ssid { get; set; } | |
public int? Rssi { get; set; } | |
public string Manufacturer { get; set; } | |
public string OperatingSystem { get; set; } | |
public decimal? Latitude { get; set; } | |
public decimal? Longitude { get; set; } | |
public decimal? Uncertainty { get; set; } | |
public List<decimal> XOffsets { get; set; } | |
public List<decimal> YOffsets { get; set; } | |
public string TimeStamp { get; set; } | |
public System.DateTime CreateDate { get; set; } | |
public DateTime? EditDate { get; set; } | |
public DateTime? DeleteDate { get; set; } | |
private static readonly Random Random = new Random(); | |
private static readonly string[] _deviceMacs; | |
private static readonly string[] _accessPointMacs; | |
private static readonly string[] _ips; | |
private static readonly PointF[] _locations; | |
private static readonly string[] _oses; | |
private static readonly string[] _floors; | |
public static WifiLocationAnalytic CreateRandom() | |
{ | |
var pointF = GetRandom(_locations); | |
var result = new WifiLocationAnalytic | |
{ | |
Id = Guid.NewGuid().ToString(), | |
ClientMacAddress = GetRandom(_deviceMacs), | |
AccessPointMacAddress = GetRandom(_accessPointMacs), | |
IpAddressV4 = GetRandom(_ips), | |
Latitude = (decimal?)pointF.X, | |
Longitude = (decimal?)pointF.Y, | |
SeenEpoch = DateTimeOffset.UtcNow.ToUnixTimeSeconds(), | |
OperatingSystem = GetRandom(_oses), | |
AccessPointFloors = new List<string>(1) { GetRandom(_floors) }, | |
CreateDate = DateTime.UtcNow, | |
TimeStamp = DateTime.UtcNow.ToLongDateString(), | |
SeenTime = DateTime.UtcNow | |
}; | |
result.PartitionKey = result.Id; | |
result.RowKey = result.ClientMacAddress; | |
return result; | |
} | |
static WifiLocationAnalytic() | |
{ | |
_deviceMacs = GetMacs(50000); | |
_accessPointMacs = GetMacs(300); | |
_ips = GetIps(1000); | |
_locations = GetLocations(100000); | |
_oses = GetOses(); | |
_floors = GetFloors(); | |
} | |
private static T GetRandom<T>(T[] array) | |
{ | |
return array[Random.Next(0, array.Length)]; | |
} | |
private static PointF[] GetLocations(int count) | |
{ | |
var hashSet = new HashSet<PointF>(); | |
while (hashSet.Count < count) | |
{ | |
var lat = Random.Next(70, 80); | |
var lon = Random.Next(50, 60); | |
hashSet.Add(new PointF((float)(lat + Random.NextDouble()), (float)(lon + Random.NextDouble()))); | |
} | |
return hashSet.ToArray(); | |
} | |
private static string[] GetOses() | |
{ | |
return new[] | |
{ | |
"Windows Server 2012", "Windows Server 2016", "Windows 8.1", | |
"Windows 10", "MacOS", "Android 5.0", "Android 7.0", "Android 8.0", "IOS 6", "IOS 7", "IOS 8", | |
"IOS 9", "IOS 10" | |
}; | |
} | |
private static string[] GetFloors() | |
{ | |
return new[] | |
{ | |
"1st floor", | |
"2nd floor", | |
"3d floor", | |
"4th floor", | |
"roof top" | |
}; | |
} | |
private static string[] GetMacs(int count) | |
{ | |
var hashSet = new HashSet<string>(StringComparer.Ordinal); | |
while (hashSet.Count < count) | |
{ | |
hashSet.Add(GetRandomMac()); | |
} | |
return hashSet.ToArray(); | |
} | |
private static string[] GetIps(int count) | |
{ | |
var hashSet = new HashSet<string>(StringComparer.Ordinal); | |
while (hashSet.Count < count) | |
{ | |
hashSet.Add(GetRandomIp()); | |
} | |
return hashSet.ToArray(); | |
} | |
private static string GetRandomIp() | |
{ | |
var v1 = Random.Next(0, 256); | |
var v2 = Random.Next(0, 256); | |
var v3 = Random.Next(0, 256); | |
var v4 = Random.Next(0, 256); | |
return $"{v1}.{v2}.{v3}.{v4}"; | |
} | |
private static string GetRandomMac() | |
{ | |
string guid = Guid.NewGuid().ToString("N"); | |
var result = new char[6 * 2 + 5]; | |
for (int i = 0; i < result.Length; i++) | |
{ | |
if ((i + 1) % 3 == 0) | |
{ | |
result[i] = ':'; | |
} | |
else | |
{ | |
result[i] = guid[i]; | |
} | |
} | |
return new string(result); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment