public void TestMinimal()
 {
     var info = new SystemProfileInfo
     {
         Timestamp = new DateTime(2011, 10, 7, 0, 0, 0, DateTimeKind.Utc),
         Duration = TimeSpan.FromMilliseconds(123)
     };
     var expected = "{ 'ts' : ISODate('2011-10-07T00:00:00Z'), 'millis' : 123.0 }".Replace("'", "\"");
     Assert.AreEqual(expected, info.ToJson());
 }
 public MongoProfileModel(SystemProfileInfo i)
 {
     Operation = i.Op;
     Namespace = i.Namespace;
     if (i.Command != null)
     {
         Command = i.Command.ToDictionary();
     }
     if (i.Query != null && i.Query.Count() > 0) {
         Query = i.Query.ToDictionary();
     }
     Error = i.Error;
     Timestamp = i.Timestamp;
 }
 private string GetCommand(SystemProfileInfo info)
 {
     if (info.Command != null)
     {
         return info.Command.ToString();
     }
     if (info.Query != null)
     {
         return info.Query.ToString();
     }
     if (info.UpdateObject != null)
     {
         return info.UpdateObject.ToString();
     }
     return string.Empty;
 }
 public void TestDeserializeAll()
 {
     var info = new SystemProfileInfo
     {
         Abbreviated = "abbreviated",
         Client = "client",
         Command = new BsonDocument("command", 1),
         CursorId = 1,
         Duration = TimeSpan.FromMilliseconds(2),
         Error = "err",
         Exception = "exception",
         ExceptionCode = 3,
         Exhaust = true,
         FastMod = true,
         FastModInsert = true,
         IdHack = true,
         Info = "info",
         KeyUpdates = 4,
         LockStatistics = new SystemProfileLockStatistics
         {
             TimeAcquiring = new SystemProfileReadWriteLockStatistics
             {
                 DatabaseReadLock = TimeSpan.FromMilliseconds(10),
                 DatabaseWriteLock = TimeSpan.FromMilliseconds(20),
                 GlobalReadLock = TimeSpan.FromMilliseconds(30),
                 GlobalWriteLock = TimeSpan.FromMilliseconds(40)
             },
             TimeLocked = new SystemProfileReadWriteLockStatistics
             {
                 DatabaseReadLock = TimeSpan.FromMilliseconds(50),
                 DatabaseWriteLock = TimeSpan.FromMilliseconds(60),
                 GlobalReadLock = TimeSpan.FromMilliseconds(70),
                 GlobalWriteLock = TimeSpan.FromMilliseconds(80)
             }
         },
         Moved = true,
         Namespace = "ns",
         NumberMoved = 11,
         NumberReturned = 5,
         NumberScanned = 6,
         NumberToReturn = 7,
         NumberToSkip = 8,
         NumberUpdated = 9,
         NumberOfYields = 10,
         Op = "op",
         Query = new BsonDocument("query", 1),
         ResponseLength = 9,
         ScanAndOrder = true,
         Timestamp = new DateTime(2011, 10, 7, 1, 2, 3, DateTimeKind.Utc),
         UpdateObject = new BsonDocument("updateObject", 1),
         Upsert = true,
         User = "******"
     };
     var json = info.ToJson(new JsonWriterSettings { Indent = true });
     var rehydrated = BsonSerializer.Deserialize<SystemProfileInfo>(json);
     Assert.AreEqual(info.Abbreviated, rehydrated.Abbreviated);
     Assert.AreEqual(info.Client, rehydrated.Client);
     Assert.AreEqual(info.Command, rehydrated.Command);
     Assert.AreEqual(info.CursorId, rehydrated.CursorId);
     Assert.AreEqual(info.Duration, rehydrated.Duration);
     Assert.AreEqual(info.Error, rehydrated.Error);
     Assert.AreEqual(info.Exception, rehydrated.Exception);
     Assert.AreEqual(info.ExceptionCode, rehydrated.ExceptionCode);
     Assert.AreEqual(info.Exhaust, rehydrated.Exhaust);
     Assert.AreEqual(info.FastMod, rehydrated.FastMod);
     Assert.AreEqual(info.FastModInsert, rehydrated.FastModInsert);
     Assert.AreEqual(info.IdHack, rehydrated.IdHack);
     Assert.AreEqual(info.Info, rehydrated.Info);
     Assert.AreEqual(info.KeyUpdates, rehydrated.KeyUpdates);
     Assert.AreEqual(info.LockStatistics.RawDocument, rehydrated.LockStatistics.RawDocument);
     Assert.AreEqual(info.LockStatistics.TimeAcquiring.DatabaseReadLock, rehydrated.LockStatistics.TimeAcquiring.DatabaseReadLock);
     Assert.AreEqual(info.LockStatistics.TimeAcquiring.DatabaseWriteLock, rehydrated.LockStatistics.TimeAcquiring.DatabaseWriteLock);
     Assert.AreEqual(info.LockStatistics.TimeAcquiring.GlobalReadLock, rehydrated.LockStatistics.TimeAcquiring.GlobalReadLock);
     Assert.AreEqual(info.LockStatistics.TimeAcquiring.GlobalWriteLock, rehydrated.LockStatistics.TimeAcquiring.GlobalWriteLock);
     Assert.AreEqual(info.LockStatistics.TimeLocked.DatabaseReadLock, rehydrated.LockStatistics.TimeLocked.DatabaseReadLock);
     Assert.AreEqual(info.LockStatistics.TimeLocked.DatabaseWriteLock, rehydrated.LockStatistics.TimeLocked.DatabaseWriteLock);
     Assert.AreEqual(info.LockStatistics.TimeLocked.GlobalReadLock, rehydrated.LockStatistics.TimeLocked.GlobalReadLock);
     Assert.AreEqual(info.LockStatistics.TimeLocked.GlobalWriteLock, rehydrated.LockStatistics.TimeLocked.GlobalWriteLock);
     Assert.AreEqual(info.Moved, rehydrated.Moved);
     Assert.AreEqual(info.Namespace, rehydrated.Namespace);
     Assert.AreEqual(info.NumberMoved, rehydrated.NumberMoved);
     Assert.AreEqual(info.NumberReturned, rehydrated.NumberReturned);
     Assert.AreEqual(info.NumberScanned, rehydrated.NumberScanned);
     Assert.AreEqual(info.NumberToReturn, rehydrated.NumberToReturn);
     Assert.AreEqual(info.NumberToSkip, rehydrated.NumberToSkip);
     Assert.AreEqual(info.NumberUpdated, rehydrated.NumberUpdated);
     Assert.AreEqual(info.NumberOfYields, rehydrated.NumberOfYields);
     Assert.AreEqual(info.Op, rehydrated.Op);
     Assert.AreEqual(info.Query, rehydrated.Query);
     Assert.AreEqual(info.ResponseLength, rehydrated.ResponseLength);
     Assert.AreEqual(info.ScanAndOrder, rehydrated.ScanAndOrder);
     Assert.AreEqual(info.Timestamp, rehydrated.Timestamp);
     Assert.AreEqual(info.UpdateObject, rehydrated.UpdateObject);
     Assert.AreEqual(info.Upsert, rehydrated.Upsert);
     Assert.AreEqual(info.User, rehydrated.User);
 }
