Example #1
0
 public POCTestReporter(POCTestResults r, MongoClient mc, POCTestOptions t)
 {
     mongoClient = mc;
     testResults = r;
     testOpts    = t;
     logger      = LogManager.GetLogger("POCTestReporter");
 }
        public void RunLoad(POCTestOptions testOpts, POCTestResults testResults)
        {
            PrepareSystem(testOpts, testResults);
            // Report on progress by looking at testResults
            var reporter = new POCTestReporter(testResults, mongoClient, testOpts);

            try
            {
                // Using a thread pool we keep filled
                // +1 for the reporter thread the timer will use
                ThreadPool.SetMaxThreads(testOpts.numThreads + 1, testOpts.numThreads + 1);

                // Allow for multiple clients to run -
                // Check for testOpts.threadIdStart - this should be an Int32 to start
                // the 'workerID' for each set of threads.
                int threadIdStart = testOpts.threadIdStart;
                //Console.Out.WriteLine("threadIdStart="+threadIdStart);
                reporter.run();
                var workers = new List <MongoWorker>();
                var tasks   = new List <Task>();
                for (int i = threadIdStart; i < (testOpts.numThreads + threadIdStart); i++)
                {
                    var worker = new MongoWorker(mongoClient, testOpts, testResults, i);
                    var task   = new Task(new Action(() => worker.run(null)));
                    workers.Add(worker);
                    tasks.Add(task);
                    task.Start();
                }

                Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) {
                    e.Cancel = true;
                    if (isCancelled)
                    {
                        return;
                    }
                    isCancelled = true;
                    foreach (var worker in workers)
                    {
                        worker.Cancel();
                    }
                    reporter.Cancel();
                    logger.Info("Cancellation requested - waiting on threads to finish");
                };

                Task.WaitAll(tasks.ToArray());
                //Console.Out.WriteLine("All Threads Complete: " + b);
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.Message);
            }
            finally
            {
                // do const report
                reporter.constReport();
            }
        }
 public LoadRunner(POCTestOptions testOpts)
 {
     logger = LogManager.GetLogger("LoadRunner");
     try
     {
         //For not authentication via connection string passing of user/pass only
         mongoClient = new MongoClient(new MongoUrl(testOpts.host));
     }
     catch (Exception e)
     {
         logger.Error(e.StackTrace);
     }
 }
Example #4
0
        private static void printTestBsonDocument(POCTestOptions testOpts)
        {
            //Sets up sample data don't remove
            int[] arr = new int[2];
            arr[0] = testOpts.arrays[0];
            arr[1] = testOpts.arrays[1];
            var tr = new TestRecord(testOpts.numFields, testOpts.depth, testOpts.textFieldLen,
                                    1, 12345678, POCTestOptions.NUMBER_SIZE, arr, testOpts.blobSize);
            //Console.Out.WriteLine(tr);

            String        json    = tr.internalDoc.ToJson();
            StringBuilder newJson = new StringBuilder();
            int           arrays  = 0;

            // Collapse inner newlines
            Boolean inquotes = false;

            for (int c = 0; c < json.Length; c++)
            {
                char inChar = json[c];
                if (inChar == '[')
                {
                    arrays++;
                }
                if (inChar == ']')
                {
                    arrays--;
                }
                if (inChar == '"')
                {
                    inquotes = !inquotes;
                }

                if (arrays > 1 && inChar == '\n')
                {
                    continue;
                }
                if (arrays > 1 && !inquotes && inChar == ' ')
                {
                    continue;
                }
                newJson.Append(json[c]);
            }
            logger.Info(newJson.ToString());

            byte[] bsonBytes = tr.internalDoc.ToBson();
            long   length    = bsonBytes.LongLength;

            logger.Info(String.Format("Records are {0:0.##} KB each as BSON", (float)length / 1024));
        }
Example #5
0
        public MongoWorker(MongoClient c, POCTestOptions t, POCTestResults r, int id)
        {
            logger      = LogManager.GetLogger($"MongoWorker {id}");
            mongoClient = c;

            //Ping
            c.GetDatabase("admin").RunCommand <BsonDocument>(new BsonDocument("ping", 1));
            testOpts    = t;
            testResults = r;
            workerID    = id;
            var db = mongoClient.GetDatabase(testOpts.namespaces[0]);

            maxCollections = testOpts.numcollections;
            String baseCollectionName = testOpts.namespaces[1];

            if (maxCollections > 1)
            {
                colls          = new List <IMongoCollection <BsonDocument> >();
                lastCollection = 0;
                for (int i = 0; i < maxCollections; i++)
                {
                    String str = baseCollectionName + i;
                    colls.Add(db.GetCollection <BsonDocument>(str));
                }
            }
            else
            {
                coll = db.GetCollection <BsonDocument>(baseCollectionName);
            }

            // id
            sequence = getHighestID();

            ReviewShards();
            rng = new Random();
            if (testOpts.zipfsize > 0)
            {
                zipfian = true;
                zipf    = new Zipf(0.99, testOpts.zipfsize);
            }

            if (!string.IsNullOrWhiteSpace(testOpts.workflow))
            {
                workflow   = testOpts.workflow;
                workflowed = true;
                keyStack   = new List <BsonDocument>();
            }
        }
        private void PrepareSystem(POCTestOptions testOpts, POCTestResults results)
        {
            IMongoDatabase db;
            IMongoCollection <BsonDocument> coll;

            //Create indexes and suchlike
            db   = mongoClient.GetDatabase(testOpts.namespaces[0]);
            coll = db.GetCollection <BsonDocument>(testOpts.namespaces[1]);
            if (testOpts.emptyFirst)
            {
                db.DropCollection(testOpts.namespaces[1]);
            }

            TestRecord    testRecord = new TestRecord(testOpts);
            List <String> fields     = testRecord.listFields();

            for (int x = 0; x < testOpts.secondaryidx; x++)
            {
                coll.Indexes.CreateOne(new BsonDocument(fields[x], 1));
            }
            if (testOpts.fulltext)
            {
                var options = new CreateIndexOptions();
                options.Background = true;
                var weights = new BsonDocument();
                weights.Add("lorem", 15);
                weights.Add("_fulltext.text", 5);
                options.Weights = weights;
                var index = new BsonDocument();
                index.Add("$**", "text");
                coll.Indexes.CreateOne(index, options);
            }

            results.initialCount = coll.Count(new BsonDocument());
            //Now have a look and see if we are sharded
            //And how many shards and make sure that the collection is sharded
            if (!testOpts.singleserver)
            {
                ConfigureSharding(testOpts);
            }
        }
