Skip to content

Commit

Permalink
fix: choose closest region did not returned valid region
Browse files Browse the repository at this point in the history
feat: new method returning latency for all available regions
  • Loading branch information
wofwof committed Jun 22, 2023
1 parent 64eb583 commit 4db965f
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 57 deletions.
8 changes: 6 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ default:
.unity_before_script: &unity_before_script
before_script:
- wget -O /usr/local/bin/yq 'https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64' && chmod +x /usr/local/bin/yq
- apt-get update && apt-get install -y --no-install-recommends xsltproc
- ./.scripts/ci/before_script.sh

.ubuntu_before_script: &ubuntu_before_script
before_script:
- apt update && apt install -y git
- apt-get update && apt-get install -y --no-install-recommends git

.bash_before_script: &bash_before_script
before_script:
Expand Down Expand Up @@ -152,7 +153,10 @@ get-activation-file:
artifacts:
when: always
expire_in: 2 weeks
coverage: /<Linecoverage>(.*?)</Linecoverage>/
paths:
- "$TEST_PLATFORM-results.xml"
reports:
junit: "$TEST_PLATFORM-results.xml"

test:playmode:
<<: *test
Expand Down
19 changes: 19 additions & 0 deletions .scripts/ci/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2016 Paul Hicks

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
71 changes: 71 additions & 0 deletions .scripts/ci/nunit3-junit.xslt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/test-run">
<testsuites tests="{@testcasecount}" failures="{@failed}" disabled="{@skipped}" time="{@duration}">
<xsl:apply-templates/>
</testsuites>
</xsl:template>

<xsl:template match="test-suite">
<xsl:if test="test-case">
<testsuite tests="{@testcasecount}" time="{@duration}" errors="{@testcasecount - @passed - @skipped - @failed}" failures="{@failed}" skipped="{@skipped}" timestamp="{@start-time}">
<xsl:attribute name="name">
<xsl:for-each select="ancestor-or-self::test-suite[@type='TestSuite']/@name">
<xsl:value-of select="concat(., '.')"/>
</xsl:for-each>
</xsl:attribute>
<xsl:apply-templates select="test-case"/>
</testsuite>
<xsl:apply-templates select="test-suite"/>
</xsl:if>
<xsl:if test="not(test-case)">
<xsl:apply-templates/>
</xsl:if>
</xsl:template>

<xsl:template match="test-case">
<testcase name="{@name}" assertions="{@asserts}" time="{@duration}" status="{@result}" classname="{@classname}">
<xsl:if test="@runstate = 'Skipped' or @runstate = 'Ignored'">
<skipped/>
</xsl:if>

<xsl:apply-templates/>
</testcase>
</xsl:template>

<xsl:template match="command-line"/>
<xsl:template match="settings"/>
<xsl:template match="filter"/>

<xsl:template match="output">
<system-out>
<xsl:value-of select="."/>
</system-out>
</xsl:template>

<xsl:template match="stack-trace">
</xsl:template>

<xsl:template match="test-case/failure">
<failure message="{./message}">
<xsl:value-of select="./stack-trace"/>
</failure>
</xsl:template>

<xsl:template match="test-suite/failure"/>

<xsl:template match="test-case/reason">
<xsl:if test="./message != null">
<skipped message="{./message}"/>
</xsl:if>
</xsl:template>

<xsl:template match="test-case/assertions">
</xsl:template>

<xsl:template match="test-suite/reason"/>

<xsl:template match="properties"/>
</xsl:stylesheet>
55 changes: 22 additions & 33 deletions .scripts/ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,42 @@ set -x

echo "Testing for $TEST_PLATFORM, Unit Type: $TESTING_TYPE"

CODE_COVERAGE_PACKAGE="com.unity.testtools.codecoverage"
PACKAGE_MANIFEST_PATH="Packages/manifest.json"
RAW_TEST_RESULTS_PATH="$UNITY_DIR/$TEST_PLATFORM-results.xml"
JUNIT_XSLT_PATH="$CI_PROJECT_DIR/.scripts/ci/nunit3-junit.xslt"
TEST_RESULTS_PATH="$CI_PROJECT_DIR/$TEST_PLATFORM-results.xml"

${UNITY_EXECUTABLE:-xvfb-run --auto-servernum --server-args='-screen 0 640x480x24' unity-editor} \
-projectPath $UNITY_DIR \
-projectPath "$UNITY_DIR" \
-runTests \
-testPlatform $TEST_PLATFORM \
-testResults $UNITY_DIR/$TEST_PLATFORM-results.xml \
-testPlatform "$TEST_PLATFORM" \
-testResults "$RAW_TEST_RESULTS_PATH" \
-logFile /dev/stdout \
-batchmode \
-nographics \
-enableCodeCoverage \
-coverageResultsPath $UNITY_DIR/$TEST_PLATFORM-coverage \
-coverageOptions "generateAdditionalMetrics;generateHtmlReport;generateHtmlReportHistory;generateBadgeReport;" \
-debugCodeOptimization

UNITY_EXIT_CODE=$?

function process_test_results {
if [ "$TESTING_TYPE" == 'JUNIT' ]; then
echo "Converting results to JUNit for analysis"
xsltproc -o "$TEST_RESULTS_PATH" "$JUNIT_XSLT_PATH" "$RAW_TEST_RESULTS_PATH"
else
echo "Not converting results to JUNit"
cp "$RAW_TEST_RESULTS_PATH" "$TEST_RESULTS_PATH"
fi
}

