Skip to content

Commit

Permalink
Presented version
Browse files Browse the repository at this point in the history
  • Loading branch information
tkopacz committed Jun 9, 2024
1 parent 1a774cf commit 2fb93f8
Show file tree
Hide file tree
Showing 82 changed files with 4,442 additions and 1 deletion.
Binary file added 20240607 Parallel programming in 2024.pdf
Binary file not shown.
30 changes: 30 additions & 0 deletions Banchmarking101/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
!**/.gitignore
!.git/HEAD
!.git/config
!.git/packed-refs
!.git/refs/heads/**
47 changes: 47 additions & 0 deletions Banchmarking101/Banchmarking101.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.34916.146
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor-SimplestDemo", "Blazor-SimplestDemo\Blazor-SimplestDemo\Blazor-SimplestDemo.csproj", "{BF3C7250-D687-4574-AD89-3147D35805D0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor-SimplestDemo.Client", "Blazor-SimplestDemo\Blazor-SimplestDemo.Client\Blazor-SimplestDemo.Client.csproj", "{7E228F9F-0697-472F-8C09-36343BFB6ECD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoadTestWebAPI", "LoadTestWebAPI\LoadTestWebAPI.csproj", "{2C4BB6C6-11C1-4007-BC99-B5B217CB7376}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoadTestWebAPIAOT", "LoadTestWebAPIAOT\LoadTestWebAPIAOT.csproj", "{57604E96-FA76-49E6-B78E-8A8AFCA57C9A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0B5A9B50-CDE7-43E8-AB7B-4D0B1838F2E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0B5A9B50-CDE7-43E8-AB7B-4D0B1838F2E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B5A9B50-CDE7-43E8-AB7B-4D0B1838F2E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B5A9B50-CDE7-43E8-AB7B-4D0B1838F2E8}.Release|Any CPU.Build.0 = Release|Any CPU
{BF3C7250-D687-4574-AD89-3147D35805D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BF3C7250-D687-4574-AD89-3147D35805D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF3C7250-D687-4574-AD89-3147D35805D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF3C7250-D687-4574-AD89-3147D35805D0}.Release|Any CPU.Build.0 = Release|Any CPU
{7E228F9F-0697-472F-8C09-36343BFB6ECD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7E228F9F-0697-472F-8C09-36343BFB6ECD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E228F9F-0697-472F-8C09-36343BFB6ECD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E228F9F-0697-472F-8C09-36343BFB6ECD}.Release|Any CPU.Build.0 = Release|Any CPU
{2C4BB6C6-11C1-4007-BC99-B5B217CB7376}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C4BB6C6-11C1-4007-BC99-B5B217CB7376}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C4BB6C6-11C1-4007-BC99-B5B217CB7376}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C4BB6C6-11C1-4007-BC99-B5B217CB7376}.Release|Any CPU.Build.0 = Release|Any CPU
{57604E96-FA76-49E6-B78E-8A8AFCA57C9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{57604E96-FA76-49E6-B78E-8A8AFCA57C9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{57604E96-FA76-49E6-B78E-8A8AFCA57C9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{57604E96-FA76-49E6-B78E-8A8AFCA57C9A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {883B1C6B-722B-4BF3-8FD9-FCEE51DAE13B}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
<StaticWebAssetProjectMode>Default</StaticWebAssetProjectMode>
<RootNamespace>Blazor-SimplestDemo.Client</RootNamespace>
<AssemblyName>$(AssemblyName.Replace(' ', '_'))</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.5" />
</ItemGroup>

<ItemGroup>
<Content Update="Pages\RunClient.razor">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@page "/counter"
@rendermode InteractiveWebAssembly

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
@page "/run"
@using SimplestDemo
@rendermode InteractiveServer
<PageTitle>Run</PageTitle>

<h1>Run</h1>

<button disabled="@isRunning" class="btn btn-primary" @onclick="RunMe">Run!</button>
<p>
<code>
@result1
</code>
</p>
<p>
<code>
@result2
</code>
</p>
<p>
<code>
@result3
</code>
</p>
@code {
private bool isRunning = false;
private string result1 = string.Empty;
private string result2 = string.Empty;
private string result3 = string.Empty;
private async Task RunMe()
{
isRunning = true;
result1 = string.Empty;
result2 = string.Empty;
result3 = string.Empty;
StateHasChanged();
await Task.Run(async () =>
{
SimpleBenchmarkRun simpleBenchmarkRun = new("InitialDemo", 10);
RunStatistics runStatistics;
var data = Enumerable.Range(1, 5000000).Select(i => i /*+ Random.Shared.Next(100)*/);
runStatistics = simpleBenchmarkRun.Run(() =>
{
data.Where(i => i > 4000).Average();
}, "LINQ");
result1 = runStatistics.ToString();
await InvokeAsync(StateHasChanged);
runStatistics = simpleBenchmarkRun.Run(() =>
{
data.AsParallel().Where(i => i > 4000).Average();
}, "PLINQ");
result2 = runStatistics.ToString();
await InvokeAsync(StateHasChanged);
runStatistics = simpleBenchmarkRun.Run(() =>
{
data.AsParallel().WithDegreeOfParallelism(1).Where(i => i > 4000).Average();
}, "PLINQ(1)");
result3 = runStatistics.ToString();
});
isRunning = false;
StateHasChanged();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
@page "/runclient"
@using SimplestDemo
@rendermode InteractiveWebAssembly
<PageTitle>Run Client</PageTitle>

