Ejemplo n.º 1
0
        /// <summary>
        /// Returns a random command object
        /// </summary>
        public DbCommand GetCommand(Random rnd, TableMetadata table, DataStressConnection conn, bool query, bool isXml = false)
        {
            if (query)
            {
                return(GetSelectCommand(rnd, table, conn, isXml));
            }
            else
            {
                // make sure arguments are correct
                DataStressErrors.Assert(!isXml, "wrong usage of GetCommand: cannot create command with FOR XML that is not query");

                int select = rnd.Next(4);
                switch (select)
                {
                case 0:
                    return(GetUpdateCommand(rnd, table, conn));

                case 1:
                    return(GetInsertCommand(rnd, table, conn));

                case 2:
                    return(GetDeleteCommand(rnd, table, conn));

                default:
                    return(GetSelectCommand(rnd, table, conn));
                }
            }
        }
Ejemplo n.º 2
0
        public virtual void GlobalTestSetup()
        {
            // Preconditions - ensure this setup is only called once
            DataStressErrors.Assert(string.IsNullOrEmpty(s_scenario), "Scenario was already set");
            DataStressErrors.Assert(s_source == null, "Source was already set");
            DataStressErrors.Assert(s_factory == null, "Factory was already set");

            // Set m_scenario
            string userProvidedScenario;

            TestMetrics.Overrides.TryGetValue("scenario", out userProvidedScenario);
            // Empty means default scenario for the test group
            s_scenario = (userProvidedScenario ?? string.Empty);
            s_scenario = s_scenario.ToUpperInvariant();

            // Set m_source
            // Empty means that test group will peek the default data source from the config file based on the scenario
            string userProvidedSourceName;

            if (TestMetrics.Overrides.TryGetValue("source", out userProvidedSourceName))
            {
                s_source = DataStressSettings.Instance.GetSourceByName(userProvidedSourceName);
            }

            // Set m_factory
            s_factory = CreateFactory(ref s_scenario, ref s_source);
            s_factory.InitializeSharedData(s_source);

            // Postconditions
            DataStressErrors.Assert(!string.IsNullOrEmpty(s_scenario), "Scenario was not set");
            DataStressErrors.Assert(s_source != null, "Source was not set");
            DataStressErrors.Assert(s_factory != null, "Factory was not set");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Kills the given connection using "kill [spid]" if the parameter is nonzero
        /// </summary>
        private void KillConnection()
        {
            DataStressErrors.Assert(_spid != 0, "Called KillConnection with spid != 0");

            using (var killerConn = DataTestGroup.Factory.CreateConnection())
            {
                killerConn.Open();

                using (var killerCmd = killerConn.CreateCommand())
                {
                    killerCmd.CommandText = "begin try kill " + _spid + " end try begin catch end catch";
                    killerCmd.ExecuteNonQuery();
                }
            }
        }
Ejemplo n.º 4
0
        protected virtual object GetRandomData(Random rnd, DbType dbType, int maxLength)
        {
            byte[] buffer;
            switch (dbType)
            {
            case DbType.Boolean:
                return(rnd.Next(2) == 0 ? false : true);

            case DbType.Byte:
                return(rnd.Next(byte.MinValue, byte.MaxValue + 1));

            case DbType.Int16:
                return(rnd.Next(short.MinValue, short.MaxValue + 1));

            case DbType.Int32:
                return(rnd.Next(2) == 0 ? int.MaxValue / rnd.Next(1, 3) : int.MinValue / rnd.Next(1, 3));

            case DbType.Int64:
                return(rnd.Next(2) == 0 ? long.MaxValue / rnd.Next(1, 3) : long.MinValue / rnd.Next(1, 3));

            case DbType.Single:
                return(rnd.NextDouble() * (rnd.Next(2) == 0 ? float.MaxValue : float.MinValue));

            case DbType.Double:
                return(rnd.NextDouble() * (rnd.Next(2) == 0 ? double.MaxValue : double.MinValue));

            case DbType.Decimal:
                return(rnd.Next(short.MinValue, short.MaxValue + 1));

            case DbType.DateTime:
            case DbType.DateTime2:
                return(DateTime.Now);

            case DbType.Date:
                return(DateTime.Now.Date);

            case DbType.Time:
                return(DateTime.Now.TimeOfDay.ToString("c"));

            case DbType.DateTimeOffset:
                return(DateTimeOffset.Now);

            case DbType.Guid:
                buffer = new byte[16];
                rnd.NextBytes(buffer);
                return(new Guid(buffer));

            case DbType.Object:
            case DbType.Binary:
                rnd.NextBytes(buffer = new byte[rnd.Next(1, maxLength)]);
                return(buffer);

            case DbType.String:
            case DbType.Xml:
                string openTag   = "<Data>";
                string closeTag  = "</Data>";
                int    tagLength = openTag.Length + closeTag.Length;

                if (tagLength > maxLength)
                {
                    // Case (1): tagLength > maxTargetLength
                    return("");
                }
                else
                {
                    StringBuilder builder = new StringBuilder(maxLength);

                    builder.Append(openTag);

                    // The data is just a repeat of one character because to the managed provider
                    // it is only really the length that matters, not the content of the data
                    char characterToUse = (char)rnd.Next((int)'@', (int)'~'); // Choosing random characters in this range to avoid special
                                                                              // xml chars like '<' or '&'
                    int numRepeats = rnd.Next(0, maxLength - tagLength);      // Case (2): tagLength == maxTargetLength
                                                                              // Case (3): tagLength < maxTargetLength <-- most common
                    builder.Append(characterToUse, numRepeats);

                    builder.Append(closeTag);

                    DataStressErrors.Assert(builder.Length <= maxLength, "Incorrect length of randomly generated string");

                    return(builder.ToString());
                }

            default:
                throw DataStressErrors.UnhandledCaseError(dbType);
            }
        }
Ejemplo n.º 5
0
 protected DataStressFactory(DbProviderFactory factory)
 {
     DataStressErrors.Assert(factory != null, "Argument to DataStressFactory constructor is null");
     this.DbFactory = factory;
 }