mirror of
https://github.com/nsnail/dot.git
synced 2025-06-17 21:13:21 +08:00
<refactor> 性能优化
This commit is contained in:
parent
0aff7fc1eb
commit
c9a60c7458
@ -1,7 +1,6 @@
|
||||
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:s="clr-namespace:System;assembly=mscorlib"
|
||||
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xml:space="preserve">
|
||||
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve">
|
||||
<s:String x:Key="/Default/CodeStyle/CSharpFileLayoutPatterns/Pattern/@EntryValue"><?xml version="1.0" encoding="utf-16"?>
|
||||
<Patterns xmlns="urn:schemas-jetbrains-com:member-reordering-patterns">
|
||||
<TypePattern>
|
||||
@ -11,6 +10,7 @@
|
||||
<Kind.Order>
|
||||
<DeclarationKind>Interface</DeclarationKind>
|
||||
<DeclarationKind>Class</DeclarationKind>
|
||||
<DeclarationKind>Record</DeclarationKind>
|
||||
<DeclarationKind>Enum</DeclarationKind>
|
||||
<DeclarationKind>Struct</DeclarationKind>
|
||||
<DeclarationKind>Delegate</DeclarationKind>
|
||||
|
9
src/Lang/Str.Designer.cs
generated
9
src/Lang/Str.Designer.cs
generated
@ -275,6 +275,15 @@ namespace Dot.Lang {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to NTP 标准网络时钟: {0}.
|
||||
/// </summary>
|
||||
public static string ServerTime {
|
||||
get {
|
||||
return ResourceManager.GetString("ServerTime", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to 已读取:{0}/{1},处理:{2},跳过:{3}.
|
||||
/// </summary>
|
||||
|
@ -1,116 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
|
||||
id="root" xmlns="">
|
||||
<xsd:element name="root" msdata:IsDataSet="true"></xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>1.3</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
|
||||
PublicKeyToken=b77a5c561934e089
|
||||
</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
|
||||
PublicKeyToken=b77a5c561934e089
|
||||
</value>
|
||||
</resheader>
|
||||
<data name="InputTextIsEmpty" xml:space="preserve">
|
||||
<value>The input text is empty</value>
|
||||
</data>
|
||||
<data name="SearchingFile" xml:space="preserve">
|
||||
<value>Find files...</value>
|
||||
</data>
|
||||
<data name="PathNotFound" xml:space="preserve">
|
||||
<value>The specified path "{0}" does not exist</value>
|
||||
</data>
|
||||
<data name="SearchingFileOK" xml:space="preserve">
|
||||
<value>Find files...OK</value>
|
||||
</data>
|
||||
<data name="ShowMessageTemp" xml:space="preserve">
|
||||
<value>Read: {0}/{1}, processed: {2}, skipped: {3}</value>
|
||||
</data>
|
||||
<data name="HelpForText" xml:space="preserve">
|
||||
<value>Text encoding tool</value>
|
||||
</data>
|
||||
<data name="Copied" xml:space="preserve">
|
||||
<value>{0}(copied to clipboard)</value>
|
||||
</data>
|
||||
<data name="FileSearchPattern" xml:space="preserve">
|
||||
<value>File wildcards</value>
|
||||
</data>
|
||||
<data name="FolderPath" xml:space="preserve">
|
||||
<value>Directory path to be processed</value>
|
||||
</data>
|
||||
<data name="ConvertEndOfLineToLF" xml:space="preserve">
|
||||
<value>Convert newline characters to LF</value>
|
||||
</data>
|
||||
<data name="GuidTool" xml:space="preserve">
|
||||
<value>GUID tool</value>
|
||||
</data>
|
||||
<data name="UseUppercase" xml:space="preserve">
|
||||
<value>Use uppercase output</value>
|
||||
</data>
|
||||
<data name="RandomPasswordGenerator" xml:space="preserve">
|
||||
<value>Random password generator</value>
|
||||
</data>
|
||||
<data name="PwdLength" xml:space="preserve">
|
||||
<value>Password length</value>
|
||||
</data>
|
||||
<data name="PwdGenerateTypes" xml:space="preserve">
|
||||
<value>BitSet 1:[0-9],2:[a-z],4:[A-Z],8:[ascii.0x21-0x2F]</value>
|
||||
</data>
|
||||
<data name="RemoveTrailingWhiteSpaces" xml:space="preserve">
|
||||
<value>Remove line breaks and spaces at the end of the file</value>
|
||||
</data>
|
||||
<data name="TrimUtf8Bom" xml:space="preserve">
|
||||
<value>Remove the uf8 bom of the file</value>
|
||||
</data>
|
||||
<data name="TextTobeProcessed" xml:space="preserve">
|
||||
<value>Text to be processed (clipboard value is taken by default)</value>
|
||||
</data>
|
||||
<data name="PressAnyKey" xml:space="preserve">
|
||||
<value>Press any key to continue...</value>
|
||||
</data>
|
||||
<data name="ReadOnly" xml:space="preserve">
|
||||
<value>Read-only mode (only for testing, no actual modification)</value>
|
||||
</data>
|
||||
<data name="NoFileToBeProcessed" xml:space="preserve">
|
||||
<value>No documents to be processed</value>
|
||||
</data>
|
||||
<data name="TimeoutMillSecs" xml:space="preserve">
|
||||
<value>Timeout for connecting to the NTP server (milliseconds)</value>
|
||||
</data>
|
||||
<data name="SyncToLocalTime" xml:space="preserve">
|
||||
<value>Synchronize local time</value>
|
||||
</data>
|
||||
<data name="NtpReceiveDone" xml:space="preserve">
|
||||
<value>Success {0}/{1}, the average value of the clock offset of the machine:{2}ms</value>
|
||||
</data>
|
||||
<data name="NtpServerCount" xml:space="preserve">
|
||||
<value>{0}/{1} NTP servers</value>
|
||||
</data>
|
||||
<data name="NtpCalling" xml:space="preserve">
|
||||
<value>{0} In communication...</value>
|
||||
</data>
|
||||
<data name="LocalTimeOffset" xml:space="preserve">
|
||||
<value>{0}, local clock offset: {1} ms</value>
|
||||
</data>
|
||||
<data name="LocalTimeSyncDone" xml:space="preserve">
|
||||
<value>Local time has been synchronized</value>
|
||||
</data>
|
||||
<data name="Server" xml:space="preserve">
|
||||
<value>Server</value>
|
||||
</data>
|
||||
<data name="Status" xml:space="preserve">
|
||||
<value>Status</value>
|
||||
</data>
|
||||
<data name="LocalClockOffset" xml:space="preserve">
|
||||
<value>Local clock offset</value>
|
||||
</data>
|
||||
</root>
|
@ -122,4 +122,7 @@
|
||||
<data name="LocalClockOffset" xml:space="preserve">
|
||||
<value>Local clock offset</value>
|
||||
</data>
|
||||
<data name="ServerTime" xml:space="preserve">
|
||||
<value>NTP 标准网络时钟: {0}</value>
|
||||
</data>
|
||||
</root>
|
178
src/Time/Main.cs
178
src/Time/Main.cs
@ -1,3 +1,4 @@
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.InteropServices;
|
||||
@ -6,6 +7,13 @@ namespace Dot.Time;
|
||||
|
||||
public sealed class Main : Tool<Option>
|
||||
{
|
||||
private record Server
|
||||
{
|
||||
public int ConsoleRowIndex;
|
||||
public TimeSpan Offset;
|
||||
public ServerStatues Status;
|
||||
}
|
||||
|
||||
private enum ServerStatues : byte
|
||||
{
|
||||
Ready
|
||||
@ -29,39 +37,74 @@ public sealed class Main : Tool<Option>
|
||||
|
||||
private const int _MAX_DEGREE_OF_PARALLELISM = 10;
|
||||
private const int _NTP_PORT = 123;
|
||||
private readonly char[] _loading = { '-', '\\', '|', '/' };
|
||||
private const string _OUTPUT_TEMP = "{0,-30} {1,20} {2,20}";
|
||||
private static readonly object _lockObj = new();
|
||||
private int _procedCnt;
|
||||
private readonly int _serverCnt;
|
||||
|
||||
private readonly Dictionary<string, Server> _serverDictionary;
|
||||
|
||||
|
||||
private readonly string[] _serverDomains = {
|
||||
"ntp.ntsc.ac.cn", "cn.ntp.org.cn", "edu.ntp.org.cn"
|
||||
, "cn.pool.ntp.org", "time.pool.aliyun.com", "time1.aliyun.com"
|
||||
, "time2.aliyun.com", "time3.aliyun.com", "time4.aliyun.com"
|
||||
, "time5.aliyun.com", "time6.aliyun.com", "time7.aliyun.com"
|
||||
, "time1.cloud.tencent.com", "time2.cloud.tencent.com"
|
||||
, "time3.cloud.tencent.com", "time4.cloud.tencent.com"
|
||||
, "time5.cloud.tencent.com", "ntp.sjtu.edu.cn", "ntp.neu.edu.cn"
|
||||
, "ntp.bupt.edu.cn", "ntp.shu.edu.cn", "pool.ntp.org", "0.pool.ntp.org"
|
||||
, "1.pool.ntp.org", "2.pool.ntp.org", "3.pool.ntp.org"
|
||||
private readonly string[] _srvAddr = {
|
||||
"ntp.ntsc.ac.cn", "cn.ntp.org.cn", "edu.ntp.org.cn", "cn.pool.ntp.org"
|
||||
, "time.pool.aliyun.com", "time1.aliyun.com", "time2.aliyun.com"
|
||||
, "time3.aliyun.com", "time4.aliyun.com", "time5.aliyun.com"
|
||||
, "time6.aliyun.com", "time7.aliyun.com", "time1.cloud.tencent.com"
|
||||
, "time2.cloud.tencent.com", "time3.cloud.tencent.com"
|
||||
, "time4.cloud.tencent.com", "time5.cloud.tencent.com", "ntp.sjtu.edu.cn"
|
||||
, "ntp.neu.edu.cn", "ntp.bupt.edu.cn", "ntp.shu.edu.cn", "pool.ntp.org"
|
||||
, "0.pool.ntp.org", "1.pool.ntp.org", "2.pool.ntp.org", "3.pool.ntp.org"
|
||||
, "asia.pool.ntp.org", "time1.google.com", "time2.google.com"
|
||||
, "time3.google.com", "time4.google.com", "time.apple.com"
|
||||
, "time1.apple.com", "time2.apple.com", "time3.apple.com"
|
||||
, "time4.apple.com", "time5.apple.com", "time6.apple.com"
|
||||
, "time7.apple.com", "time.windows.com", "time.nist.gov"
|
||||
, "time-nw.nist.gov", "time-a.nist.gov", "time-b.nist.gov"
|
||||
, "stdtime.gov.hk"
|
||||
, "time3.google.com", "time4.google.com", "time.apple.com", "time1.apple.com"
|
||||
, "time2.apple.com", "time3.apple.com", "time4.apple.com", "time5.apple.com"
|
||||
, "time6.apple.com", "time7.apple.com", "time.windows.com", "time.nist.gov"
|
||||
, "time-nw.nist.gov", "time-a.nist.gov", "time-b.nist.gov", "stdtime.gov.hk"
|
||||
};
|
||||
|
||||
private readonly Dictionary<string, Server> _srvStatus;
|
||||
private int _successCnt;
|
||||
|
||||
|
||||
public Main(Option opt) : base(opt)
|
||||
{
|
||||
_serverCnt = _serverDomains.Length;
|
||||
_serverDictionary = _serverDomains.ToDictionary(x => x, _ => new Server { Status = ServerStatues.Ready });
|
||||
_serverCnt = _srvAddr.Length;
|
||||
var i = 0;
|
||||
_srvStatus = _srvAddr.ToDictionary(
|
||||
x => x, _ => new Server { Status = ServerStatues.Ready, ConsoleRowIndex = ++i });
|
||||
}
|
||||
|
||||
private static void ChangeStatus(KeyValuePair<string, Server> server, ServerStatues status
|
||||
, TimeSpan offset = default)
|
||||
{
|
||||
server.Value.Status = status;
|
||||
server.Value.Offset = offset;
|
||||
DrawTextInConsole(0, server.Value.ConsoleRowIndex
|
||||
, string.Format(_OUTPUT_TEMP, server.Key, server.Value.Status
|
||||
, status == ServerStatues.Succeed ? server.Value.Offset : string.Empty));
|
||||
}
|
||||
|
||||
private async Task DrawLoading()
|
||||
{
|
||||
char[] loading = { '-', '\\', '|', '/' };
|
||||
var loadingIndex = 0;
|
||||
while (true) {
|
||||
if (Volatile.Read(ref _procedCnt) == _serverCnt) break;
|
||||
await Task.Delay(100);
|
||||
++loadingIndex;
|
||||
for (var i = 0; i != _serverCnt; ++i)
|
||||
DrawTextInConsole(
|
||||
34, i + 1
|
||||
, _srvStatus[_srvAddr[i]].Status is ServerStatues.Succeed or ServerStatues.Failed
|
||||
? " "
|
||||
: loading[loadingIndex % 4].ToString());
|
||||
}
|
||||
|
||||
Debug.WriteLine(Environment.CurrentManagedThreadId + ":" + DateTime.Now.ToString("O"));
|
||||
}
|
||||
|
||||
private static void DrawTextInConsole(int left, int top, string text)
|
||||
{
|
||||
lock (_lockObj) {
|
||||
Console.SetCursorPosition(left, top);
|
||||
Console.Write(text);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -102,30 +145,35 @@ public sealed class Main : Tool<Option>
|
||||
}
|
||||
}
|
||||
|
||||
private async void Printing()
|
||||
private void PrintTemplate()
|
||||
{
|
||||
const string outputTemp = "{0,-30}\t{1}\t{2,20}\t{3,20}";
|
||||
var rolling = 0;
|
||||
|
||||
Console.Clear();
|
||||
while (true) {
|
||||
await Task.Delay(100);
|
||||
Console.SetCursorPosition(0, 0);
|
||||
Console.CursorVisible = false;
|
||||
var row = //
|
||||
_serverDictionary.Select(x //
|
||||
=> string.Format(outputTemp, x.Key
|
||||
, x.Value.Status == ServerStatues.Connecting
|
||||
? _loading[++rolling % 4]
|
||||
: ' ', x.Value.Status
|
||||
, x.Value.Offset == TimeSpan.Zero
|
||||
? string.Empty
|
||||
: x.Value.Offset));
|
||||
_srvStatus.Select(x //
|
||||
=> string.Format(_OUTPUT_TEMP, x.Key, x.Value.Status
|
||||
, x.Value.Offset == TimeSpan.Zero ? string.Empty : x.Value.Offset));
|
||||
|
||||
|
||||
Console.WriteLine(outputTemp, Str.Server, ' ', Str.Status, Str.LocalClockOffset);
|
||||
Console.WriteLine(_OUTPUT_TEMP, Str.Server, Str.Status, Str.LocalClockOffset);
|
||||
Console.WriteLine(string.Join(Environment.NewLine, row));
|
||||
if (_procedCnt == _serverCnt) break;
|
||||
}
|
||||
|
||||
private ValueTask ServerHandle(KeyValuePair<string, Server> server)
|
||||
{
|
||||
ChangeStatus(server, ServerStatues.Connecting);
|
||||
var offset = GetNtpOffset(server.Key);
|
||||
Interlocked.Increment(ref _procedCnt);
|
||||
|
||||
if (offset == TimeSpan.Zero) {
|
||||
ChangeStatus(server, ServerStatues.Failed);
|
||||
}
|
||||
else {
|
||||
Interlocked.Increment(ref _successCnt);
|
||||
ChangeStatus(server, ServerStatues.Succeed, offset);
|
||||
}
|
||||
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
@ -147,48 +195,42 @@ public sealed class Main : Tool<Option>
|
||||
SetLocalTime(timeToSet);
|
||||
}
|
||||
|
||||
|
||||
[SuppressMessage("ReSharper", "AccessToDisposedClosure")]
|
||||
public override async Task Run()
|
||||
{
|
||||
var tPrinting = Task.Run(Printing);
|
||||
PrintTemplate();
|
||||
var tLoading = DrawLoading();
|
||||
|
||||
await Parallel.ForEachAsync(_serverDictionary
|
||||
|
||||
await Parallel.ForEachAsync(_srvStatus
|
||||
, new ParallelOptions { MaxDegreeOfParallelism = _MAX_DEGREE_OF_PARALLELISM }
|
||||
, (server, _) => {
|
||||
server.Value.Status = ServerStatues.Connecting;
|
||||
var offset = GetNtpOffset(server.Key);
|
||||
, (server, _) => ServerHandle(server));
|
||||
|
||||
Interlocked.Increment(ref _procedCnt);
|
||||
await tLoading;
|
||||
|
||||
if (offset == TimeSpan.Zero) {
|
||||
server.Value.Status = ServerStatues.Failed;
|
||||
}
|
||||
else {
|
||||
server.Value.Status = ServerStatues.Succeed;
|
||||
Interlocked.Increment(ref _successCnt);
|
||||
server.Value.Offset = offset;
|
||||
}
|
||||
|
||||
return ValueTask.CompletedTask;
|
||||
});
|
||||
|
||||
tPrinting.Wait();
|
||||
var avgOffset = TimeSpan.FromTicks((long)_serverDictionary //
|
||||
var avgOffset = TimeSpan.FromTicks((long)_srvStatus //
|
||||
.Where(x => x.Value.Status == ServerStatues.Succeed)
|
||||
.Average(x => x.Value.Offset.Ticks));
|
||||
|
||||
|
||||
Console.SetCursorPosition(0, _serverCnt + 1);
|
||||
Console.WriteLine(Str.NtpReceiveDone, _successCnt, _serverCnt, avgOffset.TotalMilliseconds);
|
||||
if (!Opt.Sync) return;
|
||||
Console.WriteLine();
|
||||
|
||||
if (!Opt.Sync) {
|
||||
var _ = Task.Run(async () => {
|
||||
while (true) {
|
||||
await Task.Delay(100);
|
||||
Console.SetCursorPosition(0, Console.GetCursorPosition().Top);
|
||||
Console.Write(Str.ServerTime, (DateTime.Now - avgOffset).ToString("O"));
|
||||
Console.Write(@", {0}", Str.PressAnyKey);
|
||||
}
|
||||
// ReSharper disable once FunctionNeverReturns
|
||||
});
|
||||
Console.ReadKey();
|
||||
return;
|
||||
}
|
||||
|
||||
SetSysteTime(DateTime.Now - avgOffset);
|
||||
Console.WriteLine(Str.LocalTimeSyncDone);
|
||||
}
|
||||
|
||||
private record Server
|
||||
{
|
||||
public TimeSpan Offset;
|
||||
public ServerStatues Status;
|
||||
}
|
||||
}
|
@ -6,6 +6,6 @@ public class Option : IOption
|
||||
[Option('s', "sync", HelpText = nameof(Str.SyncToLocalTime), Default = false, ResourceType = typeof(Str))]
|
||||
public bool Sync { get; set; }
|
||||
|
||||
[Option('t', "timeout", HelpText = nameof(Str.TimeoutMillSecs), Default = 3000, ResourceType = typeof(Str))]
|
||||
[Option('t', "timeout", HelpText = nameof(Str.TimeoutMillSecs), Default = 2000, ResourceType = typeof(Str))]
|
||||
public int Timeout { get; set; }
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user