Example #5
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(SystemProfileInfo));

            if (bsonReader.GetCurrentBsonType() == Bson.BsonType.Null)
            {
                bsonReader.ReadNull();
                return null;
            }
            else
            {
                var profileInfo = new SystemProfileInfo();

                bsonReader.ReadStartDocument();
                BsonType bsonType;
                while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument)
                {
                    var name = bsonReader.ReadName();
                    switch (name)
                    {
                        case "abbreviated":
                            profileInfo.Abbreviated = bsonReader.ReadString();
                            break;
                        case "client":
                            profileInfo.Client = bsonReader.ReadString();
                            break;
                        case "command":
                            profileInfo.Command = BsonDocument.ReadFrom(bsonReader);
                            break;
                        case "cursorid":
                            profileInfo.CursorId = BsonValue.ReadFrom(bsonReader).ToInt64();
                            break;
                        case "err":
                            profileInfo.Error = bsonReader.ReadString();
                            break;
                        case "exception":
                            profileInfo.Exception = bsonReader.ReadString();
                            break;
                        case "exceptionCode":
                            profileInfo.ExceptionCode = BsonValue.ReadFrom(bsonReader).ToInt32();
                            break;
                        case "exhaust":
                            profileInfo.Exhaust = BsonValue.ReadFrom(bsonReader).ToBoolean();
                            break;
                        case "fastmod":
                            profileInfo.FastMod = BsonValue.ReadFrom(bsonReader).ToBoolean();
                            break;
                        case "fastmodinsert":
                            profileInfo.FastModInsert = BsonValue.ReadFrom(bsonReader).ToBoolean();
                            break;
                        case "idhack":
                            profileInfo.IdHack = BsonValue.ReadFrom(bsonReader).ToBoolean();
                            break;
                        case "info":
                            profileInfo.Info = bsonReader.ReadString();
                            break;
                        case "keyUpdates":
                            profileInfo.KeyUpdates = BsonValue.ReadFrom(bsonReader).ToInt32();
                            break;
                        case "millis":
                            profileInfo.Duration = TimeSpan.FromMilliseconds(BsonValue.ReadFrom(bsonReader).ToDouble());
                            break;
                        case "moved":
                            profileInfo.Moved = BsonValue.ReadFrom(bsonReader).ToBoolean();
                            break;
                        case "nreturned":
                            profileInfo.NumberReturned = BsonValue.ReadFrom(bsonReader).ToInt32();
                            break;
                        case "ns":
                            profileInfo.Namespace = bsonReader.ReadString();
                            break;
                        case "nscanned":
                            profileInfo.NumberScanned = BsonValue.ReadFrom(bsonReader).ToInt32();
                            break;
                        case "ntoreturn":
                            profileInfo.NumberToReturn = BsonValue.ReadFrom(bsonReader).ToInt32();
                            break;
                        case "ntoskip":
                            profileInfo.NumberToSkip = BsonValue.ReadFrom(bsonReader).ToInt32();
                            break;
                        case "op":
                            profileInfo.Op = bsonReader.ReadString();
                            break;
                        case "query":
                            profileInfo.Query = BsonDocument.ReadFrom(bsonReader);
                            break;
                        case "responseLength":
                            profileInfo.ResponseLength = BsonValue.ReadFrom(bsonReader).ToInt32();
                            break;
                        case "scanAndOrder":
                            profileInfo.ScanAndOrder = BsonValue.ReadFrom(bsonReader).ToBoolean();
                            break;
                        case "ts":
                            profileInfo.Timestamp = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime());
                            break;
                        case "updateobj":
                            profileInfo.UpdateObject = BsonDocument.ReadFrom(bsonReader);
                            break;
                        case "upsert":
                            profileInfo.Upsert = BsonValue.ReadFrom(bsonReader).ToBoolean();
                            break;
                        case "user":
                            profileInfo.User = bsonReader.ReadString();
                            break;
                        default:
                            bsonReader.SkipValue(); // ignore unknown elements
                            break;
                    }
                }
                bsonReader.ReadEndDocument();

                return profileInfo;
            }
        }
