Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过取5倍发送字节的平均值,计算每秒发送文件的速度_C#_Performance_Average_Transfer - Fatal编程技术网

C# 通过取5倍发送字节的平均值,计算每秒发送文件的速度

C# 通过取5倍发送字节的平均值,计算每秒发送文件的速度,c#,performance,average,transfer,C#,Performance,Average,Transfer,我正在尝试使用平均值计算每秒传输文件的速度 我每秒5次计算发送字节和前一个字节的差值 下面的代码是否给出了正确的速度? 我应该更改速率数组大小吗? 还是应该更改线程睡眠(值)? 我很困惑,因为每次一点变化,速度值都会变化。。正确的解决方案是什么 static long prevSum = 0; static long[] rate = new long[5]; private static void SpeedPerSec(object o) {

我正在尝试使用平均值计算每秒传输文件的速度
我每秒5次计算发送字节和前一个字节的差值
  • 下面的代码是否给出了正确的速度?
  • 我应该更改速率数组大小吗?
  • 还是应该更改线程睡眠(值)?
    我很困惑,因为每次一点变化,速度值都会变化。。正确的解决方案是什么

        static long prevSum = 0;
        static long[] rate = new long[5];
        private static void SpeedPerSec(object o)
        {
            fileProgress fP = (fileProgress)o; //get the form conrtols
            while (busy)    // while sending file is active
            {
                for (int i = 0; i < rate.Length; i++)
                {
                    //diff between the sent bytes and prev sent bytes
                    rate[i] = (sum - prevSum);
                    Thread.Sleep(1000/rate.Length);
                }
                prevSum = sum;
                fP.RateLabel(Convert.ToInt64(rate.Average()));   
                //print the trasnfer rate which take a long value .. it just print the value in MB or KB string
            }
        }
    
    static long prevSum=0;
    静态长[]率=新长[5];
    私有静态空域SPEEDPERCEC(对象o)
    {
    fileProgress fP=(fileProgress)o;//获取表单conrtols
    while(busy)//发送文件时处于活动状态
    {
    for(int i=0;i
    以下是sendFile代码:

        public static bool busy = false;
        public static Socket client;
        public static int packetSize = 1024*8;
        public static int count = 0;
        public static long fileSize;
        public static long sum = 0;
    public static void sendFile(string filePath)
        {
            // run the progres Form
            Thread thFP = new Thread(fpRUN);
            fileProgress fP = new fileProgress("Sending...");
            thFP.Start(fP);
    
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            string fileName = Path.GetFileName(filePath);
            byte[] fileData;
            try
            {
                //sending file name and file size to the server
                busy = true;
                fileSize = fs.Length;
                byte[] fileDetial = null;
                string detail =  fileName + "," + fileSize.ToString();
                fileDetial = Encoding.ASCII.GetBytes(detail);
                client.Send(fileDetial);
    
                //sending file data to the server
    
                fileData = new byte[packetSize];
                count = 0;
                sum = 0;
    
                Thread thSpeed = new Thread(SpeedAndTimeLeft); //*here the thread of SPEED per second method
                thSpeed.Start(fP);
                fP.SizeLabel(fileSize);                     // tell the form the file size
                Thread thClock = new Thread(fP.clock);
                thClock.Start();
                while (sum < fileSize)
                {
                    fs.Seek(sum, SeekOrigin.Begin);
                    fs.Read(fileData, 0, fileData.Length);
                    count = client.Send(fileData, 0, fileData.Length, SocketFlags.None);
                    sum += count;
                    fP.ProgressBarFileHandler(sum,fileSize);        //progressbar value
                    fP.SentLabel(sum, fileSize);                    //tell the form how much sent                }
            }
            finally
            {
                busy = false;
                fs.Close();
                fileData = null;
                MessageBox.Show(string.Format("{0} sent successfully", fileName));
            }
        }
    
    public static bool busy=false;
    公共静态套接字客户端;
    公共静态int packetSize=1024*8;
    公共静态整数计数=0;
    公共静态长文件大小;
    公共静态长和=0;
    公共静态void sendFile(字符串文件路径)
    {
    //运行progres表单
    线程thFP=新线程(fpRUN);
    fileProgress fP=新文件进度(“发送…”);
    thFP.启动(fP);
    FileStream fs=newfilestream(filePath,FileMode.Open,FileAccess.Read);
    字符串fileName=Path.GetFileName(filePath);
    字节[]文件数据;
    尝试
    {
    //将文件名和文件大小发送到服务器
    忙=真;
    fileSize=fs.Length;
    字节[]fileDetial=null;
    字符串详细信息=fileName+“,“+fileSize.ToString();
    fileDetial=Encoding.ASCII.GetBytes(详细信息);
    client.Send(fileDetial);
    //将文件数据发送到服务器
    fileData=新字节[packetSize];
    计数=0;
    总和=0;
    Thread thSpeed=新线程(SpeedAndTimeLeft);//*这里是每秒速度的线程方法
    thSpeed.Start(fP);
    fP.SizeLabel(fileSize);//告诉表单文件大小
    线程时钟=新线程(fP.clock);
    thClock.Start();
    while(总和<文件大小)
    {
    Seek(sum,SeekOrigin.Begin);
    fs.Read(fileData,0,fileData.Length);
    count=client.Send(fileData,0,fileData.Length,SocketFlags.None);
    总和+=计数;
    fP.ProgressBarFileHandler(总和,文件大小);//progressbar值
    fP.sentlab(sum,fileSize);//告诉表单发送了多少}
    }
    最后
    {
    忙=假;
    fs.Close();
    fileData=null;
    Show(string.Format(“{0}发送成功”,文件名));
    }
    }
    
    我不明白您为什么要使用long[]rate变量。。。如果要计算传输速率并每秒更新一次,则应将当前文件大小存储在变量中,然后在睡眠后查看新文件大小。然后从新文件中减去以前的文件,就得到了最后一秒的传输速率(实时传输速率)。对于一般传输速率,您应该在下载/上载开始时获取一个时间戳,然后在每次睡眠后,通过将当前文件大小除以到目前为止经过的总秒数来计算速率。

    而不是每次计算平均值(如果速率数组变得非常大,则可能会变慢),你可以这样计算

    考虑到这个例子,您的费率就是这些

    long[] rates = new long[] { 5, 1, 3,4 ,2 ,5, 1};
    
    您可以这样计算每个步骤的平均值:

    double currentAverage = 0;
    for (int i = 0; i < rates.Length; i++)
    {
       long currentRate = rates[i];
       int iteration = i + 1;
       currentAverage = (currentAverage * i + currentRate) / iteration;
    }
    
    double currentAverage=0;
    for(int i=0;i

    然后等待每一个速率样本。

    sum既没有定义,也没有在代码段中为其赋值。请注意,prevSum不会在每次调用“SpeedPerSec”方法时重新初始化。您确定“速率”是一个速率,而不仅仅是传输的字节数吗?我想在有人能帮助你之前,你需要提供更多你想做的事情的细节。@JAN sum是静态长值,是当前发送的字节。@gt SpeedPerSec调用过一次。。一线生机。。prevSum是最后发送的字节之和。。总和是当前发送的字节数。。速率是它们之间的差异,正如我在文章中所说,没有“正确”的代码“以一种好看的方式显示电流传输速度”。您以前的代码没有以1秒的间隔生成“好看”的结果,我怀疑您能否以较小的间隔获得更好的结果—您的call.fileSize是常量。。您想让我初始化一个包含fileSize-sum(已发送字节)的变量吗??我认为这没有道理!long[]每秒获取发送字节平均值的速率。。这是为了避免增加和减少速度,例如:(30MB/秒,5MB/秒,25MB/秒,4MB/秒)你说的是“文件速度”,没有提到你在哪里上传或下载。无论如何,如果您正在进行上传,那么记录发送的总字节数就足够了。然后,在每次睡眠后,除以目前为止通过的总秒数将得到总传输速率(您不必每秒检查传输速率10次,但就在您更新传输速率时)。很抱歉,我忘了说该应用程序即将启动。