Skip to content

Commit

Permalink
updated load test
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Jun 10, 2023
1 parent 76459bb commit 6a9f2ef
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 24 deletions.
11 changes: 11 additions & 0 deletions src/Captcha.Net.Sample/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

# FROM docker.mofid.dev/mofidonline/dotnet/aspnet-webapi-libgdplus:6.0
FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app

Expand All @@ -15,6 +16,16 @@ RUN dotnet build "Captcha.Net.Sample.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Captcha.Net.Sample.csproj" -c Release -o /app/publish /p:UseAppHost=false

# Install the fontconfig package
RUN apt-get update && \
apt-get install -y fontconfig

# Copy the font files into the container
COPY --from=build /app/fonts/*.ttf /usr/share/fonts/truetype/

# Update the font cache
RUN fc-cache -f -v

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
Expand Down
25 changes: 15 additions & 10 deletions src/Captcha.Net.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
File.WriteAllBytes($"{currentDirectory.FullName}/captcha-{i}.jpg", result.CaptchaByteData);
}

Console.WriteLine($"Duration: Max({times.Max()}ms), Min({times.Min()}ms), Average({times.Average()}ms)");
for (int i = 1, q = 1; i < 10000; i += q)
var maxDuration = times.Max();
Console.WriteLine($"Duration: Max({maxDuration}ms), Min({times.Min()}ms), Average({times.Average()}ms)");
for (int i = 1, q = 1; i < maxDuration + q; i += q)
{
var timeCount = times.Count(t => t <= i && t > i - q);

Expand All @@ -38,13 +39,17 @@
q = 1000;
}

// open the directory after completion
if (OperatingSystem.IsWindows())
try
{
Process.Start("explorer.exe", currentDirectory.FullName);
}
else if (OperatingSystem.IsLinux())
{
// Start a new process to open the folder
Process.Start("xdg-open", currentDirectory.FullName);
// open the directory after completion
if (OperatingSystem.IsWindows())
{
Process.Start("explorer.exe", currentDirectory.FullName);
}
else if (OperatingSystem.IsLinux())
{
// Start a new process to open the folder
Process.Start("xdg-open", currentDirectory.FullName);
}
}
catch { }
44 changes: 44 additions & 0 deletions src/Captcha.Net/Captcha.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,44 @@
<Version>$(FileVersion)</Version>
</PropertyGroup>

<ItemGroup>
<None Remove="fonts\DejaVuSerif-Bold.ttf" />
<None Remove="fonts\DejaVuSerif-BoldItalic.ttf" />
<None Remove="fonts\DejaVuSerif-Italic.ttf" />
<None Remove="fonts\DejaVuSerif.ttf" />
<None Remove="fonts\DejaVuSerifCondensed-Bold.ttf" />
<None Remove="fonts\DejaVuSerifCondensed-BoldItalic.ttf" />
<None Remove="fonts\DejaVuSerifCondensed-Italic.ttf" />
<None Remove="fonts\DejaVuSerifCondensed.ttf" />
</ItemGroup>

<ItemGroup>
<Resource Include="fonts\DejaVuSerif-Bold.ttf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="fonts\DejaVuSerif-BoldItalic.ttf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="fonts\DejaVuSerif-Italic.ttf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="fonts\DejaVuSerif.ttf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="fonts\DejaVuSerifCondensed-Bold.ttf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="fonts\DejaVuSerifCondensed-BoldItalic.ttf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="fonts\DejaVuSerifCondensed-Italic.ttf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="fonts\DejaVuSerifCondensed.ttf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
Expand All @@ -38,4 +76,10 @@
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15" />
</ItemGroup>

<ItemGroup>
<None Update="fonts\DejaVu Fonts License.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
26 changes: 13 additions & 13 deletions src/Captcha.Net/Captcha.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Captcha.Net
Expand All @@ -27,18 +28,17 @@ public Captcha(CaptchaOptions options)
_options = options;
if (_font == null)
{
try
{
var fontName = _options.FontFamilies[0];
_font = SystemFonts.CreateFont(fontName, _options.FontSize, _options.FontStyle);
}
catch
{
if (_options.FontFamilies.Length > 1)
foreach (var name in _options.FontFamilies)
if (SystemFonts.Collection.TryGet(name, out var fontFamily))
{
var fontName = _options.FontFamilies[1];
_font = SystemFonts.CreateFont(fontName, _options.FontSize, _options.FontStyle);
_font = fontFamily.CreateFont(_options.FontSize, _options.FontStyle);
break;
}

if (_font == null)
{
var fontFamily = SystemFonts.Families.FirstOrDefault();
_font = fontFamily.CreateFont(_options.FontSize, _options.FontStyle);
}
}

Expand Down Expand Up @@ -115,7 +115,7 @@ protected void DrawNoiseLines(IImageProcessingContext ctx, int width, int height
int x0 = Rand.Next(0, Rand.Next(0, Math.Min(30, width)));
int y0 = Rand.Next(Math.Min(10, height), height);
int x1 = Rand.Next(width - Rand.Next(0, (int)(width * 0.25)), width);
int y1 = Rand.Next(0, height);
int y1 = Rand.Next(0, height);
var lineColor = _options.DrawLinesColor[Rand.Next(0, _options.DrawLinesColor.Length)];
ctx.DrawLines(lineColor, LineThickness.Value, new PointF[] { new PointF(x0, y0), new PointF(x1, y1) });
}
Expand All @@ -125,8 +125,8 @@ protected AffineTransformBuilder GetRotation()
var width = Rand.Next(Math.Min((ushort)10u, _options.Width), _options.Width);
var height = Rand.Next(Math.Min((ushort)10u, _options.Height), _options.Height);
var pointF = new PointF(width, height);
var rotationDegrees = _options.RotationDegree.HasValue && _options.RotationDegree <= _options.MaxRotationDegrees
? _options.RotationDegree.Value
var rotationDegrees = _options.RotationDegree.HasValue && _options.RotationDegree <= _options.MaxRotationDegrees
? _options.RotationDegree.Value
: Rand.Next(0, _options.MaxRotationDegrees);
var result = GetRotation(rotationDegrees, pointF);
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/Captcha.Net/CaptchaOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public CaptchaOptions()
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
FontFamilies = new string[] { "San Francisco", "Helvetica" };
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
FontFamilies = new string[] { "Arial", "Verdana", "Times New Roman" };
FontFamilies = new string[] { "Microsoft Sans Serif", "Arial", "Verdana", "Times New Roman" };
}
}
}
97 changes: 97 additions & 0 deletions src/Captcha.Net/fonts/DejaVu Fonts License.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
Fonts are (c) Bitstream (see below). DejaVu changes are in public domain.
Glyphs imported from Arev fonts are (c) Tavmjong Bah (see below)

Bitstream Vera Fonts Copyright
------------------------------

Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera is
a trademark of Bitstream, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of the fonts accompanying this license ("Fonts") and associated
documentation files (the "Font Software"), to reproduce and distribute the
Font Software, including without limitation the rights to use, copy, merge,
publish, distribute, and/or sell copies of the Font Software, and to permit
persons to whom the Font Software is furnished to do so, subject to the
following conditions:

The above copyright and trademark notices and this permission notice shall
be included in all copies of one or more of the Font Software typefaces.

The Font Software may be modified, altered, or added to, and in particular
the designs of glyphs or characters in the Fonts may be modified and
additional glyphs or characters may be added to the Fonts, only if the fonts
are renamed to names not containing either the words "Bitstream" or the word
"Vera".

This License becomes null and void to the extent applicable to Fonts or Font
Software that has been modified and is distributed under the "Bitstream
Vera" names.

The Font Software may be sold as part of a larger software package but no
copy of one or more of the Font Software typefaces may be sold by itself.

THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT,
TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME
FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING
ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE
FONT SOFTWARE.

Except as contained in this notice, the names of Gnome, the Gnome
Foundation, and Bitstream Inc., shall not be used in advertising or
otherwise to promote the sale, use or other dealings in this Font Software
without prior written authorization from the Gnome Foundation or Bitstream
Inc., respectively. For further information, contact: fonts at gnome dot
org.

Arev Fonts Copyright
------------------------------

Copyright (c) 2006 by Tavmjong Bah. All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining
a copy of the fonts accompanying this license ("Fonts") and
associated documentation files (the "Font Software"), to reproduce
and distribute the modifications to the Bitstream Vera Font Software,
including without limitation the rights to use, copy, merge, publish,
distribute, and/or sell copies of the Font Software, and to permit
persons to whom the Font Software is furnished to do so, subject to
the following conditions:

The above copyright and trademark notices and this permission notice
shall be included in all copies of one or more of the Font Software
typefaces.

The Font Software may be modified, altered, or added to, and in
particular the designs of glyphs or characters in the Fonts may be
modified and additional glyphs or characters may be added to the
Fonts, only if the fonts are renamed to names not containing either
the words "Tavmjong Bah" or the word "Arev".

This License becomes null and void to the extent applicable to Fonts
or Font Software that has been modified and is distributed under the
"Tavmjong Bah Arev" names.

The Font Software may be sold as part of a larger software package but
no copy of one or more of the Font Software typefaces may be sold by
itself.

THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL
TAVMJONG BAH BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

Except as contained in this notice, the name of Tavmjong Bah shall not
be used in advertising or otherwise to promote the sale, use or other
dealings in this Font Software without prior written authorization
from Tavmjong Bah. For further information, contact: tavmjong @ free
. fr.
Binary file added src/Captcha.Net/fonts/DejaVuSerif-Bold.ttf
Binary file not shown.
Binary file added src/Captcha.Net/fonts/DejaVuSerif-BoldItalic.ttf
Binary file not shown.
Binary file added src/Captcha.Net/fonts/DejaVuSerif-Italic.ttf
Binary file not shown.
Binary file added src/Captcha.Net/fonts/DejaVuSerif.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added src/Captcha.Net/fonts/DejaVuSerifCondensed.ttf
Binary file not shown.

0 comments on commit 6a9f2ef

Please sign in to comment.