Skip to content

Commit 3b35033

Browse files
committed
修复测试用例
1 parent f2d2fdb commit 3b35033

File tree

5 files changed

+133
-31
lines changed

5 files changed

+133
-31
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,92 @@
1+
using System.IO;
2+
using System.Runtime.InteropServices;
13
using DotnetSpider.Infrastructure;
24
using Xunit;
5+
using Xunit.Abstractions;
36

47
namespace DotnetSpider.Tests
58
{
69
public class SystemInformationTests
710
{
11+
private readonly ITestOutputHelper _testOutput;
12+
13+
public SystemInformationTests(ITestOutputHelper testOutput)
14+
{
15+
_testOutput = testOutput;
16+
}
17+
818
[Fact()]
919
public void GetSystemInformation()
1020
{
21+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
22+
{
23+
_testOutput.WriteLine(File.ReadAllText("/proc/meminfo"));
24+
}
25+
1126
var memoryStatus = SystemInformation.MemoryStatus;
1227

28+
_testOutput.WriteLine(
29+
$"Free: {memoryStatus.FreeMemory}, Total: {memoryStatus.TotalMemory}, Used: {memoryStatus.UsedMemory}");
1330
Assert.True(memoryStatus.FreeMemory > 0);
1431
Assert.True(memoryStatus.TotalMemory > 0);
1532
Assert.True(memoryStatus.UsedMemory > 0);
1633
}
34+
35+
[Fact]
36+
public void LinuxParser()
37+
{
38+
var msg = @"MemTotal: 7733016 kB
39+
MemFree: 179152 kB
40+
MemAvailable: 3635216 kB
41+
Buffers: 1141108 kB
42+
Cached: 1728252 kB
43+
SwapCached: 0 kB
44+
Active: 4945628 kB
45+
Inactive: 1112120 kB
46+
Active(anon): 3476156 kB
47+
Inactive(anon): 35872 kB
48+
Active(file): 1469472 kB
49+
Inactive(file): 1076248 kB
50+
Unevictable: 12 kB
51+
Mlocked: 12 kB
52+
SwapTotal: 0 kB
53+
SwapFree: 0 kB
54+
Dirty: 136 kB
55+
Writeback: 0 kB
56+
AnonPages: 3188416 kB
57+
Mapped: 445348 kB
58+
Shmem: 323636 kB
59+
Slab: 1288188 kB
60+
SReclaimable: 1215212 kB
61+
SUnreclaim: 72976 kB
62+
KernelStack: 12720 kB
63+
PageTables: 37560 kB
64+
NFS_Unstable: 0 kB
65+
Bounce: 0 kB
66+
WritebackTmp: 0 kB
67+
CommitLimit: 3866508 kB
68+
Committed_AS: 12078760 kB
69+
VmallocTotal: 34359738367 kB
70+
VmallocUsed: 23504 kB
71+
VmallocChunk: 34359676304 kB
72+
Percpu: 1616 kB
73+
HardwareCorrupted: 0 kB
74+
AnonHugePages: 598016 kB
75+
CmaTotal: 0 kB
76+
CmaFree: 0 kB
77+
HugePages_Total: 0
78+
HugePages_Free: 0
79+
HugePages_Rsvd: 0
80+
HugePages_Surp: 0
81+
Hugepagesize: 2048 kB
82+
DirectMap4k: 123576 kB
83+
DirectMap2M: 3790848 kB
84+
DirectMap1G: 4194304 kB";
85+
86+
var total = SystemInformation.Linux.GetTotalMemory(msg);
87+
var free = SystemInformation.Linux.GetFreeMemory(msg);
88+
Assert.Equal(7551, total);
89+
Assert.Equal(3550, free);
90+
}
1791
}
1892
}

src/DotnetSpider/AgentCenter/Store/AgentHeartbeat.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class AgentHeartbeat
3232
/// 空闲内存
3333
/// </summary>
3434
[Column("free_memory")]
35-
public int FreeMemory { get; private set; }
35+
public long FreeMemory { get; private set; }
3636