if [ $UNITY_EXIT_CODE -eq 0 ]; then
echo "Run succeeded, no failures occurred";
echo "Run succeeded, no failures occurred"
process_test_results
elif [ $UNITY_EXIT_CODE -eq 2 ]; then
echo "Run succeeded, some tests failed";
if [ $TESTING_TYPE == 'JUNIT' ]; then
echo "Converting results to JUNit for analysis";
saxonb-xslt -s $UNITY_DIR/$TEST_PLATFORM-results.xml -xsl $CI_PROJECT_DIR/ci/nunit-transforms/nunit3-junit.xslt >$UNITY_DIR/$TEST_PLATFORM-junit-results.xml
fi
echo "Run succeeded, some tests failed"
process_test_results
elif [ $UNITY_EXIT_CODE -eq 3 ]; then
echo "Run failure (other failure)";
if [ $TESTING_TYPE == 'JUNIT' ]; then
echo "Not converting results to JUNit";
fi
else
echo "Unexpected exit code $UNITY_EXIT_CODE";
if [ $TESTING_TYPE == 'JUNIT' ]; then
echo "Not converting results to JUNit";
fi
fi

if grep $CODE_COVERAGE_PACKAGE $PACKAGE_MANIFEST_PATH; then
cat $UNITY_DIR/$TEST_PLATFORM-coverage/Report/Summary.xml | grep Linecoverage
mv $UNITY_DIR/$TEST_PLATFORM-coverage/$CI_PROJECT_NAME-opencov/*Mode/TestCoverageResults_*.xml $UNITY_DIR/$TEST_PLATFORM-coverage/coverage.xml
rm -r $UNITY_DIR/$TEST_PLATFORM-coverage/$CI_PROJECT_NAME-opencov/
echo "Run failure (other failure)"
else
{
echo -e "\033[33mCode Coverage package not found in $PACKAGE_MANIFEST_PATH. Please install the package \"Code Coverage\" through Unity's Package Manager to enable coverage reports.\033[0m"
} 2> /dev/null
echo "Unexpected exit code $UNITY_EXIT_CODE"
fi

cat $UNITY_DIR/$TEST_PLATFORM-results.xml | grep test-run | grep Passed
exit $UNITY_EXIT_CODE
47 changes: 25 additions & 22 deletions Runtime/Util/ElympicsCloudPing.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Cysharp.Threading.Tasks;
using UnityEngine.Networking;

Expand All @@ -11,33 +12,47 @@ public static class ElympicsCloudPing
private const int IterationNumber = 3;
private const int TimeOut = 2;
private const string PingRoute = "api/ping";
private static readonly ISet<string> DistinctRegions = new HashSet<string>();
private static readonly Dictionary<string, LatencyData> RegionLatencyData = new Dictionary<string, LatencyData>();

public static async UniTask<(string Region, float LatencyMs)> ChooseClosestRegion(IList<string> regions)
{
var closestRegion = string.Empty;
var minLatencyMs = double.MaxValue;
foreach (var (region, latencyMs) in await GetLatencyDataForRegions(regions))
if (latencyMs.TotalMilliseconds < minLatencyMs)
{
minLatencyMs = latencyMs.TotalMilliseconds;
closestRegion = region;
}

return (Region: closestRegion, LatencyMs: (float)minLatencyMs);
}

public static async UniTask<Dictionary<string, TimeSpan>> GetLatencyDataForRegions(IList<string> regions)
{
if (regions == null)
throw new ArgumentNullException(nameof(regions));

if (regions.Count == 0)
throw new ArgumentException("Regions list cannot be empty.", nameof(regions));

DistinctRegions.Clear();
var distinctRegions = new HashSet<string>();

foreach (var region in regions)
{
if (!ElympicsRegionToGCRegionMapper.ElympicsRegionToGCRegionPingUrl.ContainsKey(region))
throw new ArgumentException($"Could not find Google Cloud url for region {region}", nameof(regions));
DistinctRegions.Add(region);
distinctRegions.Add(region);
}

var pingsTasks = new List<UniTask<PingResults>>();

foreach (var region in DistinctRegions)
var results = new List<PingResults>();
foreach (var region in distinctRegions)
for (var i = 0; i < IterationNumber; i++)
pingsTasks.Add(GetPingResult(region));
{
var result = await GetPingResult(region);
results.Add(result);
}

var results = await UniTask.WhenAll(pingsTasks);
RegionLatencyData.Clear();
foreach (var pingResult in results)
{
Expand All @@ -53,16 +68,7 @@ public static async UniTask<(string Region, float LatencyMs)> ChooseClosestRegio
if (RegionLatencyData.Count == 0)
throw new ElympicsException("Network error");

var closestRegion = string.Empty;
var minLatencyMs = double.MaxValue;
foreach (var latencyData in RegionLatencyData)
if (latencyData.Value.LatencyMedian < minLatencyMs)
{
minLatencyMs = latencyData.Value.LatencyMedian;
closestRegion = latencyData.Key;
}

return (Region: closestRegion, LatencyMs: (float)minLatencyMs);
return RegionLatencyData.ToDictionary(entry => entry.Key, entry => TimeSpan.FromMilliseconds(entry.Value.LatencyMedian));
}

private struct PingResults
Expand All @@ -82,10 +88,7 @@ public PingResults(string regionName, double latencyMs, bool isValid)
private static async UniTask<PingResults> GetPingResult(string region)
{
var url = ElympicsRegionToGCRegionMapper.ElympicsRegionToGCRegionPingUrl[region];
var uriBuilder = new UriBuilder(url)
{
Path = PingRoute
};
var uriBuilder = new UriBuilder(url) { Path = PingRoute };
var stopwatch = new Stopwatch();
var webRequest = UnityWebRequest.Get(uriBuilder.Uri);
webRequest.timeout = TimeOut;
Expand Down
8 changes: 8 additions & 0 deletions Tests/Runtime/CustomTestYieldInstructions.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4db965f

Please sign in to comment.