Example #6
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(SystemProfileInfo));

            if (bsonReader.GetCurrentBsonType() == Bson.BsonType.Null)
            {
                bsonReader.ReadNull();
                return(null);
            }
            else
            {
                var profileInfo = new SystemProfileInfo();

                bsonReader.ReadStartDocument();
                BsonType bsonType;
                while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument)
                {
                    var name = bsonReader.ReadName();
                    switch (name)
                    {
                    case "abbreviated":
                        profileInfo.Abbreviated = bsonReader.ReadString();
                        break;

                    case "client":
                        profileInfo.Client = bsonReader.ReadString();
                        break;

                    case "command":
                        profileInfo.Command = BsonDocument.ReadFrom(bsonReader);
                        break;

                    case "cursorid":
                        profileInfo.CursorId = BsonValue.ReadFrom(bsonReader).ToInt64();
                        break;

                    case "err":
                        profileInfo.Error = bsonReader.ReadString();
                        break;

                    case "exception":
                        profileInfo.Exception = bsonReader.ReadString();
                        break;

                    case "exceptionCode":
                        profileInfo.ExceptionCode = BsonValue.ReadFrom(bsonReader).ToInt32();
                        break;

                    case "exhaust":
                        profileInfo.Exhaust = BsonValue.ReadFrom(bsonReader).ToBoolean();
                        break;

                    case "fastmod":
                        profileInfo.FastMod = BsonValue.ReadFrom(bsonReader).ToBoolean();
                        break;

                    case "fastmodinsert":
                        profileInfo.FastModInsert = BsonValue.ReadFrom(bsonReader).ToBoolean();
                        break;

                    case "idhack":
                        profileInfo.IdHack = BsonValue.ReadFrom(bsonReader).ToBoolean();
                        break;

                    case "info":
                        profileInfo.Info = bsonReader.ReadString();
                        break;

                    case "keyUpdates":
                        profileInfo.KeyUpdates = BsonValue.ReadFrom(bsonReader).ToInt32();
                        break;

                    case "millis":
                        profileInfo.Duration = TimeSpan.FromMilliseconds(BsonValue.ReadFrom(bsonReader).ToDouble());
                        break;

                    case "moved":
                        profileInfo.Moved = BsonValue.ReadFrom(bsonReader).ToBoolean();
                        break;

                    case "nreturned":
                        profileInfo.NumberReturned = BsonValue.ReadFrom(bsonReader).ToInt32();
                        break;

                    case "ns":
                        profileInfo.Namespace = bsonReader.ReadString();
                        break;

                    case "nscanned":
                        profileInfo.NumberScanned = BsonValue.ReadFrom(bsonReader).ToInt32();
                        break;

                    case "ntoreturn":
                        profileInfo.NumberToReturn = BsonValue.ReadFrom(bsonReader).ToInt32();
                        break;

                    case "ntoskip":
                        profileInfo.NumberToSkip = BsonValue.ReadFrom(bsonReader).ToInt32();
                        break;

                    case "op":
                        profileInfo.Op = bsonReader.ReadString();
                        break;

                    case "query":
                        profileInfo.Query = BsonDocument.ReadFrom(bsonReader);
                        break;

                    case "responseLength":
                        profileInfo.ResponseLength = BsonValue.ReadFrom(bsonReader).ToInt32();
                        break;

                    case "scanAndOrder":
                        profileInfo.ScanAndOrder = BsonValue.ReadFrom(bsonReader).ToBoolean();
                        break;

                    case "ts":
                        profileInfo.Timestamp = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime());
                        break;

                    case "updateobj":
                        profileInfo.UpdateObject = BsonDocument.ReadFrom(bsonReader);
                        break;

                    case "upsert":
                        profileInfo.Upsert = BsonValue.ReadFrom(bsonReader).ToBoolean();
                        break;

                    case "user":
                        profileInfo.User = bsonReader.ReadString();
                        break;

                    default:
                        break;     // ignore unknown elements
                    }
                }
                bsonReader.ReadEndDocument();

                return(profileInfo);
            }
        }