Example #7
0
        private static void ConfigureLogging(POCTestOptions testOpts)
        {
            // Step 1. Create configuration object
            var config = new LoggingConfiguration();

            // Step 2. Create targets and add them to the configuration
            var consoleTarget = new ColoredConsoleTarget();

            config.AddTarget("console", consoleTarget);

            var fileTarget = new FileTarget();

            config.AddTarget("file", fileTarget);

            // Step 3. Set target properties
            consoleTarget.Layout = @"${date:format=HH\:mm\:ss} ${logger} ${message}";
            if (string.IsNullOrWhiteSpace(testOpts.logfile))
            {
                fileTarget.FileName = "${basedir}/POCDriver-csharp_log.txt";
            }
            else
            {
                fileTarget.FileName = testOpts.logfile;
            }
            fileTarget.Layout = @"${date:format=HH\:mm\:ss} ${logger} ${message}";

            // Step 4. Define rules
            var rule1 = new LoggingRule("*", testOpts.debug ? LogLevel.Debug : LogLevel.Info, consoleTarget);

            config.LoggingRules.Add(rule1);

            var rule2 = new LoggingRule("*", testOpts.debug ? LogLevel.Debug : LogLevel.Info, fileTarget);

            config.LoggingRules.Add(rule2);

            // Step 5. Activate the configuration
            LogManager.Configuration = config;
        }
Example #8
0
 public TestRecord(POCTestOptions testOpts) :
     this(testOpts.numFields, testOpts.depth, testOpts.textFieldLen,
          testOpts.workingset, 0, POCTestOptions.NUMBER_SIZE,
          new int[] { testOpts.arrays[0], testOpts.arrays[1] }, testOpts.blobSize)
 {
 }
        private void ConfigureSharding(POCTestOptions testOpts)
        {
            IMongoDatabase admindb = mongoClient.GetDatabase("admin");
            BsonDocument   cr      = admindb.RunCommand <BsonDocument>(new BsonDocument("serverStatus", 1));

            if (cr.GetValue("ok").AsDouble == 0)
            {
                logger.Info(cr.ToJson());
                return;
            }

            String procname = (String)cr.GetValue("process").AsString;

            if (procname != null && procname.Contains("mongos"))
            {
                testOpts.sharded = true;
                //Turn the auto balancer off - good code rarely needs it running constantly
                IMongoDatabase configdb = mongoClient.GetDatabase("config");
                IMongoCollection <BsonDocument> settings = configdb.GetCollection <BsonDocument>("settings");
                settings.UpdateOne(new BsonDocument("_id", "balancer"), new BsonDocument("$set", new BsonDocument("stopped", true)));
                //Console.Out.WriteLine("Balancer disabled");
                try
                {
                    //Console.Out.WriteLine("Enabling Sharding on Database");
                    admindb.RunCommand <BsonDocument>(new BsonDocument("enableSharding", testOpts.namespaces[0]));
                }
                catch (Exception e)
                {
                    if (!e.Message.Contains("already enabled"))
                    {
                        logger.Info(e.Message);
                    }
                }


                try
                {
                    //Console.Out.WriteLine("Sharding Collection");
                    admindb.RunCommand <BsonDocument>(new BsonDocument("shardCollection",
                                                                       testOpts.namespaces[0] + "." + testOpts.namespaces[1]).Add("key", new BsonDocument("_id", 1)));
                }
                catch (Exception e)
                {
                    if (!e.Message.Contains("already"))
                    {
                        logger.Info(e.Message);
                    }
                }


                //See how many shards we have in the system - and get a list of their names
                //Console.Out.WriteLine("Counting Shards");
                IMongoCollection <BsonDocument> shards = configdb.GetCollection <BsonDocument>("shards");
                var shardc = shards.Find <BsonDocument>(new BsonDocument()).ToCursor();
                testOpts.numShards = 0;
                while (shardc.MoveNext())
                {
                    //Console.Out.WriteLine("Found a shard");
                    testOpts.numShards++;
                }

                //Console.Out.WriteLine("System has "+testOpts.numShards+" shards");
            }
        }