Skip to content

Commit 1626e74

Browse files
committed
Added K8s example
1 parent 91ebad3 commit 1626e74

File tree

9 files changed

+250
-8
lines changed

9 files changed

+250
-8
lines changed

.dockerignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**/.dockerignore
2+
**/.env
3+
**/.git
4+
**/.gitignore
5+
**/.project
6+
**/.settings
7+
**/.toolstarget
8+
**/.vs
9+
**/.vscode
10+
**/.idea
11+
**/*.*proj.user
12+
**/*.dbmdl
13+
**/*.jfm
14+
**/azds.yaml
15+
**/bin
16+
**/charts
17+
**/docker-compose*
18+
**/Dockerfile*
19+
**/node_modules
20+
**/npm-debug.log
21+
**/obj
22+
**/secrets.dev.yaml
23+
**/values.dev.yaml
24+
LICENSE
25+
README.md

NBomber.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GrpcClient", "examples\Grpc
2323
EndProject
2424
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BookstoreSimulator", "examples\BookstoreSimulator\BookstoreSimulator.csproj", "{BB8FF9AE-4D47-47E7-9B22-F3ABDFDE28D1}"
2525
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "K8sDemo", "examples\K8sDemo\K8sDemo.csproj", "{3B4FADDF-BE99-408D-98F9-551AEEC25ADB}"
27+
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2830
Debug|Any CPU = Debug|Any CPU
@@ -129,6 +131,18 @@ Global
129131
{BB8FF9AE-4D47-47E7-9B22-F3ABDFDE28D1}.Release|x64.Build.0 = Release|Any CPU
130132
{BB8FF9AE-4D47-47E7-9B22-F3ABDFDE28D1}.Release|x86.ActiveCfg = Release|Any CPU
131133
{BB8FF9AE-4D47-47E7-9B22-F3ABDFDE28D1}.Release|x86.Build.0 = Release|Any CPU
134+
{3B4FADDF-BE99-408D-98F9-551AEEC25ADB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
135+
{3B4FADDF-BE99-408D-98F9-551AEEC25ADB}.Debug|Any CPU.Build.0 = Debug|Any CPU
136+
{3B4FADDF-BE99-408D-98F9-551AEEC25ADB}.Debug|x64.ActiveCfg = Debug|Any CPU
137+
{3B4FADDF-BE99-408D-98F9-551AEEC25ADB}.Debug|x64.Build.0 = Debug|Any CPU
138+
{3B4FADDF-BE99-408D-98F9-551AEEC25ADB}.Debug|x86.ActiveCfg = Debug|Any CPU
139+
{3B4FADDF-BE99-408D-98F9-551AEEC25ADB}.Debug|x86.Build.0 = Debug|Any CPU
140+
{3B4FADDF-BE99-408D-98F9-551AEEC25ADB}.Release|Any CPU.ActiveCfg = Release|Any CPU
141+
{3B4FADDF-BE99-408D-98F9-551AEEC25ADB}.Release|Any CPU.Build.0 = Release|Any CPU
142+
{3B4FADDF-BE99-408D-98F9-551AEEC25ADB}.Release|x64.ActiveCfg = Release|Any CPU
143+
{3B4FADDF-BE99-408D-98F9-551AEEC25ADB}.Release|x64.Build.0 = Release|Any CPU
144+
{3B4FADDF-BE99-408D-98F9-551AEEC25ADB}.Release|x86.ActiveCfg = Release|Any CPU
145+
{3B4FADDF-BE99-408D-98F9-551AEEC25ADB}.Release|x86.Build.0 = Release|Any CPU
132146
EndGlobalSection
133147
GlobalSection(SolutionProperties) = preSolution
134148
HideSolutionNode = FALSE
@@ -142,6 +156,7 @@ Global
142156
{D4E6343A-1042-CDB6-308D-6C1662607E77} = {B20C9B19-52F5-4AF1-B41D-D01294BEC6C9}
143157
{EBBD9F26-59E7-0C55-0059-93AD3EB3469A} = {B20C9B19-52F5-4AF1-B41D-D01294BEC6C9}
144158
{BB8FF9AE-4D47-47E7-9B22-F3ABDFDE28D1} = {B20C9B19-52F5-4AF1-B41D-D01294BEC6C9}
159+
{3B4FADDF-BE99-408D-98F9-551AEEC25ADB} = {10398A75-959D-4271-913A-BE99E4ED5744}
145160
EndGlobalSection
146161
GlobalSection(ExtensibilityGlobals) = postSolution
147162
SolutionGuid = {C7E9329B-84FE-436E-8A44-26D8176C5C6F}

examples/Demo/Features/RealtimeReporting/TimescaleDB/TimescaleDBReportingExample.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,64 @@ public void Run()
1212
{
1313
var step1 = await Step.Run("login", context, async () =>
1414
{
15-
await Task.Delay(500);
15+
await Task.Delay(Random.Shared.Next(100, 500));
1616
return Response.Ok(sizeBytes: 10, statusCode: "200");
1717
});
1818

1919
var step2 = await Step.Run("get_product", context, async () =>
2020
{
21-
await Task.Delay(1000);
21+
await Task.Delay(Random.Shared.Next(500, 1000));
2222
return Response.Ok(sizeBytes: 20, statusCode: "200");
2323
});
2424

2525
var step3 = await Step.Run("buy_product", context, async () =>
2626
{
27-
await Task.Delay(2000);
28-
return Response.Ok(sizeBytes: 30, statusCode: "200");
27+
await Task.Delay(Random.Shared.Next(1000, 2000));
28+
return Response.Fail(sizeBytes: 30, statusCode: "200");
2929
});
3030

3131
return Response.Ok(statusCode: "201");
3232
})
33+
.WithMaxFailCount(Int32.MaxValue)
3334
.WithWarmUpDuration(TimeSpan.FromSeconds(3))
3435
.WithLoadSimulations(
3536
Simulation.RampingInject(rate: 200, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromMinutes(1)), // rump-up to rate 200
3637
Simulation.Inject(rate: 200, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromSeconds(30)), // keep injecting with rate 200
3738
Simulation.RampingInject(rate: 0, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromMinutes(1)) // rump-down to rate 0
3839
);
3940

