zoukankan      html  css  js  c++  java
  • PostgreSQL On Windows Process Connection Performance

    本文主要对PostgreSql在Windows下的连接测试。

    测试环境: Win7 x64, PostgreSql 10.1 x64 

    测试语言: VS2015 C#  

    因为Pg的数据库连接是开启进程来处理的,不像MySQL一样是线程处理。在Windows下开启进程是比较耗时耗资源,不像Linux开启进程。

    所以,我做了个测试:循环开启200个线程分别去连接Pg,开启连接后不主动关闭;并且让线程等待300秒也不去主动关闭,以确认Pg开启的进程数。

    测试结果: Pg进程数并不会随着连接数的增加而增加,而是保持一定数量不变。 

    而后,又修改代码,去掉多线程连接数据库,只用主线程连接,还是开200个连接,结果: Pg进程只增加了1,并保持不变。 

    注:正常情况下,什么都不做,系统会保持8个常驻进程。

    以下是测试截图和测试代码:

     1 using System;
     2 using System.Threading;
     3 using Pg.Model;
     4 
     5 namespace PgConnectTest
     6 {
     7     class Program
     8     {
     9         static void Main(string[] args)
    10         {
    11             Console.WriteLine("正在测试连接进程...200个进程");
    12             int proCount = 1;
    13             while (proCount <= 200)
    14             {
    15                 Console.WriteLine($"开启第{proCount}个线程连接Postgres...");
    16                 ThreadPool.QueueUserWorkItem(state =>
    17                 {
    18                     var db = new MesDbContext();
    19                     db.Users.Add(new User() {Account = "001", Passwd = "123456", Label = "shanghai"});
    20                     db.SaveChanges();
    21                     Thread.Sleep(300000);
    22                 });
    23                 proCount++;
    24                 Thread.Sleep(1000);
    25             }
    26         }
    27     }
    28 }
     1 using System;
     2 using System.ComponentModel.DataAnnotations;
     3 using System.ComponentModel.DataAnnotations.Schema;
     4 
     5 namespace Pg.Model
     6 {
     7     public class Model
     8     {
     9         [Key]
    10         [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    11         public long Id { get; set; }
    12         public DateTime CreTime { get; set; } = DateTime.Now;
    13         public long? CreUid { get; set; }
    14         public DateTime? UpdTime { get; set; }
    15         public long? UpdUid { get; set; }
    16         public string Remark { get; set; }
    17         public bool IsDel { get; set; } = false;
    18     }
    19 
    20     [Table("User")]
    21     public class User : Model
    22     {
    23         //[Index(IsUnique = true)]
    24         public string Account { get; set; }
    25         public string Passwd { get; set; }
    26         public string Label { get; set; }
    27     }
    28 
    29 
    30 }
     1 using System.Data.Entity;
     2 using System.Data.Entity.Migrations;
     3 
     4 namespace Pg.Model
     5 {
     6     public class MesDbContext: DbContext
     7     {
     8         public MesDbContext():base("name=mes")
     9         {
    10             Database.SetInitializer(new MigrateDatabaseToLatestVersion<MesDbContext, MigrateConfig>());
    11         }
    12 
    13         public DbSet<User> Users { get; set; }
    14     }
    15 
    16     public class MigrateConfig : DbMigrationsConfiguration<MesDbContext>
    17     {
    18         public MigrateConfig()
    19         {
    20             AutomaticMigrationsEnabled = true;
    21             AutomaticMigrationDataLossAllowed = true;
    22         }
    23     }
    24 
    25 }

    ========= 2017.11.19 以下===============

    又经过推敲,其实跟写的代码有关系,

    如果是用framework的线程池 ThreadPool.QueueUserWorkItem(state => { }); ,其实不会连续开启很多线程,差不多我测试的结果是开启7个线程。

    如果是换作 Task.Run(() => { }); 任务,那么情况差不多,Pg进程开启的数量也是10几个。

    但是,如果是直接 new Thread(() => {}) 方式,那么甚至会开启上百个Pg进程

    极端,如果是只使用主线程,那么只会开启一个Pg进程。

  • 相关阅读:
    〖Linux〗Kubuntu设置打开应用时就只在打开时的工作区显示
    〖Linux〗Kubuntu, the application 'Google Chrome' has requested to open the wallet 'kdewallet'解决方法
    unity, dll is not allowed to be included or could not be found
    android check box 自定义图片
    unity, ios skin crash
    unity, Collider2D.bounds的一个坑
    unity, ContentSizeFitter立即生效
    类里的通用成员函数应声明为static
    unity, Gizmos.DrawMesh一个坑
    直线切割凹多边形
  • 原文地址:https://www.cnblogs.com/jonney-wang/p/7846697.html
Copyright © 2011-2022 走看看