Skip to content

Commit 7917bb5

Browse files
committed
Added K8s example
1 parent 91ebad3 commit 7917bb5

File tree

9 files changed

+244
-8
lines changed

9 files changed

+244
-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/run-test-job.yaml

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

0 commit comments

Comments
 (0)