Skip to content

Commit 583d332

Browse files
authored
Merge pull request #72 from KBS-ASD/feature/save-result-azure-storage-blobs
Feature/save result azure storage blobs
2 parents 4e2613e + 8a28492 commit 583d332

File tree

19 files changed

+214
-90
lines changed

19 files changed

+214
-90
lines changed

src/KBS.Configuration/BaseConfiguration.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ public abstract class BaseConfiguration
1010
/// </summary>
1111
private static dynamic _commandLineArgumentConfiguration = JsonConvert.DeserializeObject("{}");
1212

13-
private static T GetFromAny<T>(string key)
13+
/// <summary>
14+
/// Tries to get the value matching given key from arguments first, then from environment.
15+
/// </summary>
16+
/// <typeparam name="T">
17+
/// </typeparam>
18+
/// <param name="key">
19+
/// </param>
20+
public static T GetFromAny<T>(string key)
1421
{
1522
var value = GetFromArguments<T>(key);
1623

@@ -21,6 +28,16 @@ private static T GetFromAny<T>(string key)
2128
return value;
2229
}
2330

31+
/// <summary>
32+
/// Tries to get the value matching given key from arguments first, then from environment.
33+
/// The fallback will be returned if the value is the default of specified type
34+
/// </summary>
35+
/// <typeparam name="T">
36+
/// </typeparam>
37+
/// <param name="key">
38+
/// </param>
39+
/// <param name="fallback">
40+
/// </param>
2441
public static T GetFromAny<T>(string key, T fallback)
2542
{
2643
var value = GetFromAny<T>(key);
@@ -35,8 +52,6 @@ public static T GetFromAny<T>(string key, T fallback)
3552
/// </typeparam>
3653
/// <param name="key">
3754
/// </param>
38-
/// <returns>
39-
/// </returns>
4055
public static T GetFromEnvironment<T>(string key)
4156
{
4257
var value = Environment.GetEnvironmentVariable(key);
@@ -57,8 +72,8 @@ public static T GetFromEnvironment<T>(string key)
5772
/// </param>
5873
/// <param name="fallback">
5974
/// </param>
60-
/// <returns>
61-
/// </returns>
75+
/// <param name="fallback">
76+
/// </param>
6277
public static T GetFromEnvironment<T>(string key, T fallback)
6378
{
6479
var value = GetFromArguments<T>(key);
@@ -73,8 +88,6 @@ public static T GetFromEnvironment<T>(string key, T fallback)
7388
/// </typeparam>
7489
/// <param name="key">
7590
/// </param>
76-
/// <returns>
77-
/// </returns>
7891
public static T GetFromArguments<T>(string key)
7992
{
8093
var value = _commandLineArgumentConfiguration[key];
@@ -89,31 +102,27 @@ public static T GetFromArguments<T>(string key)
89102
/// Gets value from command line arguments. The fallback will be returned if the value is the
90103
/// default of specified type type of T
91104
/// </summary>
92-
/// <typeparam name="T">
93-
/// </typeparam>
94105
/// <param name="key">
95106
/// </param>
96107
/// <param name="fallback">
97108
/// </param>
98-
/// <returns>
99-
/// </returns>
100109
public static T GetFromArguments<T>(string key, T fallback)
101110
{
102111
var value = GetFromArguments<T>(key);
103112

104-
return value.Equals(default(T)) ? fallback : value;
113+
if (value == null || value.Equals(default(T)))
114+
return fallback;
115+
116+
return value;
105117
}
106118

107119
/// <summary>
108120
/// Convert given json string to a dynamic object which can later be used to retreive
109121
/// configuration values. This value is memoized because it can take over 100ms to
110122
/// deserialize jsonString.
111123
/// </summary>
112-
/// ///
113124
/// <param name="jsonString">
114125
/// </param>
115-
/// <returns>
116-
/// </returns>
117126
public static void SetCommandLineArgsConfiguration(string jsonString)
118127
{
119128
if (jsonString == string.Empty)

src/KBS.Configuration/BenchmarkConfiguration.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,33 @@
22

33
namespace KBS.Configuration
44
{
5-
public static class BenchmarkConfiguration
5+
public class BenchmarkConfiguration : BaseConfiguration
66
{
7+
private static readonly string InitializedAt = DateTime.UtcNow.ToString("o");
8+
9+
/// <summary>
10+
/// Unique benchmark name
11+
/// </summary>
12+
public static string Name => GetFromArguments("Name", InitializedAt);
13+
714
/// <summary>
815
/// Amount of messages to send during the benchmark
916
/// </summary>
10-
public static int MessageCount => BaseConfiguration.GetFromArguments("MessageCount", 1000);
17+
public static int MessageCount => GetFromArguments("MessageCount", 1000);
1118

1219
/// <summary>
1320
/// Message size in bytes (a message will be filled with a byte array of the given size)
1421
/// </summary>
15-
public static int FillerSize => BaseConfiguration.GetFromArguments<int>("FillerSize");
22+
public static int FillerSize => GetFromArguments<int>("FillerSize");
1623

1724
/// <summary>
1825
/// Amounts of threads to use to send messages
1926
/// </summary>
20-
public static int ClientCount => BaseConfiguration.GetFromArguments("ClientCount", 2);
27+
public static int ClientCount => GetFromArguments("ClientCount", 2);
2128

2229
/// <summary>
2330
/// Time before test should abort after last message was sent
2431
/// </summary>
25-
public static TimeSpan Timeout => BaseConfiguration.GetFromArguments("Timeout", TimeSpan.FromMinutes(1));
32+
public static TimeSpan Timeout => GetFromArguments("Timeout", TimeSpan.FromMinutes(1));
2633
}
2734
}
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
namespace KBS.Configuration
22
{
3-
public static class ControllerConfiguration
3+
public class ControllerConfiguration : BaseConfiguration
44
{
5-
public static string KuduHost => BaseConfiguration.GetFromEnvironment<string>("KuduHost");
5+
public static string KuduHost => GetFromEnvironment<string>("KuduHost");
66

7-
public static string KuduUsername => BaseConfiguration.GetFromEnvironment<string>("KuduUsername");
7+
public static string KuduUsername => GetFromEnvironment<string>("KuduUsername");
88

9-
public static string KuduPassword => BaseConfiguration.GetFromEnvironment<string>("KuduPassword");
9+
public static string KuduPassword => GetFromEnvironment<string>("KuduPassword");
1010

11-
public static string WebJobName => BaseConfiguration.GetFromEnvironment<string>("WebJobName");
11+
public static string WebJobName => GetFromEnvironment<string>("WebJobName");
12+
13+
public static string StorageAccountConnectionString => GetFromEnvironment<string>("StorageAccountConnectionString");
14+
15+
public static string StorageAccountContainerName => GetFromEnvironment<string>("StorageAccountContainerName");
16+
17+
public static string StorageAccountName => GetFromEnvironment<string>("StorageAccountName");
1218
}
1319
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace KBS.Configuration
22
{
3-
public static class TelemetryClientConfiguration
3+
public class TelemetryClientConfiguration : BaseConfiguration
44
{
5-
public static readonly string AppInsightsInstrumentationKey = BaseConfiguration.GetFromArguments<string>("APPINSIGHTS_INSTRUMENTATIONKEY");
5+
public static readonly string AppInsightsInstrumentationKey = GetFromArguments<string>("AppInsightsInstrumentationKey");
66
}
77
}

src/KBS.Configuration/TestCaseConfiguration.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
namespace KBS.Configuration
44
{
5-
public static class TestCaseConfiguration
5+
public class TestCaseConfiguration : BaseConfiguration
66
{
77
/// <summary>
88
/// Value that is used to choose the test case that is used to run the benchmark
99
/// </summary>
10-
public static TestCaseType TestCaseType => BaseConfiguration.GetFromArguments("TestCaseType", TestCaseType.ConsumeConsumer);
10+
public static TestCaseType TestCaseType => GetFromArguments("TestCaseType", TestCaseType.ConsumeConsumer);
1111

1212
/// <summary>
1313
/// Value that is used to choose between the different BusControl transports
1414
/// </summary>
15-
public static TransportType TransportType => BaseConfiguration.GetFromArguments("TransportType", TransportType.InMemory);
15+
public static TransportType TransportType => GetFromArguments("TransportType", TransportType.InMemory);
1616

1717
/// <summary>
1818
/// Value that is used to choose the telemetry client that is used to track events
1919
/// </summary>
20-
public static TelemetryClientType TelemetryClientType => BaseConfiguration.GetFromEnvironment("TelemetryClientType", TelemetryClientType.InMemory);
20+
public static TelemetryClientType TelemetryClientType => GetFromEnvironment("TelemetryClientType", TelemetryClientType.InMemory);
2121
}
2222
}

src/KBS.Configuration/TransportConfiguration.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace KBS.Configuration
44
{
5-
public static class TransportConfiguration
5+
public class TransportConfiguration : BaseConfiguration
66
{
77
#region general
88

99
/// <summary>
1010
/// Variable used to indicate whether transport should run in "Express" or "Durable" mode
1111
/// </summary>
12-
public static string UseExpress => BaseConfiguration.GetFromArguments<string>("UseExpress");
12+
public static string UseExpress => GetFromArguments<string>("UseExpress");
1313

1414
#endregion general
1515

@@ -18,17 +18,17 @@ public static class TransportConfiguration
1818
/// <summary>
1919
/// Azure service bus location
2020
/// </summary>
21-
public static string AzureServiceBusUri => BaseConfiguration.GetFromEnvironment<string>("AzureServiceBusUri");
21+
public static string AzureServiceBusUri => GetFromEnvironment<string>("AzureServiceBusUri");
2222

2323
/// <summary>
2424
/// Value used to authenticate when using the azure service bus transport
2525
/// </summary>
26-
public static string AzureServiceBusToken => BaseConfiguration.GetFromEnvironment<string>("AzureServiceBusToken");
26+
public static string AzureServiceBusToken => GetFromEnvironment<string>("AzureServiceBusToken");
2727

2828
/// <summary>
2929
/// Value used to set the maximum duration of an operation when using the azure service bus transport
3030
/// </summary>
31-
public static TimeSpan AzureServiceBusOperationTimeout => BaseConfiguration.GetFromArguments("AzureServiceBusOperationTimeout", TimeSpan.FromSeconds(30));
31+
public static TimeSpan AzureServiceBusOperationTimeout => GetFromArguments("AzureServiceBusOperationTimeout", TimeSpan.FromSeconds(30));
3232

3333
#endregion azure service bus
3434

@@ -37,17 +37,17 @@ public static class TransportConfiguration
3737
/// <summary>
3838
/// RabbitMQ location
3939
/// </summary>
40-
public static string RabbitMqHost => BaseConfiguration.GetFromEnvironment<string>("RabbitMqHost");
40+
public static string RabbitMqHost => GetFromEnvironment<string>("RabbitMqHost");
4141

4242
/// <summary>
4343
/// RabbitMQ username that is used for authentication when using the RabbitMq transport
4444
/// </summary>
45-
public static string RabbitMqUsername => BaseConfiguration.GetFromEnvironment<string>("RabbitMqUsername");
45+
public static string RabbitMqUsername => GetFromEnvironment<string>("RabbitMqUsername");
4646

4747
/// <summary>
4848
/// RabbitMQ password that is used for authentication when using the RabbitMq transport
4949
/// </summary>
50-
public static string RabbitMqPassword => BaseConfiguration.GetFromEnvironment<string>("RabbitMqPassword");
50+
public static string RabbitMqPassword => GetFromEnvironment<string>("RabbitMqPassword");
5151

5252
#endregion rabbit mq
5353
}

src/KBS.ConfigurationTests/ConfigurationTests.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ public ConfigurationTests()
1111
Environment.SetEnvironmentVariable("myEnvVarString", "myRandomValue");
1212
Environment.SetEnvironmentVariable("myEnvVarInt", 54125.ToString());
1313

14-
BaseConfiguration.SetCommandLineArgsConfiguration("{myArg:\"A string\",myOtherArg:\"12345\"}");
14+
BaseConfiguration.SetCommandLineArgsConfiguration(
15+
"{myArg:\"A string\",myOtherArg:\"12345\",myNullArg:null}"
16+
);
1517
}
1618

1719
[Fact]
@@ -50,6 +52,16 @@ public void Should_ReturnConvertValueToCorrectType_When_GettingValueUsingCommand
5052
Assert.Equal(expectedValue, value);
5153
}
5254

55+
[Fact]
56+
public void Should_ReturnConvertValueToCorrectType_When_ArgumentIsNull()
57+
{
58+
const string expectedValue = "myFallBack";
59+
60+
var value = BaseConfiguration.GetFromArguments("myNullArg", expectedValue);
61+
62+
Assert.Equal(expectedValue, value);
63+
}
64+
5365
[Fact]
5466
public void Should_ReturnFallbackValueFromCommandLineArguments_When_ArgumentDoesNotExist()
5567
{

src/KBS.Controller/Controllers/TestController.cs renamed to src/KBS.Controller/Controllers/BenchmarkController.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
namespace KBS.Controller.Controllers
1212
{
13-
[Produces("application/json")]
1413
[Route("api/benchmark")]
14+
[Produces("application/json")]
1515
[ApiController]
16-
public class TestController : ControllerBase
16+
public class BenchmarkController : ControllerBase
1717
{
1818
private static readonly HttpClient KuduHttpClient;
1919

20-
static TestController()
20+
static BenchmarkController()
2121
{
2222
KuduHttpClient = new HttpClient
2323
{
@@ -73,17 +73,5 @@ public async Task<string> TriggerWebjob([FromBody] SimpleBenchmarkConfiguration
7373

7474
return await response.Content.ReadAsStringAsync();
7575
}
76-
77-
// DELETE api/test
78-
[HttpDelete]
79-
[ProducesResponseType(204)]
80-
public async Task<string> DeleteWebjob()
81-
{
82-
var response = await KuduHttpClient.DeleteAsync(
83-
$"triggeredwebjobs/{ControllerConfiguration.WebJobName}"
84-
);
85-
86-
return await response.Content.ReadAsStringAsync();
87-
}
8876
}
8977
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using KBS.Configuration;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.WindowsAzure.Storage;
7+
using Microsoft.WindowsAzure.Storage.Blob;
8+
9+
namespace KBS.Controller.Controllers
10+
{
11+
[Route("api/results")]
12+
[Produces("application/json")]
13+
[ApiController]
14+
public class ResultsController : ControllerBase
15+
{
16+
// GET api/test
17+
[HttpGet]
18+
[ProducesResponseType(302)]
19+
public async Task<List<string>> GetAll()
20+
{
21+
var account = CloudStorageAccount.Parse(ControllerConfiguration.StorageAccountConnectionString);
22+
var serviceClient = account.CreateCloudBlobClient();
23+
24+
var blobContainer = serviceClient.GetContainerReference(ControllerConfiguration.StorageAccountContainerName);
25+
26+
var results = new List<string>();
27+
BlobContinuationToken continuationToken = null;
28+
29+
do
30+
{
31+
var response = await blobContainer.ListBlobsSegmentedAsync(continuationToken);
32+
continuationToken = response.ContinuationToken;
33+
34+
var blobNames = response.Results
35+
.OfType<CloudBlockBlob>()
36+
.Select(b => b.Name)
37+
.ToList();
38+
39+
results.AddRange(blobNames);
40+
}
41+
while (continuationToken != null);
42+
43+
return results;
44+
}
45+
46+
[HttpGet]
47+
[Route("{fileName}")]
48+
[ProducesResponseType(200)]
49+
public RedirectResult Get(string fileName)
50+
{
51+
/*if (fileName == string.Empty)
52+
{
53+
throw new NullReferenceException("fileName");
54+
}*/
55+
56+
return new RedirectResult(
57+
$"https://{ControllerConfiguration.StorageAccountName}.blob.core.windows.net/{ControllerConfiguration.StorageAccountContainerName}/{fileName}"
58+
);
59+
}
60+
}
61+
}

src/KBS.Controller/Model/SimpleBenchmarkConfiguration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public class SimpleBenchmarkConfiguration
1111
/// <summary>
1212
/// Amount of messages to send during the benchmark
1313
/// </summary>
14+
public string Name { get; set; }
15+
16+
/// <summary>
17+
/// </summary>
1418
public int MessagesCount { get; set; }
1519

1620
/// <summary>

0 commit comments

Comments
 (0)