forked from powerfun/udpservice
内存优化
This commit is contained in:
1 parent
8f0845faaa
commit
b07be239fd
6 files changed
+368
-202
No files matched your search
@@ -1,14 +1,10 @@
|
||||
using OnlineUserPool.Api;
|
||||
using OnlineUserPool.Model;
|
||||
using OnlineUserPool.Unility;
|
||||
|
||||
using Serilog;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Timers;
|
||||
|
||||
namespace OnlineUserPool.Hander
|
||||
{
|
||||
@@ -16,13 +12,19 @@ namespace OnlineUserPool.Hander
|
||||
{
|
||||
private static List<MapRecordRanking> mapRecordRankings = new List<MapRecordRanking>();
|
||||
private static object locker = new object();//防止服务端响应慢,多次加载
|
||||
//private int top = ConfigHelp.Top;
|
||||
private const int START = 120;
|
||||
private const int SMALL = 100;
|
||||
private const int MEDIUM = 150;
|
||||
private const int LARGE = 180;
|
||||
private const int OFFSET = 15;
|
||||
const int offset = 1;
|
||||
const int timeInterval = 15;
|
||||
List<MsgModel> msgModels = new List<MsgModel>();
|
||||
private DateTime UpdateVirtualTime { get; set; }
|
||||
|
||||
public MapRecordRankingHander()
|
||||
{
|
||||
Init();
|
||||
//var timer = new Timer();
|
||||
//timer
|
||||
}
|
||||
|
||||
private void Init()
|
||||
@@ -32,26 +34,13 @@ namespace OnlineUserPool.Hander
|
||||
var records = WebService.GetMapRouteRandomRecord(START, null);
|
||||
mapRecordRankings.AddRange(WebService.GetRecordFileFromServer(records.Select(n => n.Id).ToList()));
|
||||
UpdateVirtualTime = DateTime.Now;
|
||||
//var pageSize = 10;
|
||||
//var pageCount = (int)Math.Ceiling(records.Count / (double)pageSize);
|
||||
//for (int i = 0; i < pageCount; i++)
|
||||
//{
|
||||
// mapRecordRankings.AddRange(WebService.GetRecordFileFromServer(records.Skip(i * pageSize).Take(pageSize).Select(n => n.Id).ToList()));
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
private const int START = 120;
|
||||
private const int SMALL = 100;
|
||||
private const int MEDIUM = 150;
|
||||
private const int LARGE = 180;
|
||||
private const int OFFSET = 15;
|
||||
|
||||
//根据当前时段计算虚拟总人数
|
||||
private int ComputeTop()
|
||||
{
|
||||
var rand = new Random();
|
||||
|
||||
var hour = DateTime.Now.Hour;//当前点数
|
||||
int offset = 1;//少:1 中:3 多:10
|
||||
if (hour <= 6)
|
||||
@@ -79,11 +68,13 @@ namespace OnlineUserPool.Hander
|
||||
/// <returns></returns>
|
||||
public List<MsgModel> GetVirtualUserData()
|
||||
{
|
||||
List<MsgModel> msgModels = new List<MsgModel>();
|
||||
if(ConfigHelp.ShowVirtualUser == false)
|
||||
msgModels.Clear();
|
||||
|
||||
if (ConfigHelp.ShowVirtualUser == false)
|
||||
{
|
||||
return msgModels;
|
||||
}
|
||||
|
||||
if (mapRecordRankings != null)
|
||||
{
|
||||
for (int i = 0; i < mapRecordRankings.Count; i++)
|
||||
@@ -93,12 +84,7 @@ namespace OnlineUserPool.Hander
|
||||
var item = mapRecordRankings[i];
|
||||
item.CurrentIndex++;
|
||||
TargetData targetData = item.GetCurrentTargetData();
|
||||
//List<string> prop = new List<string>()
|
||||
//{
|
||||
// Math.Round(targetData._Speed, 2).ToString(),
|
||||
// targetData._Power.ToString(),
|
||||
// targetData._Cadence.ToString()
|
||||
//};
|
||||
|
||||
var weightKg = 0.0D;
|
||||
if (targetData._Power >= 0)
|
||||
{
|
||||
@@ -110,9 +96,9 @@ namespace OnlineUserPool.Hander
|
||||
//机器人上线
|
||||
var loginInfo = new MsgModel()
|
||||
{
|
||||
exit = item.End,
|
||||
Exit = item.End,
|
||||
IsCompleted = item.End,
|
||||
MemberId = item.UserId,//虚拟的人Id变为负数
|
||||
MemberId = item.UserId, //虚拟的人Id变为负数
|
||||
Point = new double[] { -1d, -1d },
|
||||
//Prop = string.Join(',', prop),
|
||||
RouteId = item.RouteId,
|
||||
@@ -126,11 +112,12 @@ namespace OnlineUserPool.Hander
|
||||
};
|
||||
msgModels.Add(loginInfo);
|
||||
}
|
||||
|
||||
var info = new MsgModel()
|
||||
{
|
||||
exit = item.End,
|
||||
Exit = item.End,
|
||||
IsCompleted = item.End,
|
||||
MemberId = item.UserId,//虚拟的人Id变为负数
|
||||
MemberId = item.UserId, //虚拟的人Id变为负数
|
||||
Point = new double[] { Math.Round(targetData._Lat, 6), Math.Round(targetData._Lon, 6) },
|
||||
//Prop = string.Join(',', prop),
|
||||
RouteId = item.RouteId,
|
||||
@@ -148,13 +135,12 @@ namespace OnlineUserPool.Hander
|
||||
{
|
||||
Log.Error("加载虚拟人物错误:" + e.Message + "\r\n" + e.StackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return msgModels;
|
||||
}
|
||||
const int offset = 1;
|
||||
const int timeInterval = 15;
|
||||
private DateTime UpdateVirtualTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 删除已经骑行完的人,添加新的人物进去
|
||||
/// </summary>
|
||||
@@ -162,11 +148,9 @@ namespace OnlineUserPool.Hander
|
||||
{
|
||||
lock (locker)
|
||||
{
|
||||
//var offlineCount = mapRecordRankings.Where(c => c.End).Count();//下线人数
|
||||
var top = ComputeTop();//理论上当前时间段虚拟用户的总数
|
||||
mapRecordRankings.RemoveAll(n => n.End);//移除骑行结束的人
|
||||
var routeIds = mapRecordRankings.Select(n => n.RouteId).Distinct().ToList();
|
||||
|
||||
var currentVirtualCount = mapRecordRankings.Count(d => d.UserId < 0);//实际虚拟用户数
|
||||
if (top - currentVirtualCount > 0)
|
||||
{
|
||||
|
||||
@@ -68,7 +68,7 @@ namespace OnlineUserPool.Hander
|
||||
var point = _turfHelper.Along(item.Distance * _index);
|
||||
var info = new MsgModel()
|
||||
{
|
||||
exit = false,
|
||||
Exit = false,
|
||||
IsCompleted = false,
|
||||
MemberId = item.Id,//虚拟的人Id变为负数
|
||||
Point = new double[] { Math.Round(point.Latitude, 6), Math.Round(point.Longitude, 6) },
|
||||
@@ -81,7 +81,7 @@ namespace OnlineUserPool.Hander
|
||||
Speed = item.Speed,
|
||||
WeightKg = 0,
|
||||
PreDistance = preDistance,
|
||||
Competitionid = _competitionid
|
||||
CompetitionId = _competitionid
|
||||
};
|
||||
msgModels.Add(info);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user