using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PONumberServer;
using System.Threading;
using System.Threading.Tasks;
using Untity;
using System.Reflection;
using ServerInterFace;
namespace MultiThreadTestTool
{
public partial class MainForm : Form
{
public List<long> timeList;
private object obj;
public string ServerType=string.Empty;
public IServer Iserver;
private string ServerUrl;
public MainForm()
{
InitializeComponent();
string strChoseItem = Configer.GetConfig("ChoseItem");
if (!string.IsNullOrWhiteSpace(strChoseItem))
{
string[] arrItem = strChoseItem.Split(',');
cmbChose.Items.AddRange(arrItem);
cmbChose.SelectedIndex = 0;
}
else {
btnLoad.Enabled = false;
}
string strThreadItem = Configer.GetConfig("ThreadItem");
if (!string.IsNullOrWhiteSpace(strThreadItem))
{
string[] arrItem = strThreadItem.Split(',');
cmbThreadChose.Items.AddRange(arrItem);
cmbThreadChose.SelectedIndex = 0;
}
tbInputValue.Text = "1021739";
timeList = new List<long>();
obj = new object();
}
private void btnLoad_Click(object sender, EventArgs e)
{
Logger.WriteProcessLog("开始时间:" + DateTime.Now.ToString());
btnLoad.Enabled = false;
timeList.Clear();
ServerType = cmbChose.SelectedItem.ToString();
if (string.IsNullOrWhiteSpace(ServerType))
{
return;
}
ServerUrl = Configer.GetConfig(ServerType) + tbInputValue.Text.Trim();
Assembly MyAssembly = Assembly.Load(ServerType + "Server");
Iserver = (IServer)MyAssembly.CreateInstance(ServerType + "Server." + ServerType);
int threadNum=0;
int.TryParse(cmbThreadChose.SelectedItem.ToString(), out threadNum);
threadNum = threadNum == 0 ? 1 : threadNum;
var taskQueue = new Queue<Task>();
for (int i = 0; i < threadNum; i++)
{
taskQueue.Enqueue(Task.Factory.StartNew(() =>
{
Execute();
}));
}
Task.Factory.ContinueWhenAll(taskQueue.ToArray(), completedTasks =>
{
taskCompleteExecute(threadNum);
});
}
void taskCompleteExecute(int threadNum)
{
int count = timeList.Count;
long maxtime = timeList.Max();
long mintime = timeList.Min();
long totalTime = 0;
timeList.ForEach((time) => { totalTime += time; });
float avgTime = totalTime / threadNum;
string strResult = "MinTime:" + mintime.ToString() + Environment.NewLine +
"MaxTime:" + maxtime.ToString() + Environment.NewLine +
"AvgTime:" + avgTime.ToString() + Environment.NewLine +
"EndTime:" + DateTime.Now.ToString();
;
SetTextBoxValue(strResult);
SetButtonValue(null);
}
void SetButtonValue(object obj) {
if (btnLoad.InvokeRequired)
{
btnLoad.Invoke(new Action<object>(SetButtonValue), obj);
}
else
{
this.btnLoad.Enabled = true;
}
}
void SetTextBoxValue(object obj) {
if (tbResult.InvokeRequired)
{
tbResult.Invoke(new Action<object>(SetTextBoxValue),obj);
}
else {
this.tbResult.Text = obj.ToString();
}
}
public void Execute() {
long time = Iserver.LoadData(ServerUrl);
lock (obj)
{
timeList.Add(time);
Logger.WriteProcessLog("耗时:"+ time.ToString() +" : "+DateTime.Now.ToString());
}
}
}
}
个人网站:http://www.jiebian.org