41+
var scenario2 = Scenario.Create("user_flow_scenario2", async context =>
42+
{
43+
var step1 = await Step.Run("login", context, async () =>
44+
{
45+
await Task.Delay(Random.Shared.Next(100, 500));
46+
return Response.Ok(sizeBytes: 10, statusCode: "200");
47+
});
48+
49+
var step2 = await Step.Run("get_product", context, async () =>
50+
{
51+
await Task.Delay(Random.Shared.Next(500, 1000));
52+
return Response.Ok(sizeBytes: 20, statusCode: "200");
53+
});
54+
55+
var step3 = await Step.Run("buy_product", context, async () =>
56+
{
57+
await Task.Delay(Random.Shared.Next(1000, 2000));
58+
return Response.Fail(sizeBytes: 30, statusCode: "200");
59+
});
60+
61+
return Response.Ok(statusCode: "201");
62+
})
63+
.WithMaxFailCount(Int32.MaxValue)
64+
.WithWarmUpDuration(TimeSpan.FromSeconds(3))
65+
.WithLoadSimulations(
66+
Simulation.RampingInject(rate: 200, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromMinutes(1)), // rump-up to rate 200
67+
Simulation.Inject(rate: 200, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromSeconds(30)), // keep injecting with rate 200
68+
Simulation.RampingInject(rate: 0, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromMinutes(1)) // rump-down to rate 0
69+
);
70+
4071
NBomberRunner
41-
.RegisterScenarios(scenario)
72+
.RegisterScenarios(scenario, scenario2)
4273
.LoadInfraConfig("Features/RealtimeReporting/TimescaleDB/infra-config.json")
4374
.WithReportingInterval(TimeSpan.FromSeconds(5))
4475
.WithReportingSinks(_timescaleDbSink)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"TimescaleDbSink":{
3-
"ConnectionString": "Host=localhost;Port=5432;Database=metricsdb;Username=timescaledb;Password=timescaledb;Pooling=true;Maximum Pool Size=300;"
3+
"ConnectionString": "Host=localhost;Port=5432;Database=nb_studio_db;Username=timescaledb;Password=timescaledb;Pooling=true;Maximum Pool Size=300;"
44
}
55
}

examples/Demo/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
// -------------------------------
4141
// ----- Hello World examples -----
4242
// -------------------------------
43-
new HelloWorldExample().Run();
43+
// new HelloWorldExample().Run();
4444
// new ScenarioWithInit().Run();
4545
// new ScenarioWithSteps().Run();
4646
// new StepsShareData().Run();
@@ -75,7 +75,7 @@
7575

7676
// ---- Real-time reporting ----
7777
// new InfluxDBReportingExample().Run();
78-
// new TimescaleDBReportingExample().Run();
78+
new TimescaleDBReportingExample().Run();
7979
// new DatadogReportingExample().Run();
8080
// new CustomReportingExample().Run();
8181
// new NBomberStudioReportingExample().Run();

