Ejemplo n.º 1
0
        private void FullSeqBenchmark(object ctobj)
        {
            chartFullSeq.Invoke(new Action(() =>
            {
                //chartFullSeq.Series[0].Points.Clear();
            }));
            CancellationToken token = (CancellationToken)ctobj;

            progressBar1.Invoke(new Action(() =>
            {
                progressBar1.Style = ProgressBarStyle.Marquee;
            }));
            float ioLengthMB   = 64;
            int   steplengthMB = 1024;

            Random random = new Random();

            byte[] buffer = new byte[(int)(1024 * 1024 * ioLengthMB)];
            GenerateRandomArray(buffer);
            string path = diskRootPath + "test.bin";

            if (File.Exists(path))
            {
                File.Delete(path);
            }
            long freeSpace = DiskOperation.GetHardDiskFreeSpace(diskRootPath);

            CreateDummyFile(path, freeSpace - 100 * 1024 * 1024);

            SafeFileHandle safeFileHandle = CreateFile(path, FileAccess.ReadWrite, FileShare.None, IntPtr.Zero, FileMode.Open, file_flags, IntPtr.Zero);

            using FileStream fileStream = new FileStream(safeFileHandle, FileAccess.ReadWrite, 0, false);

            //fileStream.Position = 0;
            //fileStream.Write(buffer, 0, steplengthMB * 1024 * ioLengthMB);
            //fileStream.Flush();
            //progressBar1.Invoke(new Action(() =>
            //{
            //    progressBar1.Style = ProgressBarStyle.Blocks;
            //}));
            List <double> testPoints = new List <double>();
            Stopwatch     speedTimer = new Stopwatch();

            //Console.WriteLine(Stopwatch.IsHighResolution);
            speedTimer.Start();
            Thread.Sleep(3000);
            //long prenum = 0L;
            long previousPos = 0L;
            // int loopTimes = 30;
            StringBuilder csvBuilder = new StringBuilder();

            for (int num = 0; num < freeSpace / (steplengthMB * 1024 * 1024); num++)
            {
                if (token.IsCancellationRequested)
                {
                    break;
                }
                //MessageBox.Show((steplengthMB * 1024 * 1024).ToString());
                //          using SafeFileHandle handle = File.OpenHandle(path + num.ToString(), FileMode.Create, FileAccess.Write, FileShare.None, FileOptions.WriteThrough
                //, preallocationSize: steplengthMB * 1024 * 1024); // new API (preview 6)
                //          using FileStream fileStream = new FileStream(handle, FileAccess.ReadWrite, 0, false);

                //Write steplengthMB
                double previousTime = speedTimer.Elapsed.TotalMilliseconds;
                for (int p = 0; p < steplengthMB / ioLengthMB; p++)
                {
                    fileStream.Position = previousPos + (int)(1024 * 1024 * p * ioLengthMB);
                    fileStream.Write(buffer, 0, (int)(1024 * 1024 * ioLengthMB));
                    //fileStream.Flush();
                }
                double curTime = speedTimer.Elapsed.TotalMilliseconds;

                //Console.WriteLine((curTime - previousTime) / 1000.0);
                double curSpeed = (float)steplengthMB / ((curTime - previousTime) / 1000.0);
                //Console.WriteLine(curSpeed);
                Debug.WriteLine(curSpeed);

                previousPos = previousPos + steplengthMB * 1024 * 1024;
                //continue;
                //GenerateRandomArray(buffer);

                if (num == 0)
                {
                    continue;
                }
                _ = UpdateChart(csvBuilder, num, curSpeed, steplengthMB);
                //Thread tChart = new Thread(new ThreadStart(() =>
                //{
                //    chartFullSeq.Invoke(new Action(() =>
                //    {
                //        //chartFullSeq.Series[0].Points.AddY(curSpeed);
                //        chartFullSeq.Series[0].Points.AddXY((num - 1) / 4.0, curSpeed);

                //    }));

                //    csvBuilder.Append(((num - 1) / 4.0).ToString());
                //    csvBuilder.Append(",");
                //    csvBuilder.Append(curSpeed);
                //    csvBuilder.AppendLine(",");

                //}));
            }

            fileStream.Close();
            File.Delete(path);
            speedTimer.Stop();
            File.WriteAllText("FullSeq_" + DateTime.Now.ToString("yyyy-MM-ddTHH-mm-ss") + ".csv", csvBuilder.ToString());
            progressBar1.Invoke(new Action(() =>
            {
                progressBar1.Style = ProgressBarStyle.Continuous;
                progressBar1.Value = 100;
            }));
        }
Ejemplo n.º 2
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (UdObj == null)
            {
                MessageBox.Show("请选择移动磁盘");
                return;
            }
            if (checkBoxScenario.Checked)
            {
                //Write4K_MultiThread(txtUDisk.Text + "test.bin", 16);
                //return;
            }

            if (btnStart.Text == "开始")
            {
                cts.Dispose();
                cts    = new CancellationTokenSource();
                tBench = new Thread(() =>
                {
                    try
                    {
                        if (checkBoxFast.Checked)
                        {
                            FastBenchmark(cts.Token);
                        }
                        if (checkBoxThread.Checked)
                        {
                            if (DiskOperation.GetHardDiskFreeSpace(diskRootPath) < 20 * 1024 * 1024)
                            {
                                MessageBox.Show("至少需要20GB可用空间进行测试!");
                                return;
                            }
                            if (checkBoxFast.Checked)
                            {
                                Thread.Sleep(10000);
                            }
                            MultiThreadBenchmark(cts.Token);
                        }
                        if (checkBoxFullSeq.Checked)
                        {
                            FullSeqBenchmark(cts.Token);
                            Thread.Sleep(10000);
                        }
                        if (checkBoxScenario.Checked)
                        {
                            if (DiskOperation.GetHardDiskFreeSpace(diskRootPath) < 20 * 1024 * 1024)
                            {
                                MessageBox.Show("至少需要20GB可用空间进行测试!");
                                return;
                            }
                            progressBar1.Invoke(new Action(() =>
                            {
                                progressBar1.Style = ProgressBarStyle.Marquee;
                            }));
                            List <Scenario> scenarios = new List <Scenario>();
                            scenarios.Add(CSVReader.Read(@".\Scenarios\normal_web.csv"));

                            long total_IO = ScenarioBenchmark(scenarios, cts.Token);
                            labelSceRes.Invoke(new Action(() => { labelSceRes.Text = (total_IO / 1000).ToString(); }));
                            progressBar1.Invoke(new Action(() =>
                            {
                                progressBar1.Style = ProgressBarStyle.Blocks;
                            }));
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    btnStart.Invoke(new Action(() => { btnStart.Text = "开始"; }));
                });
                tBench.Start();
                btnStart.Text = "停止";
            }
            else
            {
                if (tBench != null)
                {
                    cts.Cancel();

                    //tBench.Abort();
                    //Environment.Exit(0);
                }
                btnStart.Text = "开始";
            }
        }