<h1>Run Client</h1>

<button disabled="@isRunning" class="btn btn-primary" @onclick="RunMe">Run!</button>
<p>
<code>
@result1
</code>
</p>
<p>
<code>
@result2
</code>
</p>
<p>
<code>
@result3
</code>
</p>
@code {
private bool isRunning = false;
private string result1 = string.Empty;
private string result2 = string.Empty;
private string result3 = string.Empty;
private async Task RunMe()
{
isRunning = true;
result1 = string.Empty;
result2 = string.Empty;
result3 = string.Empty;
StateHasChanged();
await Task.Run(async () =>
{
SimpleBenchmarkRun simpleBenchmarkRun = new("InitialDemo", 10);
RunStatistics runStatistics;
var data = Enumerable.Range(1, 50000).Select(i => i /*+ Random.Shared.Next(100)*/);
runStatistics = simpleBenchmarkRun.Run(() =>
{
data.Where(i => i > 4).Average();
}, "LINQ");
result1 = runStatistics.ToString();
await InvokeAsync(StateHasChanged);
runStatistics = simpleBenchmarkRun.Run(() =>
{
data.AsParallel().Where(i => i > 4).Average();
}, "PLINQ");
result2 = runStatistics.ToString();
await InvokeAsync(StateHasChanged);
runStatistics = simpleBenchmarkRun.Run(() =>
{
data.AsParallel().WithDegreeOfParallelism(1).Where(i => i > 4).Average();
}, "PLINQ(1)");
result3 = runStatistics.ToString();
});
isRunning = false;
StateHasChanged();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);

await builder.Build().RunAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System.Diagnostics;
using System.Text;

namespace SimplestDemo
{
public static class EnumerableExtensions
{
public static double Median(this IEnumerable<long> source)
{
if (source == null || !source.Any())
{
throw new ArgumentException("Cannot compute median for a null or empty set.");
}

var sortedList = source.OrderBy(num => num).ToList();
int itemIndex = sortedList.Count / 2;

if (sortedList.Count % 2 == 0)
{
// If even, average the two middle elements
return (sortedList[itemIndex] + sortedList[itemIndex - 1]) / 2;
}
else
{
// If odd, return the middle element
return sortedList[itemIndex];
}
}
}

public class RunStatistics
{
List<long> _runs = new List<long>();
private readonly string _title;
private readonly string _subtitle;

public RunStatistics(string title, string subtitle) { _title = title; _subtitle = subtitle; }
public void AddRun(long ms)
{
_runs.Add(ms);
}

public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"-=-= {_title} -=-=");
sb.AppendLine($"Runs:{_subtitle}");
//_runs.ForEach(a => sb.AppendLine(a.ToString()));
sb.AppendLine($"Cnt:{_runs.Count()}");
sb.AppendLine($"Average:{_runs.Average(a => a)}");
sb.AppendLine($"Min:{_runs.Min()}");
sb.AppendLine($"Max:{_runs.Max()}");
sb.AppendLine($"Median:{_runs.Median()}");
return sb.ToString();
}
}
public class SimpleBenchmarkRun
{
private readonly string _title;

/// <summary>
/// Number of iteration
/// </summary>
public int N { get; set; } = 4;
/// <summary>
/// Iterations to skip
/// </summary>
public int Skip { get; set; } = 1;

public SimpleBenchmarkRun(string title, int n = 4)
{
_title = title;
N = n;
}
public RunStatistics Run(Action torun, string subtitle = "")
{
RunStatistics runStatistics = new RunStatistics(_title, subtitle);
Stopwatch stopwatch = new Stopwatch();
long lastms = 0;
for (int i = 0; i < Skip; i++)
{
torun();
}
stopwatch.Start();
for (int i = 0; i < N; i++)
{
torun();
runStatistics.AddRun(stopwatch.ElapsedMilliseconds - lastms);
lastms = stopwatch.ElapsedMilliseconds;
}
stopwatch.Stop();
return runStatistics;
}
public async Task<RunStatistics> RunAsync(Func<Task> torun, string subtitle = "")
{
RunStatistics runStatistics = new RunStatistics(_title, subtitle);
Stopwatch stopwatch = new Stopwatch();
long lastms = 0;
for (int i = 0; i < Skip; i++)
{
await torun();
}
stopwatch.Start();
for (int i = 0; i < N; i++)
{
await torun();
runStatistics.AddRun(stopwatch.ElapsedMilliseconds - lastms);
lastms = stopwatch.ElapsedMilliseconds;
}
stopwatch.Stop();
return runStatistics;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using Blazor_SimplestDemo.Client
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Loading

0 comments on commit 2fb93f8

Please sign in to comment.