3737
/// <summary>
3838
/// CPU 负载
@@ -46,7 +46,7 @@ public class AgentHeartbeat
4646
[Column("creation_time")]
4747
public DateTimeOffset CreationTime { get; private set; }
4848

49-
public AgentHeartbeat(string agentId, string agentName, int freeMemory, int cpuLoad)
49+
public AgentHeartbeat(string agentId, string agentName, long freeMemory, int cpuLoad)
5050
{
5151
agentId.NotNullOrWhiteSpace(nameof(agentId));
5252

src/DotnetSpider/AgentCenter/Store/AgentInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class AgentInfo
3030
/// 总内存
3131
/// </summary>
3232
[Column("total_memory")]
33-
public int TotalMemory { get; private set; }
33+
public long TotalMemory { get; private set; }
3434

3535
/// <summary>
3636
/// 上一次更新时间
@@ -51,7 +51,7 @@ public class AgentInfo
5151
[Column("creation_time")]
5252
public DateTimeOffset CreationTime { get; private set; }
5353

54-
public AgentInfo(string id, string name, int processorCount, int totalMemory)
54+
public AgentInfo(string id, string name, int processorCount, long totalMemory)
5555
{
5656
id.NotNullOrWhiteSpace(nameof(id));
5757
name.NotNullOrWhiteSpace(nameof(name));

src/DotnetSpider/Infrastructure/SystemInformation.cs

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ namespace DotnetSpider.Infrastructure
1414
{
1515
public struct MemoryStatus
1616
{
17-
public int FreeMemory;
18-
public int UsedMemory;
19-
public int TotalMemory;
17+
public long FreeMemory;
18+
public long UsedMemory;
19+
public long TotalMemory;
2020
}
2121

2222
public static class SystemInformation
@@ -26,7 +26,7 @@ private static class OSX
2626
{
2727
private static readonly int _structLength = Marshal.SizeOf<VmStatistics>();
2828
private static readonly int _pageSize;
29-
private static readonly int _totalMemory;
29+
private static readonly long _totalMemory;
3030

3131
static OSX()
3232
{
@@ -50,20 +50,17 @@ public static MemoryStatus GetMemoryStatus()
5050
statistics.compressor_page_count) *
5151
_pageSize) / 1024 / 1024;
5252

53-
return new MemoryStatus
54-
{
55-
TotalMemory = _totalMemory, UsedMemory = (int)(used), FreeMemory = (int)(free)
56-
};
53+
return new MemoryStatus {TotalMemory = _totalMemory, UsedMemory = (used), FreeMemory = (free)};
5754
}
5855

5956
/// <summary>
6057
/// MB
6158
/// </summary>
6259
/// <returns></returns>
63-
private static int GetTotalMemory()
60+
private static long GetTotalMemory()
6461
{
6562
var n = GetNumber("hw.memsize");
66-
return (int)(n / 1024 / 1024);
63+
return (n / 1024 / 1024);
6764
}
6865

6966
[DllImport("libc", CallingConvention = CallingConvention.Cdecl)]
@@ -166,7 +163,7 @@ private static long GetNumber(string param)
166163
private static class Windows
167164
{
168165
private static readonly uint _structLength = (uint)Marshal.SizeOf<WindowsMemoryStatus>();
169-
private static readonly int _totalMemory;
166+
private static readonly long _totalMemory;
170167

171168
static Windows()
172169
{
@@ -188,11 +185,11 @@ public static MemoryStatus GetMemoryStatus()
188185
};
189186
}
190187

191-
private static int GetTotalMemory()
188+
private static long GetTotalMemory()
192189
{
193190
var memoryInfo = new WindowsMemoryStatus {DwLength = _structLength};
194191
GlobalMemoryStatusEx(ref memoryInfo);
195-
return memoryInfo.Equals(default) ? 0 : (int)(memoryInfo.UllTotalPhys / 1024 / 1024);
192+
return memoryInfo.Equals(default) ? 0 : (memoryInfo.UllTotalPhys / 1024 / 1024);
196193
}
197194

198195
[StructLayout(LayoutKind.Sequential, Pack = 1)]
@@ -217,7 +214,7 @@ private struct WindowsMemoryStatus
217214
private static extern bool GlobalMemoryStatusEx(ref WindowsMemoryStatus mi);
218215
}
219216

220-
private static class Linux
217+
internal static class Linux
221218
{
222219
/// <summary>
223220
/// eg. "MemTotal: 12345 kB"
@@ -227,40 +224,71 @@ private static class Linux
227224
RegexOptions.Multiline | RegexOptions.Compiled
228225
);
229226

230-
private static readonly int _totalMemory;
227+
private static readonly long _totalMemory;
231228

232229
static Linux()
233230
{
234-
var dict = GetDict();
235-
_totalMemory = (int)dict["MemTotal"];
231+
_totalMemory = GetTotalMemory(GetMeminfo());
236232
}
237233

238234
public static MemoryStatus GetMemoryStatus()
239235
{
240-
var dict = GetDict();
241-
var free = (int)dict["MemAvailable"];
236+
var free = GetFreeMemory(GetMeminfo());
242237
return new MemoryStatus
243238
{
244239
TotalMemory = _totalMemory, FreeMemory = free, UsedMemory = _totalMemory - free
245240
};
246241
}
247242

248-
private static IDictionary<string, ulong> GetDict()
243+
internal static long GetTotalMemory(string output)
249244
{
250-
var path = "/proc/meminfo";
251-
if (!File.Exists(path))
245+
if (string.IsNullOrWhiteSpace(output))
252246
{
253-
return default;
247+
return 0;
248+
}
249+
250+
var dict = GetDict(output);
251+
var total = dict["MemTotal"];
252+
return total;
253+
}
254+
255+
internal static long GetFreeMemory(string output)
256+
{
257+
if (string.IsNullOrWhiteSpace(output))
258+
{
259+
return 0;
254260
}
255261

256-
var lines = File.ReadAllText(path);
262+
var dict = GetDict(output);
263+
var free = dict["MemAvailable"];
264+
return free;
265+
}
257266

267+
internal static IDictionary<string, long> GetDict(string output)
268+
{
269+
if (string.IsNullOrWhiteSpace(output))
270+
{
271+
return new Dictionary<string, long>();
272+
}
273+
274+
var lines = output;
258275
// ReSharper disable once RedundantEnumerableCastCall
259276
var dict = _linuxMemoryInfoLineRegex.Matches(lines).Cast<Match>().ToDictionary(match =>
260277
match.Groups["name"].Value.TrimStart()
261-
, match => ulong.Parse(match.Groups["value"].Value) * 1024);
278+
, match => long.Parse(match.Groups["value"].Value) / 1024);
262279
return dict;
263280
}
281+
282+
private static string GetMeminfo()
283+
{
284+
var path = "/proc/meminfo";
285+
if (!File.Exists(path))
286+
{
287+
return default;
288+
}
289+
290+
return File.ReadAllText(path);
291+
}
264292
}
265293

266294
public static async Task<double> GetCpuUsageForCurrentProcess()

src/DotnetSpider/MessageQueue/Messages.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Heartbeat : Message
2424
/// <summary>
2525
/// 空闲内存
2626
/// </summary>
27-
public int FreeMemory { get; set; }
27+
public long FreeMemory { get; set; }
2828

2929
/// <summary>
3030
/// CPU 负载
@@ -52,7 +52,7 @@ public class Register : Message
5252
/// <summary>
5353
/// 总内存
5454
/// </summary>
55-
public int TotalMemory { get; set; }
55+
public long TotalMemory { get; set; }
5656
}
5757
}
5858

0 commit comments

Comments
 (0)