Commit 6d1013f9 by Kumar

scan test code

0 parents
using System;
using System.Collections.Generic;
using System.Text;
namespace Google.Cloud.Bigtable.ScanTest
{
class AppSettings
{
public string InstanceId { get; set; }
public string ProjectId { get; set; }
public string TableName { get; set; }
public string ColumnFamily { get; set; }
public string RowKeyPrefix { get; set; }
public string LogFile { get; set; }
public string PerfFile { get; set; }
public long Records { get; set; }
public int LoadThreads { get; set; }
public int RowKeySize { get; set; }
public int ColumnLength { get; set; }
public string ColumnPrefix { get; set; }
public int ScanInterval { get; set; }
public int Columns { get; set; }
public int BatchSize { get; set; }
public string ScanLimits { get; set; }
public int MutateRowTimeOutInMilliSeconds { get; set; }
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="HdrHistogram" Version="2.5.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\google\apis\Google.Cloud.Bigtable.Admin.V2\Google.Cloud.Bigtable.Admin.V2\Google.Cloud.Bigtable.Admin.V2.csproj" />
<ProjectReference Include="..\..\..\..\..\google\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.V2.csproj" />
</ItemGroup>
</Project>

using Google.Cloud.Bigtable.Admin.V2;
using Google.Cloud.Bigtable.V2;
using HdrHistogram;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
namespace Google.Cloud.Bigtable.ScanTest
{
class Program
{
public static AppSettings AppSetting;
public static int ScanTime;
public static long ScanCount = 0;
public static long RecordScanned = 0;
public static long ThroughputTime = 0;
public static DateTime _startTime = DateTime.Now;
public static bool IsDataLoaded = false;
static void Main(string[] args)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Starting Scan test");
var builder = new ConfigurationBuilder().
SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
IConfigurationRoot configuration = builder.Build();
AppSetting = new AppSettings();
configuration.Bind(AppSetting);
V2.TableName _table = new tableHelper().getTable();
/*string[] arrLimit = AppSetting.ScanLimits.Split(',');
Hashtable _result = new Hashtable();
System.Timers.Timer _timer = new System.Timers.Timer();
_timer = new System.Timers.Timer();
_timer.Elapsed += _timer_Elapsed;
_timer.Interval = 60000; //minute
foreach (string arr in arrLimit)
{
if (arr.Trim().Length > 0)
{
if (Convert.ToInt32(arr.Trim()) > 0)
{
_timer.Enabled = true;
Program.ScanTime = 0;
Program.ThroughputTime = 0;
Program.ScanCount = 0;
Program.RecordScanned = 0;
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Table {AppSetting.TableName} time interval: {AppSetting.ScanInterval} mins.");
new scanHelper(_table).scan(Convert.ToInt32(arr.Trim())).Wait();
//_scanResult.Wait();
int _throughtput = Convert.ToInt32(Program.RecordScanned / (Program.ThroughputTime / 1000));
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Table {AppSetting.TableName} time interval: {AppSetting.ScanInterval} mins, limit: {arr.Trim()}, throughput time:{_throughtput}.");
_result[arr.Trim()] = _throughtput;
_timer.Enabled = false;
}
}
}
_timer.Stop();
WriteCsv(_result);
WriteToConsole(_result);*/
if (IsDataLoaded)
{
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine($"Main thread going to sleep mode for {AppSetting.MutateRowTimeOutInMilliSeconds} milliseconds");
Thread.Sleep(AppSetting.MutateRowTimeOutInMilliSeconds);
}
Environment.Exit(0);
}
private static void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Performed {ScanCount} scans in : {ScanTime + 1} mins, total scanned {RecordScanned} rows, throughput: {ThroughputTime} milliseconds");
++ScanTime;
}
static void WriteCsv(Hashtable hash)
{
try
{
using (StreamWriter writetext = new StreamWriter(AppSetting.LogFile))
{
writetext.WriteLine("Number of rows per scan, Throughput");
foreach (DictionaryEntry pair in hash)
{
writetext.WriteLine(pair.Key.ToString() + "," + pair.Value.ToString());
}
writetext.WriteLine("");
writetext.WriteLine("");
writetext.WriteLine("Client, Type, StartTime");
writetext.WriteLine($"C#, scant, {_startTime.ToString("yyyy-MM-dd HH:mm:ss")}");
}
}
catch (Exception ex)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} Exception writing test results into {AppSetting.LogFile },error: {ex.Message}");
}
}
static void WriteToConsole(Hashtable hash)
{
try
{
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("---------------------RESULT--------------------");
Console.WriteLine("Number of rows per scan, Throughput");
foreach (DictionaryEntry pair in hash)
{
Console.WriteLine(pair.Key.ToString() + "," + pair.Value.ToString());
}
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("Client, Type, StartTime");
Console.WriteLine($"C#, scant, {_startTime.ToString("yyyy-MM-dd HH:mm:ss")}");
}
catch (Exception ex)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} Exception writing test results into {AppSetting.LogFile },error: {ex.Message}");
}
}
}
}
{
"profiles": {
"Google.Cloud.Bigtable.ScanTest": {
"commandName": "Project",
"environmentVariables": {
"GOOGLE_APPLICATION_CREDENTIALS": " C:\\qlogic\\google-cloud-dotnet\\apis\\Google.Cloud.Bigtable.V2\\Grass Clump 479-b5c624400920.json"
}
}
}
}
\ No newline at end of file
{
"instanceId": "dotnet-perf",
"projectId": "grass-clump-479",
"tableName": "perfscantest",
"columnFamily": "cf",
"rowKeyPrefix": "user",
"rowKeySize": 7,
"records": 1000,
"batchSize": 1000,
"loadThreads": 10,
"columnLength": 100,
"columns": 10,
"columnPrefix": "field",
"scanLimits": "100",
"scanInterval": 1,
"mutateRowTimeOutInMilliSeconds": 60000,
"logFile": "c:\\qlogic\\google\\scan.txt"
}
\ No newline at end of file
This diff could not be displayed because it is too large.
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\Amrendra\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\Amrendra\\.nuget\\packages",
"C:\\Program Files\\dotnet\\sdk\\NuGetFallbackFolder"
]
}
}
\ No newline at end of file
{
"runtimeOptions": {
"tfm": "netcoreapp2.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "2.0.0"
}
}
}
\ No newline at end of file
This diff could not be displayed because it is too large.
using System;
using System.Collections.Generic;
using System.Text;
using HdrHistogram;
namespace Google.Cloud.Bigtable.ScanTest
{
class histogramHelper
{
public static LongConcurrentHistogram ScanResponseHistogram = new LongConcurrentHistogram(3, TimeStamp.Hours(1), 3); // ticks
public static LongConcurrentHistogram ThroughputHistogram = new LongConcurrentHistogram(3, TimeStamp.Hours(1), 3);
public static LongConcurrentHistogram LoadHistogram = new LongConcurrentHistogram(3, TimeStamp.Hours(1), 3);
}
}
using Google.Cloud.Bigtable.V2;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using HdrHistogram;
using System.Threading.Tasks;
using Google.Api.Gax.Grpc;
namespace Google.Cloud.Bigtable.ScanTest
{
class loadDataHelper
{
private AppSettings _appSettings;
private V2.TableName _table;
private BigtableClient _client;
private Random _rnd = new Random();
public loadDataHelper(V2.TableName _tableName)
{
_appSettings = Program.AppSetting;
_table = _tableName;
_client = BigtableClient.Create();
Program.IsDataLoaded = true;
}
public void loadTable()
{
//List<Thread> threads = new List<Thread>();
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Loading {_appSettings.Records} records into table {_appSettings.TableName}. Threads: {_appSettings.LoadThreads}. Cluster: {_appSettings.InstanceId}");
try
{
var tasks = new List<Task<int>>();
long recordsPerThread = _appSettings.Records / _appSettings.LoadThreads;
long reminder = _appSettings.Records % _appSettings.LoadThreads;
//initialize load histogram again
//_loadHistogram = new LongConcurrentHistogram(3, TimeStamp.Hours(1), 3);
for (int threadCount = 0; threadCount < _appSettings.LoadThreads; threadCount++)
{
int _position = threadCount;
if (threadCount == _appSettings.LoadThreads - 1)
{
//threads.Add(new Thread(() => processLoad(_position, recordsPerThread, recordsPerThread + reminder)));
string _input = _position.ToString() + "/" + recordsPerThread.ToString() + "/" + (recordsPerThread + reminder).ToString();
tasks.Add(Task<int>.Factory.StartNew(processLoad, _input));
}
else
{
// threads.Add(new Thread(() => processLoad(_position, recordsPerThread, recordsPerThread)));
string _input = _position.ToString() + "/" + recordsPerThread.ToString() + "/" + recordsPerThread.ToString();
tasks.Add(Task<int>.Factory.StartNew(processLoad, _input));
}
}
Task.WaitAll(tasks.ToArray());
//threads.ForEach(a => a.Start());
//threads.ForEach(a => a.Join());
//threads.Clear();
//threads = null;
tasks.ForEach(a => a.Dispose());
tasks.Clear();
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Data loaded successfully into table {_appSettings.TableName}");
}
catch (Exception ex)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Exception while loading data, error message: {ex.Message}");
}
}
private int processLoad(object input)
{
try
{
string[] _array = input.ToString().Split('/');
int threadNumber = Convert.ToInt32(_array[0]);
long startPosition = Convert.ToInt64(_array[1]);
long totalRecords = Convert.ToInt64(_array[2]);
int recordCount = 0;
MutateRowsRequest request = new MutateRowsRequest() { TableNameAsTableName = _table };
MutateRowsRequest.Types.Entry entry = new MutateRowsRequest.Types.Entry();
// iterating through number of records assigned to a thread
long recordNumber;
for (int insertCount = 0; insertCount < totalRecords; insertCount++)
{
recordCount++;
entry = new MutateRowsRequest.Types.Entry();
entry.Mutations.Add(MutationsBuilder());
// building a batch of defined size
recordNumber = threadNumber * startPosition + insertCount;
StringBuilder sbRowKey = new StringBuilder(_appSettings.RowKeyPrefix + recordNumber.ToString("D" + _appSettings.RowKeySize));
entry.RowKey = Protobuf.ByteString.CopyFromUtf8(sbRowKey.ToString());
request.Entries.Add(entry);
if (recordCount == _appSettings.BatchSize || insertCount == totalRecords - 1)
{
long startTime = Stopwatch.GetTimestamp();
long responseTime;
recordCount = 0;
try
{
#if DEBUG
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Thread: {threadNumber + 1} Inserting record {insertCount + 1}. Last rowkey {sbRowKey.ToString()} for table {_appSettings.TableName}");
#endif
BigtableClient.MutateRowsStream response = _client.MutateRows(request
, CallSettings.FromCallTiming(CallTiming.FromTimeout(TimeSpan.FromMilliseconds(_appSettings.MutateRowTimeOutInMilliSeconds))));
response.GrpcCall.ResponseHeadersAsync.Wait();
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Exception inserting record {insertCount + 1}. Last rowkey {sbRowKey.ToString()} for table {_appSettings.TableName}");
#endif
}
responseTime = (Stopwatch.GetTimestamp() - startTime) / (Stopwatch.Frequency / 100000);
histogramHelper.LoadHistogram.RecordValue(responseTime);
request = new MutateRowsRequest
{
TableNameAsTableName = _table
};
}
}
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} Exception to load data for table {_appSettings.TableName}. Error Message: {ex.Message}");
#endif
}
return 0;
}
// Constructing mutation for 1 row
private Mutation[] MutationsBuilder()
{
Mutation[] _columns = new Mutation[_appSettings.Columns];
for (int cellCount = 0; cellCount < _columns.Length; cellCount++)
{
_columns[cellCount] = Mutations.SetCell(_appSettings.ColumnFamily, $"{_appSettings.ColumnPrefix}{cellCount}", new BigtableByteString(RandomData()));
}
return _columns;
}
private Byte[] RandomData()
{
Byte[] b = new Byte[_appSettings.ColumnLength];
_rnd.NextBytes(b);
return b;
}
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Google.Cloud.Bigtable.ScanTest")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("Google.Cloud.Bigtable.ScanTest")]
[assembly: System.Reflection.AssemblyTitleAttribute("Google.Cloud.Bigtable.ScanTest")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\bin\Debug\netcoreapp2.0\Google.Cloud.Bigtable.ScanTest.deps.json
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\bin\Debug\netcoreapp2.0\Google.Cloud.Bigtable.ScanTest.runtimeconfig.json
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\bin\Debug\netcoreapp2.0\Google.Cloud.Bigtable.ScanTest.runtimeconfig.dev.json
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\bin\Debug\netcoreapp2.0\Google.Cloud.Bigtable.ScanTest.dll
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\obj\Debug\netcoreapp2.0\Google.Cloud.Bigtable.ScanTest.csprojResolveAssemblyReference.cache
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\obj\Debug\netcoreapp2.0\Google.Cloud.Bigtable.ScanTest.csproj.CoreCompileInputs.cache
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\obj\Debug\netcoreapp2.0\Google.Cloud.Bigtable.ScanTest.AssemblyInfoInputs.cache
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\obj\Debug\netcoreapp2.0\Google.Cloud.Bigtable.ScanTest.AssemblyInfo.cs
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\bin\Debug\netcoreapp2.0\Google.Cloud.Bigtable.ScanTest.pdb
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\obj\Debug\netcoreapp2.0\Google.Cloud.Bigtable.ScanTest.dll
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\obj\Debug\netcoreapp2.0\Google.Cloud.Bigtable.ScanTest.pdb
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\bin\Debug\netcoreapp2.0\Google.Cloud.Bigtable.Admin.V2.dll
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\bin\Debug\netcoreapp2.0\Google.Cloud.Bigtable.V2.dll
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\bin\Debug\netcoreapp2.0\Google.Cloud.Bigtable.Admin.V2.pdb
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\bin\Debug\netcoreapp2.0\Google.Cloud.Bigtable.Admin.V2.xml
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\bin\Debug\netcoreapp2.0\Google.Cloud.Bigtable.V2.pdb
C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\bin\Debug\netcoreapp2.0\Google.Cloud.Bigtable.V2.xml
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">C:\qlogic\google\qlogic\apis\Google.Cloud.Bigtable.V2\Google.Cloud.Bigtable.ScanTest\Google.Cloud.Bigtable.ScanTest\obj\project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Amrendra\.nuget\packages\;C:\Program Files\dotnet\sdk\NuGetFallbackFolder</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">4.4.0</NuGetToolVersion>
</PropertyGroup>
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
</PropertyGroup>
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<Import Project="C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.0.0\build\netcoreapp2.0\Microsoft.NETCore.App.props" Condition="Exists('C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.0.0\build\netcoreapp2.0\Microsoft.NETCore.App.props')" />
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
</PropertyGroup>
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<Import Project="C:\Program Files\dotnet\sdk\NuGetFallbackFolder\netstandard.library\2.0.0\build\netstandard2.0\NETStandard.Library.targets" Condition="Exists('C:\Program Files\dotnet\sdk\NuGetFallbackFolder\netstandard.library\2.0.0\build\netstandard2.0\NETStandard.Library.targets')" />
<Import Project="C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.0.0\build\netcoreapp2.0\Microsoft.NETCore.App.targets" Condition="Exists('C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.0.0\build\netcoreapp2.0\Microsoft.NETCore.App.targets')" />
</ImportGroup>
</Project>
\ No newline at end of file
This diff could not be displayed because it is too large.
using Google.Cloud.Bigtable.V2;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
namespace Google.Cloud.Bigtable.ScanTest
{
class scanHelper
{
private AppSettings _appSettings;
private V2.TableName _table;
private BigtableClient _client;
public scanHelper(V2.TableName tbl)
{
_appSettings = Program.AppSetting;
_table = tbl;
_client = BigtableClient.Create();
}
public async Task scan(int batchLimit)
{
Stopwatch _startTime = Stopwatch.StartNew();
long _responseTime = 0;
Stopwatch _startRead = Stopwatch.StartNew();
try
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Table {_appSettings.TableName} read starting for batchsize {batchLimit}.");
long _scannedRecordInBatch = 0;
while (Program.ScanTime < _appSettings.ScanInterval)
{
_scannedRecordInBatch = 0;
ReadRowsRequest _readRequest = getReadRequest(batchLimit);
_startRead.Restart();
var response = _client.ReadRows(_readRequest);
++Program.ScanCount;
//response.GrpcCall.ResponseHeadersAsync.Wait();
while (await response.ResponseStream.MoveNext(default))
{
var current = response.ResponseStream.Current;
for (int index = 0; index < current.Chunks.Count;)
{
++Program.RecordScanned;
++_scannedRecordInBatch;
++index;
while (current.Chunks[index].RowKey.Length == 0)
{
++index;
if (index >= current.Chunks.Count)
break;
}
}
}
_startRead.Stop();
_responseTime = _startRead.ElapsedMilliseconds;
long _currentScanThroughput = _scannedRecordInBatch / _responseTime ;
histogramHelper.ThroughputHistogram.RecordValue(_currentScanThroughput);
histogramHelper.ScanResponseHistogram.RecordValue(_startRead.ElapsedMilliseconds);
#if DEBUG
//Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Recently scanned {_scannedRecordInBatch} in {_responseTime} milliseconds.");
#endif
Program.ThroughputTime += _responseTime;
}
}
catch (Exception ex)
{
_startTime.Stop();
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Exception during scan. Performed {Program.ScanCount} scans consuming {Program.RecordScanned} rows in {_startTime.ElapsedMilliseconds} milliseconds., error message {ex.Message}");
}
_startTime.Stop();
Program.ThroughputTime = _startTime.ElapsedMilliseconds;
}
private ReadRowsRequest getReadRequest(int limit)
{
Random rand = new Random();
int _startPosition = rand.Next(0, (int)_appSettings.Records);
StringBuilder sbStartRowKey = new StringBuilder(_appSettings.RowKeyPrefix + _startPosition.ToString("D" + _appSettings.RowKeySize));
//StringBuilder sbEndRowKey = new StringBuilder(_appSettings.RowKeyPrefix + (_startPosition + _appSettings.ScanLimit - 1).ToString("D" + _appSettings.RowKeySize));
Google.Protobuf.ByteString startRowKey = Google.Protobuf.ByteString.CopyFromUtf8(sbStartRowKey.ToString());
// Google.Protobuf.ByteString endRowKey = Google.Protobuf.ByteString.CopyFromUtf8(sbEndRowKey.ToString());
RowFilter _rowFilter = new RowFilter();
_rowFilter.FamilyNameRegexFilter = _appSettings.ColumnFamily;
_rowFilter.CellsPerColumnLimitFilter = 1;
List<RowRange> _rowRanges = new List<RowRange>();
_rowRanges.Add(new RowRange() { StartKeyClosed = startRowKey }); //, EndKeyClosed = endRowKey
ReadRowsRequest _readRequest = new ReadRowsRequest()
{
TableNameAsTableName = _table
,
Rows = new RowSet { RowRanges = { _rowRanges[0] } }
};
_readRequest.RowsLimit = limit;
_readRequest.Filter = _rowFilter;
return _readRequest;
}
}
}
using Google.Cloud.Bigtable.Admin.V2;
using System;
using System.Collections.Generic;
using System.Text;
namespace Google.Cloud.Bigtable.ScanTest
{
class tableHelper
{
private AppSettings _appSettings { get; set; }
private BigtableTableAdminClient _adminClient;
private V2.TableName _table;
public tableHelper()
{
this._appSettings = Program.AppSetting;
_adminClient = BigtableTableAdminClient.Create();
_appSettings.TableName = "perf123456";
dropTable();
}
public V2.TableName getTable()
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Checking existance of {_appSettings.TableName}");
bool _isTableExists = checkTable();
if (!_isTableExists)
{
while (!_isTableExists)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Creating table {_appSettings.TableName}.");
createTable();
_isTableExists = checkTable();
};
}
else
{
_table = new Google.Cloud.Bigtable.V2.TableName(_appSettings.ProjectId, _appSettings.InstanceId, _appSettings.TableName);
}
return _table;
}
private bool checkTable()
{
DateTime startTime = DateTime.Now;
try
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Checking table {_appSettings.TableName}.");
var response = _adminClient.GetTable(new Admin.V2.TableName(_appSettings.ProjectId, _appSettings.InstanceId, _appSettings.TableName), null);
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Table {_appSettings.TableName} exits.");
}
catch (Exception ex)
{
if (ex.Message.ToLower().Contains("table not found"))
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Table {_appSettings.TableName} not found.");
return false;
}
else
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Exception while checking table {_appSettings.TableName}. Error message: {ex.Message}.");
checkTable();
}
}
return true;
}
private void createTable()
{
try
{
var response = _adminClient.CreateTable(new InstanceName(_appSettings.ProjectId, _appSettings.InstanceId),
_appSettings.TableName,
new Table
{
Granularity = Table.Types.TimestampGranularity.Millis,
ColumnFamilies =
{
{ _appSettings.ColumnFamily, new ColumnFamily { GcRule = new GcRule { MaxNumVersions = 3 } } }
}
});
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Table created {_appSettings.TableName} successfully.");
_table = new Google.Cloud.Bigtable.V2.TableName(_appSettings.ProjectId, _appSettings.InstanceId, _appSettings.TableName);
new loadDataHelper(_table).loadTable();
}
catch (Exception ex)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Exception while creating table {_appSettings.TableName}. Error message: {ex.Message}.");
}
}
private void dropTable()
{
DateTime startTime = DateTime.Now;
try
{
_adminClient.DeleteTable(
new Admin.V2.TableName(_appSettings.ProjectId, _appSettings.InstanceId, _appSettings.TableName),
null);
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Table {_appSettings.TableName} deleted successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Exception while deleting table {_appSettings.TableName}, error message: {ex.Message}");
}
}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!