public void Consume(object arg)
        {
            Params p = (Params)arg;

            using (consume_outputfile = new System.IO.StreamWriter(p.Path + "\\consume.txt"))
            {
                consume_outputfile.WriteLine("Database: " + Program.Database + "\t Variables: " + p.Variables + "\t Iterations: " + p.Iterations + "\t Timespan: " + p.timeSpan + "\t WriteFrequency: " + p.writefrequency + "\t InitIterations: " + p.inititerations);
                consume_outputfile.WriteLine("Variables\tDuration(ms)\tReadCount");

                int      variables          = p.Variables;
                TimeSpan variabletimespanTS = p.timeSpan;
                telemetry_c = new Telemetry(Program.VmName, p.Path, variables, adapter);
                telemetry_c.SetID("consume");
                telemetry_c.InitTelemetryCollection("consume");
                bool ex = false;
                while (c_mRunning)
                {
                    System.Diagnostics.Stopwatch sww = new System.Diagnostics.Stopwatch();
                    for (int ii = 0; ii < p.inititerations; ii++)
                    {
                        if (!c_mRunning)
                        {
                            break;
                        }
                        sww.Restart();
                        adapter.PrepareReadMultipleLatest(variables, variabletimespanTS);
                        adapter.ReadStatic(variables, variabletimespanTS);
                        sww.Stop();
                        int wait = 1000 - (int)sww.ElapsedMilliseconds;
                        if (wait > 0)
                        {
                            System.Threading.Thread.Sleep(wait);
                        }
                    }
                    var percent = p.Iterations / 100;
                    for (int i = 0; i < p.Iterations; i++)
                    {
                        if (!c_mRunning)
                        {
                            break;
                        }
                        adapter.PrepareReadMultipleLatest(variables, variabletimespanTS);
                        var sw = System.Diagnostics.Stopwatch.StartNew();
                        try
                        {
                            adapter.ReadStatic(variables, variabletimespanTS);
                        }catch (System.NullReferenceException e) { ex = true; continue; }

                        sw.Stop();
                        if (ex)
                        {
                            System.Console.WriteLine("Consumer: OK"); ex = false;
                        }
                        double elap = sw.Elapsed.TotalMilliseconds;
                        try
                        {
                            consume_outputfile.WriteLine(variables + "\t" + elap + "\t" + adapter.GetStaticReadCount());
                        }
                        catch (System.ObjectDisposedException) { }
                        if (i % percent == 0)
                        {
                            System.Console.WriteLine("Consumer: " + i / percent + "% progress");
                        }
                        int sleep = 1000 - (int)elap;
                        if (sleep > 0)
                        {
                            System.Threading.Thread.Sleep(sleep);
                        }
                    }
                    while (!p_done)
                    {
                        if (!c_mRunning)
                        {
                            break;
                        }
                        adapter.PrepareReadMultipleLatest(variables, variabletimespanTS);
                        try
                        {
                            adapter.ReadStatic(variables, variabletimespanTS);
                        }
                        catch (System.NullReferenceException e) { ex = true; continue; }
                    }

                    break;
                }
                System.Console.WriteLine("Consumer ending");
                telemetry_c.CollectVariableRunTelemetry(variables);
                telemetry_c.mRunning = false;
                consume_outputfile.Flush();
                consume_outputfile.Close();
                consume_outputfile.Dispose();
            }
        }
        public void Run()
        {
            mRunning = true;
            #region WriteDataSet
            WriteDataSet();
            if (System.Threading.ThreadPool.SetMinThreads(300, 300))
            {
                System.Console.WriteLine("Set minimum was successful");
            }
            System.Threading.Thread.Sleep(5000);
            #endregion
            #region TimeSpan Test
            using (outputfile = new System.IO.StreamWriter(Path + "\\timespan_read_test.txt"))
            {
                outputfile.WriteLine("Database: " + Program.Database + "\t MaxTimeSpan: " + maxtimespan + "\t Iterations: " + timespaniterations + "\t WriteFrequency: " + writefrequency);
                outputfile.WriteLine("Timespan\tDuration(ms)");

                //TimepanRead-TestCase
                System.Console.WriteLine("Start TimepanRead-TestCase");
                TimeSpan timespanTS    = TimeSpan.Parse(timespaninitvalue);
                TimeSpan maxtimespanTS = TimeSpan.Parse(maxtimespan);
                telemetry = new Telemetry(Program.VmName, Path, (int)timespanTS.TotalMinutes, adapter);
                telemetry.SetID("timespan");
                telemetry.InitTelemetryCollection("timespan");
                while (mRunning)
                {
                    for (int i = 0; i < timespaniterations; i++)
                    {
                        PrepareReadOne(timespanTS);
                        var sw = System.Diagnostics.Stopwatch.StartNew();
                        ReadOne(timespanTS);
                        sw.Stop();
                        double elap    = sw.Elapsed.TotalMilliseconds;
                        string logtext = "Timespan: " + timespanTS + " Duration: " + elap;
                        System.Console.WriteLine(logtext);
                        try
                        {
                            outputfile.WriteLine(timespanTS + "\t" + elap);
                        }
                        catch (System.ObjectDisposedException) { }
                    }
                    telemetry.CollectVariableRunTelemetry((int)timespanTS.TotalMinutes);
                    timespanTS += TimeSpan.Parse(timespaninc);
                    if (timespanTS > maxtimespanTS)
                    {
                        break;
                    }
                    try { outputfile.WriteLine("New TimeSpan: " + timespanTS + "\t"); }
                    catch (System.ObjectDisposedException) { }
                }
                telemetry.CloseVMConnection();
            }
            #endregion
            #region Variable Test
            using (outputfile = new System.IO.StreamWriter(Path + "\\variable_read_test.txt"))
            {
                outputfile.WriteLine("Database: " + Program.Database + "\t MaxVariables: " + maxvariables + "\t Iterations: " + variableiterations + "\t Timespan: " + variabletimespan + "\t WriteFrequency: " + writefrequency);
                outputfile.WriteLine("Variables\tDuration(ms)");

                //VariableRead-TestCase
                System.Console.WriteLine("Start VariableRead-TestCase");
                int      variables          = variablesinitvalue;
                TimeSpan variabletimespanTS = TimeSpan.Parse(variabletimespan);
                telemetry = new Telemetry(Program.VmName, Path, variables, adapter);
                telemetry.SetID("variable");
                telemetry.InitTelemetryCollection("variable");
                while (mRunning)
                {
                    for (int i = 0; i < variableiterations; i++)
                    {
                        PrepareReadMultiple(variables, variabletimespanTS);
                        var sw = System.Diagnostics.Stopwatch.StartNew();
                        ReadMultiple(variables, variabletimespanTS);
                        sw.Stop();
                        double elap    = sw.Elapsed.TotalMilliseconds;
                        string logtext = "Variables: " + variables + " Duration: " + elap;
                        System.Console.WriteLine(logtext);
                        try
                        {
                            outputfile.WriteLine(variables + "\t" + elap);
                        }
                        catch (System.ObjectDisposedException) { }
                    }
                    telemetry.CollectVariableRunTelemetry(variables);
                    variables += variableinc;
                    if (variables > maxvariables)
                    {
                        break;
                    }
                    try { outputfile.WriteLine("New Variable count: " + variables + "\t"); }
                    catch (System.ObjectDisposedException) { }
                }
                telemetry.CloseVMConnection();
            }
            #endregion
            #region Client Test
            using (outputfile = new System.IO.StreamWriter(Path + "\\client_read_test.txt"))
            {
                outputfile.WriteLine("Database: " + Program.Database + "\t MaxClient: " + maxclients + "\t Iterations: " + clientiterations + "\t Timespan: " + clienttimespan + "\t Variables: " + clientvariables + "\t WriteFrequency: " + writefrequency);
                outputfile.WriteLine("Clients\tDuration(ms)");

                //ClientRead-TestCase
                System.Console.WriteLine("Start ClientRead-TestCase");
                int      clients          = clientsinitvalue;
                TimeSpan clienttimespanTS = TimeSpan.Parse(clienttimespan);
                Running = true;
                for (int i = 0; i < clients; i++)
                {
                    System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ReadStaticC))
                    {
                        IsBackground = false
                    };
                    Params a = new Params(clientvariables, clienttimespanTS);
                    t.Start(a);
                    clientlist.Add(t);
                }
                System.Threading.Thread.Sleep(2000);
                PrepareReadMultiple(clientvariables, clienttimespanTS);
                telemetry = new Telemetry(Program.VmName, Path, clients, adapter);
                telemetry.SetID("client");
                telemetry.InitTelemetryCollection("client");
                while (mRunning)
                {
                    for (int i = 0; i < clientiterations; i++)
                    {
                        var sw = System.Diagnostics.Stopwatch.StartNew();
                        ReadStatic(clientvariables, clienttimespanTS);
                        sw.Stop();
                        double elap    = sw.Elapsed.TotalMilliseconds;
                        string logtext = "Clients: " + clients + " Duration: " + elap;
                        System.Console.WriteLine(logtext);
                        try
                        {
                            outputfile.WriteLine(clients + "\t" + elap);
                        }
                        catch (System.ObjectDisposedException) { }
                    }
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    telemetry.CollectVariableRunTelemetry(clients);
                    clients += clientinc;
                    if (clients > maxclients)
                    {
                        break;
                    }
                    System.Threading.ThreadPool.GetAvailableThreads(out int awt, out int acpt);
                    System.Console.WriteLine("Available: " + awt + " IO: " + acpt);
                    while (clientlist.Count <= clients)
                    {
                        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ReadStaticC))
                        {
                            IsBackground = false
                        };
                        Params a = new Params(clientvariables, clienttimespanTS);
                        t.Start(a);
                        clientlist.Add(t);
                        //System.Threading.Thread.Sleep(500);
                    }
                    System.Threading.ThreadPool.GetAvailableThreads(out int awt2, out int acpt2);
                    System.Console.WriteLine("After increase Available: " + awt2 + " IO: " + acpt2);
                    System.Threading.Thread.Sleep(2000);
                    try { outputfile.WriteLine("New Client count: " + clients + "\t"); }
                    catch (System.ObjectDisposedException) { }
                    System.Threading.Thread.Sleep(5000);
                }
                System.Console.WriteLine("ClientList.Count: " + clientlist.Count);
                foreach (var th in clientlist)
                {
                    if (!th.IsAlive)
                    {
                        System.Console.WriteLine("Client not alive");
                    }
                }
                Running = false;
                foreach (var t in clientlist)
                {
                    t.Join();
                }
                telemetry.CloseVMConnection();
            }
            #endregion
        }
        public void Produce(object arg)
        {
            Params p = (Params)arg;

            using (produce_outputfile = new System.IO.StreamWriter(p.Path + "\\produce.txt"))
            {
                produce_outputfile.WriteLine("Database: " + Program.Database + "\t Variables: " + p.Variables + "\t Iterations: " + p.Iterations + "\t WriteFrequency: " + p.writefrequency + "\t InitIterations: " + p.inititerations);
                produce_outputfile.WriteLine("Insert duration\tFinal duration\tRead back\tSpeed");

                telemetry_p = new Telemetry(Program.VmName, p.Path, p.Variables, adapter);
                telemetry_p.InitTelemetryCollection("produce");
                System.Diagnostics.Stopwatch onevariable_watch = new System.Diagnostics.Stopwatch();
                var w_mock = new System.Diagnostics.Stopwatch();
                int sleep  = (int)((1.0f / (double)p.writefrequency) * 1000);
                while (p_mRunning)
                {
                    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                    var startime = System.DateTime.Now;
                    var endtime  = startime.Add(p.timeSpan);
                    while (System.DateTime.Now.Subtract(endtime).TotalSeconds <= 0)
                    {
                        if (!p_mRunning)
                        {
                            break;
                        }
                        sw.Restart();
                        adapter.PrepareWriteData(p.Variables);
                        adapter.Send(ref onevariable_watch);
                        sw.Stop();
                        int wait = sleep - (int)sw.ElapsedMilliseconds;
                        if (wait > 0)
                        {
                            System.Threading.Thread.Sleep(wait);
                        }
                    }
                    while (p.Variables < max_var)
                    {
                        var percent = p.Iterations / 100;
                        for (int j = 0; j < p.Iterations; j++)
                        {
                            if (!p_mRunning)
                            {
                                break;
                            }
                            sw.Restart();
                            if (p.writefrequency > 1)
                            {
                                for (int i = 1; i < p.writefrequency; i++)
                                {
                                    var st0 = System.Diagnostics.Stopwatch.StartNew();
                                    adapter.PrepareWriteData(p.Variables);
                                    adapter.Send(ref w_mock);
                                    double elap0 = st0.Elapsed.TotalSeconds;
                                    int    wait0 = sleep - (int)(elap0 * 1000);
                                    if (wait0 > 0)
                                    {
                                        System.Threading.Thread.Sleep(wait0);
                                    }
                                }
                            }
                            adapter.PrepareWriteData(p.Variables);

                            adapter.Send(ref onevariable_watch);
                            var check1 = sw.Elapsed.TotalSeconds;
                            while (true)
                            {
                                bool x = adapter.CheckInsert();
                                if (x)
                                {
                                    break;
                                }
                                if (!p_mRunning)
                                {
                                    break;
                                }
                            }
                            onevariable_watch.Stop();
                            sw.Stop();
                            double elap  = sw.Elapsed.TotalSeconds;
                            float  speed = (float)(p.Variables / elap);
                            try
                            {
                                produce_outputfile.WriteLine(check1 + "\t" + elap + "\t" + onevariable_watch.Elapsed.TotalSeconds + "\t" + speed);
                            }
                            catch (System.ObjectDisposedException) { }
                            onevariable_watch.Reset();
                            if (j % percent == 0)
                            {
                                System.Console.WriteLine("Producer: " + j / percent + "% progress");
                            }
                            int wait = sleep - (int)sw.ElapsedMilliseconds;
                            if (wait > 0)
                            {
                                System.Threading.Thread.Sleep(wait);
                            }
                        }
                        if (p_mRunning)
                        {
                            p.Variables += increament;                             //Increase updated variable count
                            telemetry_p.CollectVariableRunTelemetry(p.Variables);
                            if (p.Variables >= max_var)
                            {
                                p_mRunning = false; break;
                            }
                            try { produce_outputfile.WriteLine("New Variable count: " + p.Variables + "\t"); }
                            catch (System.ObjectDisposedException) { }
                        }
                    }
                    p_done = true;
                    System.Console.WriteLine("Producer iterations finished");
                    telemetry_p.CollectVariableRunTelemetry(p.Variables);
                    telemetry_p.mRunning = false;
                    produce_outputfile.Flush();
                    produce_outputfile.Close();
                    produce_outputfile.Dispose();
                    System.Console.WriteLine("Producer start fill");
                    while (p_mRunning)
                    {
                        sw.Restart();
                        adapter.PrepareWriteData(p.Variables);
                        adapter.Send(ref onevariable_watch);
                        sw.Stop();
                        int wait = sleep - (int)(sw.ElapsedMilliseconds);
                        if (wait > 0)
                        {
                            System.Threading.Thread.Sleep(wait);
                        }
                    }
                    System.Console.WriteLine("Producer fill finished");
                    break;
                }
                System.Console.WriteLine("Producer ending");
            }
        }
        public void Run()
        {
            mRunning = true;
            int sleep = 1000 / frequency;

            using (outputfile = new System.IO.StreamWriter(Path + "\\write_test.txt"))
            {
                outputfile.WriteLine("Database: " + Program.Database + "\t Variables: " + variables + "\t Iterations: " + iterations + "\t WriteFrequency: " + frequency);
                outputfile.WriteLine("Insert duration\tFinal duration\tRead back\tSpeed");

                //Write-TestCase
                System.Console.WriteLine("Start Write-TestCase");
                telemetry = new Telemetry(Program.VmName, Path, variables, adapter);
                telemetry.InitTelemetryCollection("write");
                var w_mock      = new System.Diagnostics.Stopwatch();
                var endinittime = System.DateTime.Now.Add(inititerations);
                while (endinittime.Subtract(System.DateTime.Now).TotalSeconds > 0)
                {
                    if (!mRunning)
                    {
                        break;
                    }
                    var st = System.Diagnostics.Stopwatch.StartNew();
                    PrepareData(initvariables);
                    Send(ref w_mock);
                    st.Stop();
                    double elap = st.Elapsed.TotalSeconds;
                    int    wait = sleep - (int)(elap * 1000);
                    if (wait > 0)
                    {
                        System.Threading.Thread.Sleep(wait);
                    }
                }
                while (mRunning)
                {
                    for (int j = 0; j < iterations; j++)
                    {
                        if (!mRunning)
                        {
                            break;
                        }
                        var st = System.Diagnostics.Stopwatch.StartNew();
                        if (frequency > 1)
                        {
                            for (int i = 1; i < frequency; i++)
                            {
                                var st0 = System.Diagnostics.Stopwatch.StartNew();
                                PrepareData(variables);
                                Send(ref w_mock);
                                double elap0 = st0.Elapsed.TotalSeconds;
                                int    wait0 = sleep - (int)(elap0 * 1000);
                                if (wait0 > 0)
                                {
                                    System.Threading.Thread.Sleep(wait0);
                                }
                            }
                        }
                        var check00 = st.Elapsed.TotalSeconds;
                        PrepareData(variables);
                        var check0 = st.Elapsed.TotalSeconds;
                        Send(ref onevariable_watch);
                        var check1 = st.Elapsed.TotalSeconds;
                        while (true)
                        {
                            bool x = CheckInsert();
                            if (x)
                            {
                                break;
                            }
                            System.Threading.Thread.Sleep(100);
                        }
                        onevariable_watch.Stop();
                        st.Stop();
                        double elap    = st.Elapsed.TotalSeconds;
                        double d       = check0 - check00;
                        string logtext = "Insert duration: " + check1 + " Final duration: " + elap + " Read back: " + onevariable_watch.Elapsed.TotalSeconds + " PrepareData: " + d;
                        System.Console.WriteLine(logtext);
                        float speed = (float)(variables / elap);
                        System.Console.WriteLine("Speed: " + speed);

                        try
                        {
                            outputfile.WriteLine(check1 + "\t" + elap + "\t" + onevariable_watch.Elapsed.TotalSeconds + "\t" + speed);
                        }
                        catch (System.ObjectDisposedException) { }
                        onevariable_watch.Reset();
                        int wait = 1000 - (int)(elap * 1000);
                        if (wait > 0)
                        {
                            System.Threading.Thread.Sleep(wait);
                        }
                    }
                    if (mRunning)
                    {
                        variables += increament;                         //Increase updated variable count
                        telemetry.CollectVariableRunTelemetry(variables);
                        if (variables >= stop_value)
                        {
                            mRunning = false; break;
                        }
                        try { outputfile.WriteLine("New Variable count: " + variables + "\t"); }
                        catch (System.ObjectDisposedException) { }
                    }
                }
                telemetry.CloseVMConnection();
            }
        }