Exemplo n.º 1
0
 public Task <BenchmarkReply> Post(BenchmarkRequest data)
 {
     // ProcessRequest(RequestType.PlainText, data);
     return(Task.FromResult(new BenchmarkReply {
         Message = data.Name
     }));
 }
Exemplo n.º 2
0
        public override async Task runBenchmark(BenchmarkRequest request, IServerStreamWriter <BenchmarkReply> responseStream, ServerCallContext context)
        {
            GC.Collect();
            Console.WriteLine(request);
            try
            {
                if (request.ShutdownServer)
                {
                    Program.ShutdownRequested = true;
                }
                else
                {
                    if (request.Algorithm == Algorithm.ProSecCo)
                    {
                        await runBatchMiner(request, responseStream, context);
                    }
                    else if (request.Algorithm == Algorithm.PrefixSpan)
                    {
                        await runPrefixSpan(request, responseStream, context);
                    }
                }

                GC.Collect();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
Exemplo n.º 3
0
        private async Task <CallResult[]> PlainTextAsync(int requestCount, CancellationToken ct)
        {
            var data = new BenchmarkRequest
            {
                Name = _config.GetRequestPayload(),
            };

            var duration = _config.GetDuration();

            if (duration != TimeSpan.Zero)
            {
                // timeout base
                using var cts       = new CancellationTokenSource(duration);
                using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cts.Token, ct);
                var linkedCt = linkedCts.Token;

                using var pool = new TaskWorkerPool <BenchmarkRequest, BenchmarkReply>(_config.ClientConcurrency, linkedCt);
                pool.RunWorkers((id, data, ct) => GetClient(id).SayHelloAsync(data), data, ct);
                await Task.WhenAny(pool.WaitForCompleteAsync(), pool.WaitForTimeout());

                return(pool.GetResult());
            }
            else
            {
                // request base
                using var pool = new TaskWorkerPool <BenchmarkRequest, BenchmarkReply>(_config.ClientConcurrency, ct)
                      {
                          CompleteCondition = x => x.completed >= requestCount,
                      };
                pool.RunWorkers((id, data, ct) => GetClient(id).SayHelloAsync(data), data, ct);
                await Task.WhenAny(pool.WaitForCompleteAsync(), pool.WaitForTimeout());

                return(pool.GetResult());
            }
        }
Exemplo n.º 4
0
 private void ProcessRequest(RequestType requestType, BenchmarkRequest body)
 {
     if (requestType == RequestType.PlainText)
     {
         var writer = GetWriter(Writer, sizeHint: 160 * 16); // 160*16 is for Plaintext, for Json 160 would be enough
         PlainText(ref writer, Encoding.UTF8.GetBytes(body.Name).AsSpan());
     }
 }
Exemplo n.º 5
0
            public async Task <BenchmarkReply> SayHelloAsync(BenchmarkRequest data)
            {
                var request = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json");

                using var res = await _client.PostAsync(_endpointPlainText, request);

                res.EnsureSuccessStatusCode();
                var content = await res.Content.ReadAsStringAsync();

                return(JsonSerializer.Deserialize <BenchmarkReply>(content));
            }
Exemplo n.º 6
0
        private async Task runBatchMiner(BenchmarkRequest request, IServerStreamWriter <BenchmarkReply> responseStream, ServerCallContext context)
        {
            Console.WriteLine("run batch miner " + request.UseTopK);

            await Task.Run(async() => {
                BatchMiner miner   = new BatchMiner();
                var batchStopwatch = new Stopwatch();
                batchStopwatch.Restart();
                long totalTimeElapsed = 0;
                //int numBlocks = (int)Math.Ceiling(request.DBSize/(double)request.SampleSize);

                await miner.ProcessCSVFile(
                    request.File,
                    request.Support,
                    request.K,
                    request.UseTopK,
                    request.SampleSize,
                    request.DBSize,
                    request.ErrorTolerance,
                    async delegate(List <Sequence> frequentSequencePatterns, double error, int iteration)
                {
                    batchStopwatch.Stop();
                    totalTimeElapsed += batchStopwatch.ElapsedMilliseconds;
                    var reply         = new BenchmarkReply
                    {
                        NrProcessedRecords = miner.NumTransactionsProcessed,
                        ReplyType          = ReplyType.Batch,
                        Iteration          = iteration,
                        Error = error,
                        BatchRuntimeInMillis                = batchStopwatch.ElapsedMilliseconds,
                        TotalRuntimeInMillis                = totalTimeElapsed,
                        PrevBlockFileReadingTime            = miner.PrevBlockFileReadingTime,
                        PrevBlockPreProcessingRuntime       = miner.Algorithm.PrevBlockPreProcessingRuntime,
                        PrevBlockPrefixSpanRuntime          = miner.Algorithm.PrevBlockPrefixSpanRuntime,
                        PrevBlockSubsequenceMatchingRuntime = miner.Algorithm.PrevBlockSubsequenceMatchingRuntime
                    };

                    var ser = JsonConvert.SerializeObject(frequentSequencePatterns, _jsonSettings);
                    reply.SequencesInJson = ser;
                    await responseStream.WriteAsync(reply);
                    batchStopwatch.Restart();
                },
                    long.MaxValue
                    );

                await responseStream.WriteAsync(new BenchmarkReply
                {
                    ReplyType            = ReplyType.Complete,
                    TotalRuntimeInMillis = totalTimeElapsed
                });
            });
        }
Exemplo n.º 7
0
        private void RunBenchmark(String pcapFilePath, IActorRef subject)
        {
            var captureInfo = new CaptureInfo(new Uri(pcapFilePath));
            var req         = new BenchmarkRequest {
                CaptureInfo = captureInfo, PcapLoader = this._services.GetService <IPcapLoader>()
            };

            var probe = this.CreateTestProbe();

            subject.Tell(req, probe);
            var result = probe.ExpectMsg <ProcessingResult>(TimeSpan.FromSeconds(60));

            this._output.WriteLine($"Total time: {result.TotalTime}");
        }
Exemplo n.º 8
0
        public void Test_DoMain()
        {
            var request = new BenchmarkRequest
            {
                Constituent1 = _reader.ReadResource("Test.Resource.Constituent1.txt").Array1,
                Constituent2 = _reader.ReadResource("Test.Resource.ConstituentExtrapolate.txt").Array1,
                IndexProxy   = _reader.ReadResource("Test.Resource.ProxyExtrapolate.txt").Array1,
                FXRate       = _reader.ReadResource("Test.Resource.FxRate.txt").Array1,
                Const1Weight = 0.6M,
                Const2Weight = 0.4M,
                AssetClassLeverageLongTermTargetLTV   = 0.525M,
                AssetClassLeverageLTVImpliedInProxies = 0.5M
            };

            var result = Helpers.DoMain(request).Result;

            var sum1 = result.Bench1.Sum();
            var sum2 = result.Bench2.Sum();

            Assert.AreEqual(1827, result.Bench1.Length);
            Assert.AreEqual(1827, result.Bench2.Length);

            Assert.AreEqual(100M, result.Bench1[0]);
            Assert.AreEqual(102.01260179936821023510537295M, result.Bench1[1]);
            Assert.AreEqual(102.28569387136973135806175615M, result.Bench1[2]);
            Assert.AreEqual(60.613395807105584389034344311M, result.Bench1[1825]);
            Assert.AreEqual(58.083916956257370307665124339M, result.Bench1[1826]);
            Assert.AreEqual(128700.71734227535247154670352M, sum1);

            Assert.AreEqual(100M, result.Bench2[0]);
            Assert.AreEqual(99.65084634755699301704409669M, result.Bench2[1]);
            Assert.AreEqual(98.47090088900808479798186066M, result.Bench2[2]);
            Assert.AreEqual(87.17968565685839338007814813M, result.Bench2[1825]);
            Assert.AreEqual(82.99126372590390566556216151M, result.Bench2[1826]);
            Assert.AreEqual(155154.71822011685792957086427M, sum2);
        }
Exemplo n.º 9
0
 public UnaryResult <BenchmarkReply> SayHelloAsync(BenchmarkRequest data)
 {
     return(UnaryResult(new BenchmarkReply {
         Message = data.Name
     }));
 }
Exemplo n.º 10
0
        private async Task runPrefixSpan(BenchmarkRequest request, IServerStreamWriter <BenchmarkReply> responseStream, ServerCallContext context)
        {
            Console.WriteLine("run prefix span");

            await Task.Run(async() => {
                Stopwatch stopwatch = Stopwatch.StartNew(); //creates and start the instance of Stopwatch

                List <Sequence> sequenceList = new List <Sequence>();

                // open the CSV file
                using (var reader = new StreamReader(File.OpenRead(request.File)))
                {
                    while (!reader.EndOfStream)
                    {
                        // read the current line
                        string line = reader.ReadLine();

                        // split the line by comma (assuming CSV file)
                        string [] transactions = line.Split(new string[] { " -1 " }, StringSplitOptions.None);

                        List <Transaction> tr = new List <Transaction>(transactions.Length);
                        for (int i = 0; i < transactions.Length - 1; i++)
                        {
                            string [] items   = transactions[i].Split(' ');
                            Transaction trans = new Transaction(items);
                            tr.Add(trans);
                        }

                        Sequence sequence = new Sequence(tr);
                        sequenceList.Add(sequence);
                    }
                }

                PrefixSpan algorithm = new PrefixSpan(sequenceList);

                List <Sequence> frequentSequences = algorithm.MinSupportFrequentSequences(request.Support);

                // stop the stopwatch after frequent patterns are
                // returned
                stopwatch.Stop();

                var reply = new BenchmarkReply
                {
                    NrProcessedRecords = request.DBSize,
                    ReplyType          = ReplyType.Batch,
                    Iteration          = 0,
                    Error = 0,
                    BatchRuntimeInMillis = stopwatch.ElapsedMilliseconds,
                    TotalRuntimeInMillis = stopwatch.ElapsedMilliseconds
                };
                var ser = JsonConvert.SerializeObject(frequentSequences, _jsonSettings);
                reply.SequencesInJson = ser;
                await responseStream.WriteAsync(reply);

                reply = new BenchmarkReply
                {
                    ReplyType            = ReplyType.Complete,
                    TotalRuntimeInMillis = stopwatch.ElapsedMilliseconds
                };

                await responseStream.WriteAsync(reply);
            });
        }
Exemplo n.º 11
0
 public Task <BenchmarkReply> SayHelloAsync(BenchmarkRequest data)
 {
     return(Task.FromResult(new BenchmarkReply {
         Message = data.Name
     }));
 }
Exemplo n.º 12
0
        public async static Task run()
        {
            var channel = new Channel("127.0.0.1:" + _port, ChannelCredentials.Insecure);
            var client  = new Benchmarker.BenchmarkerClient(channel);

            Report report = new Report()
            {
                Id             = _id,
                Algorithm      = _algorithm,
                NumberOfRuns   = _numberOfRuns,
                SampleSize     = _sampleSize,
                ErrorTolerance = _errorTolerance,
                Support        = _support,
                File           = _file
            };

            Console.Write("computing or reading expected results...");
            List <Sequence> expectedResults;
            var             cacheFilename = "../results/groundtruths/" + Path.GetFileNameWithoutExtension(_file) + "_" + _support + "_gt";

            if (File.Exists(cacheFilename))
            {
                var ser = File.ReadAllText(cacheFilename);
                expectedResults = JsonConvert.DeserializeObject <List <Sequence> >(ser, _jsonSettings);
            }
            else
            {
                Tuple <List <Sequence>, int> results = getGroundTruth(_file, _support);
                expectedResults = results.Item1;
                expectedResults.Sort((a, b) => b.Support.CompareTo(a.Support));
                var ser = JsonConvert.SerializeObject(expectedResults, _jsonSettings);
                File.WriteAllText(cacheFilename, ser);
            }
            Console.WriteLine(" done");


            for (int r = 0; r < _numberOfRuns; r++)
            {
                Run run = new Run()
                {
                    RunId = r
                };

                // shuffle
                Console.Write("shuffle file for run " + r + "...");
                var shuffledFile = shuffleDataset(_file);
                Console.WriteLine(" done");

                var ts = new CancellationTokenSource();
                CancellationToken ct = ts.Token;

                // setup memory monitor
                Task.Run(async() =>
                {
                    try
                    {
                        Stopwatch sw = new Stopwatch();
                        sw.Start();

                        Console.WriteLine("Start Memory Monitor");
                        using (var call = client.monitorMemory(new MemoryRequest()))
                        {
                            while (await call.ResponseStream.MoveNext())
                            {
                                if (ct.IsCancellationRequested)
                                {
                                    break;
                                }
                                var response = call.ResponseStream.Current;
                                double gb    = response.MemoryUsgageInBytes / (double)1073741824;
                                Console.WriteLine("Current Memory Usage: " + gb.ToString("N2") + "GB" + " / " + (sw.ElapsedMilliseconds / 1000) + "s / run nr: " + r);
                                if (!run.MemoryUsagePerTimeInMillis.ContainsKey(sw.ElapsedMilliseconds))
                                {
                                    run.MemoryUsagePerTimeInMillis.Add(sw.ElapsedMilliseconds, response.MemoryUsgageInBytes);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("MEMORY MONITOR FAILED");
                        Console.WriteLine(e.Message);
                        Console.WriteLine(e.StackTrace);
                        Console.WriteLine(e.InnerException.StackTrace);
                    }
                }, ct);


                var request = new BenchmarkRequest
                {
                    Id             = _id,
                    Algorithm      = _algorithm == "ProSecCo" ? Algorithm.ProSecCo : _algorithm == "PrefixSpan" ? Algorithm.PrefixSpan :Algorithm.Undefined,
                    SampleSize     = _sampleSize,
                    K              = _k,
                    UseTopK        = _useTopK,
                    DBSize         = _dbSize,
                    ErrorTolerance = _errorTolerance,
                    Support        = _support,
                    File           = shuffledFile,
                    ShutdownServer = false
                };

                Console.WriteLine(request);


                using (var call = client.runBenchmark(request))
                {
                    while (await call.ResponseStream.MoveNext())
                    {
                        var response = call.ResponseStream.Current;

                        if (response.ReplyType == ReplyType.Batch)
                        {
                            var sequences = JsonConvert.DeserializeObject <List <Sequence> >(response.SequencesInJson, _jsonSettings);
                            run.RuntimePerBatch.Add(response.BatchRuntimeInMillis);
                            run.Errors.Add(response.Error);

                            run.PrevBlockFileReadingTime.Add(response.PrevBlockFileReadingTime);
                            run.PrevBlockPreProcessingRuntime.Add(response.PrevBlockPreProcessingRuntime);
                            run.PrevBlockPrefixSpanRuntime.Add(response.PrevBlockPrefixSpanRuntime);
                            run.PrevBlockSubsequenceMatchingRuntime.Add(response.PrevBlockSubsequenceMatchingRuntime);

                            evaluate(run, response.Iteration, expectedResults, sequences, response.NrProcessedRecords);

                            /*Console.WriteLine("--- " + (run.Errors.Count() - 1));
                             * foreach (var seq in sequences)
                             * {
                             *  var description = "";
                             *  foreach(var itemSet in seq.Transactions) {
                             *      if (itemSet.NumItems == 1)
                             *          description += itemSet.ToString() + " ";
                             *      else
                             *          description +="{" + itemSet.ToString() + "} ";
                             *  }
                             *
                             *  description = description.Substring(0, description.Length - 1);
                             *
                             *  Console.WriteLine(description + ", " + ((double)seq.Support / (double)response.NrProcessedRecords) * (double)_dbSize);
                             * }*/
                        }
                        else if (response.ReplyType == ReplyType.Complete)
                        {
                            run.Errors.Add(response.Error);
                            run.TotalRuntimeInMillis = response.TotalRuntimeInMillis;
                        }
                    }
                }
                ts.Cancel();

                report.Runs.Add(run);
                Console.WriteLine("Wait for a little...");
                await Task.Delay(5000); // to make sure GC is done collecting on server side
            }

            if (_terminateServer)
            {
                client.runBenchmark(new BenchmarkRequest {
                    ShutdownServer = true
                });
                await channel.ShutdownAsync();
            }

            // write report
            File.WriteAllText("../results/" + _id + ".json", JsonConvert.SerializeObject(report, new JsonSerializerSettings()
            {
                Formatting = Formatting.Indented
            }));
        }