Skip to content

Commit

Permalink
optimizing code
Browse files Browse the repository at this point in the history
  • Loading branch information
nmalkapuram committed Dec 2, 2024
1 parent a722b95 commit 03f1b17
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace VirtualClient.Actions
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MathNet.Numerics;
using Microsoft.Extensions.DependencyInjection;
using VirtualClient;
using VirtualClient.Common;
Expand Down Expand Up @@ -98,7 +99,8 @@ await this.ServerApiClient.PollForExpectedStateAsync<CtsTrafficServerState>(
cancellationToken,
this.PollingInterval);

string targetIPAddress = this.GetServerIPAddress(cancellationToken);
ClientInstance instance = this.Layout.GetClientInstance(this.AgentId);
string targetIPAddress = (instance.Role == ClientRole.Server) ? "localhost" : this.GetServerIpAddress();

string ctsTrafficCommandArgs = $"-Target:{targetIPAddress} -Consoleverbosity:1 -StatusFilename:{this.StatusFileName} " +
$@"-ConnectionFilename:{this.ConnectionsFileName} -ErrorFileName:{this.ErrorFileName} -Port:{this.Port} " +
Expand Down Expand Up @@ -145,18 +147,5 @@ await this.ServerApiClient.PollForExpectedStateAsync<CtsTrafficServerState>(
}

}

private string GetServerIPAddress(CancellationToken cancellationToken)
{
string targetIPAddress = "localhost";

if (this.IsMultiRoleLayout())
{
ClientInstance serverInstance = this.GetLayoutClientInstances(ClientRole.Server).First();
targetIPAddress = serverInstance.IPAddress;
}

return targetIPAddress;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private async Task RunOLTPWorkloadAsync(EventContext telemetryContext, Cancellat
string script = $"{this.SysbenchPackagePath}/run-workload.py ";

using (IProcessProxy process = await this.ExecuteCommandAsync(
PythonCommand,
SysbenchClientExecutor.PythonCommand,
script + this.sysbenchExecutionArguments,
this.SysbenchPackagePath,
telemetryContext,
Expand Down Expand Up @@ -246,7 +246,7 @@ private async Task RunTPCCWorkloadAsync(EventContext telemetryContext, Cancellat
string script = $"{this.SysbenchPackagePath}/run-workload.py ";

using (IProcessProxy process = await this.ExecuteCommandAsync(
PythonCommand,
SysbenchClientExecutor.PythonCommand,
script + this.sysbenchExecutionArguments,
this.SysbenchPackagePath,
telemetryContext,
Expand Down Expand Up @@ -274,7 +274,7 @@ private async Task TruncateMySQLDatabaseAsync(EventContext telemetryContext, Can
}

using (IProcessProxy process = await this.ExecuteCommandAsync(
PythonCommand,
SysbenchClientExecutor.PythonCommand,
arguments,
Environment.CurrentDirectory,
telemetryContext,
Expand Down Expand Up @@ -303,11 +303,10 @@ private async Task PrepareOLTPMySQLDatabase(EventContext telemetryContext, Cance
this.sysbenchPrepareArguments += $" --host \"{serverIp}\"";
}

string command = $"python3";
string arguments = $"{this.SysbenchPackagePath}/populate-database.py ";

using (IProcessProxy process = await this.ExecuteCommandAsync(
command,
SysbenchClientExecutor.PythonCommand,
arguments + this.sysbenchPrepareArguments,
this.SysbenchPackagePath,
telemetryContext,
Expand All @@ -320,19 +319,5 @@ private async Task PrepareOLTPMySQLDatabase(EventContext telemetryContext, Cance
}
}
}

private string GetServerIpAddress()
{
string serverIPAddress = IPAddress.Loopback.ToString();

if (this.IsMultiRoleLayout())
{
ClientInstance serverInstance = this.GetLayoutClientInstances(ClientRole.Server).First();
IPAddress.TryParse(serverInstance.IPAddress, out IPAddress serverIP);
serverIPAddress = serverIP.ToString();
}

return serverIPAddress;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,5 @@ private async Task PrepareTPCCMySQLDatabase(EventContext telemetryContext, Cance
}
}
}

private string GetServerIpAddress()
{
string serverIPAddress = IPAddress.Loopback.ToString();

if (this.IsMultiRoleLayout())
{
ClientInstance serverInstance = this.GetLayoutClientInstances(ClientRole.Server).First();
IPAddress.TryParse(serverInstance.IPAddress, out IPAddress serverIP);
serverIPAddress = serverIP.ToString();
}

return serverIPAddress;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public string Action
{
this.Parameters.TryGetValue(nameof(this.Action), out IConvertible action);
return action?.ToString();
// return this.Parameters.GetValue<string>(nameof(this.Action), null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace VirtualClient.Contracts
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

/// <summary>
/// Extensions for <see cref="EnvironmentLayout"/> instances.
Expand Down Expand Up @@ -34,5 +35,23 @@ public static ClientInstance GetClientInstance(this EnvironmentLayout layout, st

return clientInstances?.FirstOrDefault();
}

/// <summary>
/// Returns first ServerIpAddress from the layout file in a client-server scenario.
/// </summary>
/// <param name="component">The component with the environment layout.</param>
/// <returns></returns>
public static string GetServerIpAddress(this VirtualClientComponent component)
{
string serverIPAddress = IPAddress.Loopback.ToString();

if (component.IsMultiRoleLayout())
{
ClientInstance serverInstance = component.GetLayoutClientInstances(ClientRole.Server).First();
serverIPAddress = serverInstance.IPAddress;
}

return serverIPAddress;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,20 +299,6 @@ private async Task<string> GetMySQLInMemoryCapacityAsync(CancellationToken cance
return bufferSizeInMegaBytes.ToString();
}

private string GetServerIpAddress()
{
string serverIPAddress = IPAddress.Loopback.ToString();

if (this.IsMultiRoleLayout())
{
ClientInstance serverInstance = this.GetLayoutClientInstances(ClientRole.Server).First();
IPAddress.TryParse(serverInstance.IPAddress, out IPAddress serverIP);
serverIPAddress = serverIP.ToString();
}

return serverIPAddress;
}

private string GetClientIpAddresses()
{
string clientIpAddresses = string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,6 @@ private async Task<string> GetPostgreSQLInnodbDirectoriesAsync(CancellationToken
return diskPaths;
}

private string GetServerIpAddress()
{
string serverIPAddress = IPAddress.Loopback.ToString();

if (this.IsMultiRoleLayout())
{
ClientInstance serverInstance = this.GetLayoutClientInstances(ClientRole.Server).First();
IPAddress.TryParse(serverInstance.IPAddress, out IPAddress serverIP);
serverIPAddress = serverIP.ToString();
}

return serverIPAddress;
}

/// <summary>
/// Supported PostgreSQL Server configuration actions.
/// </summary>
Expand Down

0 comments on commit 03f1b17

Please sign in to comment.