examples/K8sDemo/Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
2+
USER $APP_UID
3+
WORKDIR /app
4+
5+
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
6+
ARG BUILD_CONFIGURATION=Release
7+
WORKDIR /src
8+
COPY ["examples/K8sDemo/K8sDemo.csproj", "examples/K8sDemo/"]
9+
RUN dotnet restore "examples/K8sDemo/K8sDemo.csproj"
10+
COPY . .
11+
WORKDIR "/src/examples/K8sDemo"
12+
RUN dotnet build "./K8sDemo.csproj" -c $BUILD_CONFIGURATION -o /app/build
13+
14+
FROM build AS publish
15+
ARG BUILD_CONFIGURATION=Release
16+
RUN dotnet publish "./K8sDemo.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
17+
18+
FROM base AS final
19+
WORKDIR /app
20+
COPY --from=publish /app/publish .
21+
ENTRYPOINT ["dotnet", "K8sDemo.dll"]

examples/K8sDemo/K8sDemo.csproj

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
9+
<ServerGarbageCollection>false</ServerGarbageCollection>
10+
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<Content Include="..\..\.dockerignore">
15+
<Link>.dockerignore</Link>
16+
</Content>
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<PackageReference Include="NBomber" Version="6.1.1" />
21+
</ItemGroup>
22+
23+
</Project>

examples/K8sDemo/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using NBomber.CSharp;
2+
3+
var scenario = Scenario.Create("my_scenario", async context =>
4+
{
5+
await Task.Delay(500);
6+
7+
return Response.Ok();
8+
});
9+
10+
NBomberRunner
11+
.RegisterScenarios(scenario)
12+
.Run(args);

examples/K8sDemo/my-test-1.yaml

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: nb-config-my-test-1
5+
namespace: nbomber-tests
6+
data:
7+
nb-config.json: |
8+
{
9+
"TestSuite": "my test suite",
10+
"TestName": "my-test-1",
11+
"ClusterSettings": {
12+
13+
"AutoCluster": {
14+
15+
"ClusterId": "test_cluster",
16+
"NATSServerURL": "nats://nats-my-test-1",
17+
18+
"Coordinator": {
19+
"TargetScenarios": []
20+
},
21+
22+
"Agent": {
23+
"TargetScenarios": ["my_scenario"],
24+
"AgentsCount": 1
25+
}
26+
}
27+
},
28+
29+
"GlobalSettings": {
30+
31+
"ScenariosSettings": [
32+
{
33+
"ScenarioName": "my_scenario",
34+
"WarmUpDuration": "00:00:03",
35+
36+
"LoadSimulationsSettings": [
37+
{ "Inject": [10, "00:00:01", "00:00:20"] }
38+
]
39+
}
40+
],
41+
42+
"ReportFileName": "my_report_name",
43+
"ReportFolder": "my_reports"
44+
}
45+
}
46+
47+
---
48+
49+
apiVersion: batch/v1
50+
kind: Job
51+
metadata:
52+
name: k8sdemo-job-my-test-1
53+
namespace: nbomber-tests
54+
spec:
55+
completions: 2 # total number of pods that should run to completion
56+
parallelism: 2 # number of pods running at the same time
57+
template:
58+
spec:
59+
containers:
60+
- name: k8sdemo
61+
image: antyadev/k8sdemo:latest
62+
args: ["--config", "/config/nb-config.json", "--cluster-local-dev", "true"]
63+
# args: ["--config", "/config/nb-config.json", "--license", "YOUR_LICENSE_KEY"]
64+
volumeMounts:
65+
- name: nb-config-volume
66+
mountPath: /config
67+
readOnly: true
68+
69+
restartPolicy: Never
70+
71+
volumes:
72+
- name: nb-config-volume
73+
configMap:
74+
name: nb-config-my-test-1
75+
76+
---
77+
78+
apiVersion: apps/v1
79+
kind: Deployment
80+
metadata:
81+
namespace: nbomber-tests
82+
name: nats-my-test-1
83+
labels:
84+
app: nats
85+
spec:
86+
replicas: 1
87+
selector:
88+
matchLabels:
89+
app: nats
90+
template:
91+
metadata:
92+
labels:
93+
app: nats
94+
spec:
95+
containers:
96+
- name: nats
97+
image: nats:2.9.9
98+
args: [ "--js" ]
99+
ports:
100+
- containerPort: 4222
101+
---
102+
103+
apiVersion: v1
104+
kind: Service
105+
metadata:
106+
namespace: nbomber-tests
107+
name: nats-my-test-1
108+
labels:
109+
app: nats
110+
spec:
111+
selector:
112+
app: nats
113+
ports:
114+
- port: 4222
115+
type: ClusterIP

0 commit comments

Comments
 (0)