Example #1
0
 public void ExecuteScalar()
 {
     using (var conn = OpenConnection())
         using (var metrics = TestMetrics.Start(TestRunTime, true))
         {
             while (!metrics.TimesUp)
             {
                 conn.ExecuteScalar("SELECT 1 + 1");
                 metrics.IncrementIterations();
             }
         }
 }
Example #2
0
 public void ExecuteUpdateNonQuery()
 {
     using (var conn = OpenConnection())
         using (var metrics = TestMetrics.Start(TestRunTime, true))
         {
             while (!metrics.TimesUp)
             {
                 conn.ExecuteNonQuery("set lock_timeout = 1000");
                 metrics.IncrementIterations();
             }
         }
 }
Example #3
0
        public async void ExecuteScalarAsync()
        {
            using (var metrics = TestMetrics.Start(TestRunTime, true))
            {
                while (!metrics.TimesUp)
                {
                    await ExecuteScalarAsync("SELECT 1 + 1");

                    metrics.IncrementIterations();
                }
            }
        }
Example #4
0
        public async void ExecuteUpdateNonQueryAsync()
        {
            using (var metrics = TestMetrics.Start(TestRunTime, true))
            {
                while (!metrics.TimesUp)
                {
                    await ExecuteNonQueryAsync("set lock_timeout = 1000");

                    metrics.IncrementIterations();
                }
            }
        }
Example #5
0
        public void ParameterizedSelectByteaArrayRoundTrip()
        {
            using (var conn = OpenConnection())
                using (var command = conn.CreateCommand())
                {
                    command.CommandType = CommandType.Text;
                    command.CommandText = "SELECT :data";

                    byte[] bytes = new byte[50000];
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        bytes[i] = (byte)(i % 255);
                    }
                    byte[][] data = new byte[][] { bytes, bytes };

                    NpgsqlParameter dataParameter = command.CreateParameter();
                    dataParameter.Direction     = ParameterDirection.Input;
                    dataParameter.NpgsqlDbType  = NpgsqlTypes.NpgsqlDbType.Bytea | NpgsqlTypes.NpgsqlDbType.Array;
                    dataParameter.ParameterName = "data";
                    command.Parameters.Add(dataParameter);
                    dataParameter.Value = data;

                    command.Prepare();

                    using (var metrics = TestMetrics.Start(TestRunTime, true))
                    {
                        while (!metrics.TimesUp)
                        {
                            try
                            {
                                command.ExecuteScalar();
                            }
                            catch (PostgresException e)
                            {
                                if (e.Message.Length > 500)
                                {
                                    Console.WriteLine(string.Format("Error: {0}", e.Message.Substring(0, 100)));
                                    throw new Exception(e.Message.Substring(0, 500));
                                }
                                else
                                {
                                    throw;
                                }
                            }
                            metrics.IncrementIterations();
                        }
                    }
                }
        }
Example #6
0
        public void ParameterizedSelectArrayRoundTrip()
        {
            using (var command = Conn.CreateCommand())
            {
                command.CommandType = CommandType.Text;
                command.CommandText = "SELECT :data";

                string[] data = new string[1000];

                for (int i = 0; i < 1000; i++)
                {
                    data[i] = string.Format("A string with the number {0}, a ', a \", and a \\.", i);
                }

                NpgsqlParameter dataParameter = command.CreateParameter();
                dataParameter.Direction     = ParameterDirection.Input;
                dataParameter.NpgsqlDbType  = NpgsqlTypes.NpgsqlDbType.Text | NpgsqlTypes.NpgsqlDbType.Array;
                dataParameter.ParameterName = "data";
                command.Parameters.Add(dataParameter);
                dataParameter.Value = data;

                command.Prepare();

                using (var metrics = TestMetrics.Start(TestRunTime, true))
                {
                    while (!metrics.TimesUp)
                    {
                        try
                        {
                            command.ExecuteScalar();
                        }
                        catch (NpgsqlException e)
                        {
                            if (e.Message.Length > 500)
                            {
                                Console.WriteLine(string.Format("Error: {0}", e.Message.Substring(0, 100)));
                                throw new Exception(e.Message.Substring(0, 500));
                            }
                            else
                            {
                                throw;
                            }
                        }
                        metrics.IncrementIterations();
                    }
                }
            }
        }
Example #7
0
        public void ConnectWithPool(bool withPool)
        {
            NpgsqlConnectionStringBuilder csb = new NpgsqlConnectionStringBuilder(Conn.ConnectionString);

            csb.Pooling = withPool;
            String conStr = csb.ConnectionString;

            using (var metrics = TestMetrics.Start(TestRunTime, true))
            {
                while (!metrics.TimesUp)
                {
                    var con = new NpgsqlConnection(conStr);
                    con.Open();
                    con.Dispose();
                    metrics.IncrementIterations();
                }
            }
        }
Example #8
0
 public void ExecuteReader(int rows)
 {
     for (var i = 0; i < rows; i++)
     {
         ExecuteNonQuery("INSERT INTO DATA (field_int4) VALUES (10)");
     }
     using (var metrics = TestMetrics.Start(TestRunTime, true))
     {
         while (!metrics.TimesUp)
         {
             using (var cmd = new NpgsqlCommand("SELECT field_int4 FROM data", Conn))
                 using (var reader = cmd.ExecuteReader()) {
                     while (reader.Read())
                     {
                     }
                 }
             metrics.IncrementIterations();
         }
     }
 }
Example #9
0
 public async void ExecuteReaderAsync(int rows)
 {
     using (var conn = OpenConnection())
     {
         for (var i = 0; i < rows; i++)
         {
             conn.ExecuteNonQuery("INSERT INTO DATA (field_int4) VALUES (10)");
         }
         using (var metrics = TestMetrics.Start(TestRunTime, true))
         {
             while (!metrics.TimesUp)
             {
                 using (var cmd = new NpgsqlCommand("SELECT field_int4 FROM data", conn))
                     using (var reader = await cmd.ExecuteReaderAsync())
                         while (await reader.ReadAsync())
                         {
                         }
                 metrics.IncrementIterations();
             }
         }
     }
 }