在测试docker下postgresql性能时, 发现的一个DNAT转换后的网络性能问题.

首先附上KVM,DOCKER,本机的几组测试数据(测试数据仅供参考, 有兴趣的朋友可以自己测试一下) : 


注意在测试连接超出物理核心数后, docker的性能开始严重下降. (可能有优化手段解决这个问题) 

TPS                      16连接IP,             16连接UNIX SOCK,  48连接UNIX SOCK,  96连接UNIX SOCK
docker DNAT               29662
docker                    174089.814292          257437.255033    265112.234836     180037.521722 
kvm                       58692.023756           62217.477740     166212.113952     162729.867472 
本机                      185341.577666          271232.160555    335530.912484     303621.978815

宿主机内核 : 


[root@db-172-16-3-221 ~]# uname -r
2.6.32-431.el6.x86_64


docker版本 : 


[root@db-172-16-3-221 ~]# docker info
Containers: 1
Images: 99
Storage Driver: devicemapper
 Pool Name: docker-8:49-17826869-pool
 Data file: /var/lib/docker/devicemapper/devicemapper/data
 Metadata file: /var/lib/docker/devicemapper/devicemapper/metadata
 Data Space Used: 1455.9 Mb
 Data Space Total: 102400.0 Mb
 Metadata Space Used: 3.4 Mb
 Metadata Space Total: 2048.0 Mb
Execution Driver: native-0.2
Kernel Version: 2.6.32-431.el6.x86_64


本地的images如下 : 


[root@db-172-16-3-221 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
postgres            9.4-beta3           68b6ddf9ca08        47 hours ago        213.6 MB
postgres            9.4                 68b6ddf9ca08        47 hours ago        213.6 MB
postgres            9                   935836384c52        47 hours ago        212.9 MB
postgres            latest              935836384c52        47 hours ago        212.9 MB
postgres            9.3                 935836384c52        47 hours ago        212.9 MB
postgres            9.3.5               935836384c52        47 hours ago        212.9 MB
postgres            9.2                 2a9ab0b9fa56        47 hours ago        212.7 MB
postgres            9.2.9               2a9ab0b9fa56        47 hours ago        212.7 MB
postgres            9.1.14              ee60947f6805        47 hours ago        212.1 MB
postgres            9.1                 ee60947f6805        47 hours ago        212.1 MB
postgres            9.0.18              f29be88283f6        47 hours ago        211.5 MB
postgres            9.0                 f29be88283f6        47 hours ago        211.5 MB
postgres            8                   c57c14beb696        47 hours ago        211 MB
postgres            8.4.22              c57c14beb696        47 hours ago        211 MB
postgres            8.4                 c57c14beb696        47 hours ago        211 MB
postgres            9.4-beta2           bf872395e1d4        2 weeks ago         213.6 MB


我们可以看看一个image的网络配置 :


[root@db-172-16-3-221 ~]# docker inspect postgres:9.3.5
[{
    "Architecture": "amd64",
    "Author": "",
    "Comment": "",
    "Config": {
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "postgres"
        ],
        "CpuShares": 0,
        "Cpuset": "",
        "Domainname": "",
        "Entrypoint": [
            "/docker-entrypoint.sh"
        ],
        "Env": [
            "PATH=/usr/lib/postgresql/9.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "LANG=en_US.utf8",
            "PG_MAJOR=9.3",
            "PG_VERSION=9.3.5-1.pgdg70+1",
            "PGDATA=/var/lib/postgresql/data"
        ],
        "ExposedPorts": {
            "5432/tcp": {}
        },
        "Hostname": "39d2c877bc4f",
        "Image": "15d042248df39ca896223c9e75f0f4541996f2b52030f1f8e6619cf18f77e395",
        "Memory": 0,
        "MemorySwap": 0,
        "NetworkDisabled": false,
        "OnBuild": [],
        "OpenStdin": false,
        "PortSpecs": null,
        "StdinOnce": false,
        "Tty": false,
        "User": "",
        "Volumes": {
            "/var/lib/postgresql/data": {}
        },
        "WorkingDir": ""
    },
    "Container": "977043cde52cd15e070d71a67fbd8e2ec908af49123f3f773f71cda2061e9f44",
    "ContainerConfig": {
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "/bin/sh",
            "-c",
            "#(nop) CMD [postgres]"
        ],
        "CpuShares": 0,
        "Cpuset": "",
        "Domainname": "",
        "Entrypoint": [
            "/docker-entrypoint.sh"
        ],
        "Env": [
            "PATH=/usr/lib/postgresql/9.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "LANG=en_US.utf8",
            "PG_MAJOR=9.3",
            "PG_VERSION=9.3.5-1.pgdg70+1",
            "PGDATA=/var/lib/postgresql/data"
        ],
        "ExposedPorts": {
            "5432/tcp": {}
        },
        "Hostname": "39d2c877bc4f",
        "Image": "15d042248df39ca896223c9e75f0f4541996f2b52030f1f8e6619cf18f77e395",
        "Memory": 0,
        "MemorySwap": 0,
        "NetworkDisabled": false,
        "OnBuild": [],
        "OpenStdin": false,
        "PortSpecs": null,
        "StdinOnce": false,
        "Tty": false,
        "User": "",
        "Volumes": {
            "/var/lib/postgresql/data": {}
        },
        "WorkingDir": ""
    },
    "Created": "2014-10-21T05:54:39.659381225Z",
    "DockerVersion": "1.2.0",
    "Id": "935836384c524aafc0ac1c05246002c9c93343f0b4283a34e77a5f92f97f9a7c",
    "Os": "linux",
    "Parent": "15d042248df39ca896223c9e75f0f4541996f2b52030f1f8e6619cf18f77e395",
    "Size": 0
}


启动一个container, 使用-P将端口映射出来.


[root@db-172-16-3-221 ~]# docker run -d -P postgres:9.3.5
[root@db-172-16-3-221 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                     NAMES
786d815d71b0        postgres:9          /docker-entrypoint.s   2 hours ago         Up 2 hours          0.0.0.0:49155->5432/tcp   suspicious_wright

这个时候会自动创建nat转换条目, 可以使用iptables-save命令查看, 如下有一台DNAT的规则是端口49155的转发 : 


[root@db-172-16-3-221 ~]# iptables-save
# Generated by iptables-save v1.4.7 on Thu Oct 23 14:00:48 2014
*nat
:PREROUTING ACCEPT [109:52442]
:POSTROUTING ACCEPT [90:4680]
:OUTPUT ACCEPT [90:4680]
:DOCKER - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER 
-A POSTROUTING -s 172.17.0.0/16 ! -d 172.17.0.0/16 -j MASQUERADE 
-A POSTROUTING -s 172.17.0.0/16 ! -d 172.17.0.0/16 -j MASQUERADE 
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER 
-A DOCKER ! -i docker0 -p tcp -m tcp --dport 49155 -j DNAT --to-destination 172.17.0.4:5432 
COMMIT
# Completed on Thu Oct 23 14:00:48 2014



同时查看这个container的网络, 也就是说, 外部可以通过宿主机的 49155  端口访问这个container的postgresql数据库.


宿主机本地则可以通过127.0.0.1: 49155  或 172.17.0.4:5432来访问.


问题来了 : 


通过nat访问的性能和直接访问的性能差太多了.


"NetworkSettings": {
        "Bridge": "docker0",
        "Gateway": "172.17.42.1",
        "IPAddress": "172.17.0.4",
        "IPPrefixLen": 16,
        "PortMapping": null,
        "Ports": {
            "5432/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "49155"
                }
            ]
        }


详细 : 


[root@db-172-16-3-221 ~]# docker inspect 786d815d71b0
[{
    "Args": [
        "postgres"
    ],
    "Config": {
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "postgres"
        ],
        "CpuShares": 0,
        "Cpuset": "",
        "Domainname": "",
        "Entrypoint": [
            "/docker-entrypoint.sh"
        ],
        "Env": [
            "PATH=/usr/lib/postgresql/9.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "LANG=en_US.utf8",
            "PG_MAJOR=9.3",
            "PG_VERSION=9.3.5-1.pgdg70+1",
            "PGDATA=/var/lib/postgresql/data"
        ],
        "ExposedPorts": {
            "5432/tcp": {}
        },
        "Hostname": "786d815d71b0",
        "Image": "postgres:9.3.5",
        "Memory": 0,
        "MemorySwap": 0,
        "NetworkDisabled": false,
        "OnBuild": null,
        "OpenStdin": false,
        "PortSpecs": null,
        "StdinOnce": false,
        "Tty": false,
        "User": "",
        "Volumes": {
            "/var/lib/postgresql/data": {}
        },
        "WorkingDir": ""
    },
    "Created": "2014-10-23T02:51:06.152941964Z",
    "Driver": "devicemapper",
    "ExecDriver": "native-0.2",
    "HostConfig": {
        "Binds": null,
        "ContainerIDFile": "",
        "Dns": null,
        "DnsSearch": null,
        "Links": null,
        "LxcConf": [],
        "NetworkMode": "bridge",
        "PortBindings": {},
        "Privileged": false,
        "PublishAllPorts": true,
        "VolumesFrom": null
    },
    "HostnamePath": "/var/lib/docker/containers/786d815d71b0dca10042e73842634483158811a9c54509e58d7a1d2afa6fd94d/hostname",
    "HostsPath": "/var/lib/docker/containers/786d815d71b0dca10042e73842634483158811a9c54509e58d7a1d2afa6fd94d/hosts",
    "Id": "786d815d71b0dca10042e73842634483158811a9c54509e58d7a1d2afa6fd94d",
    "Image": "935836384c524aafc0ac1c05246002c9c93343f0b4283a34e77a5f92f97f9a7c",
    "MountLabel": "",
    "Name": "/suspicious_wright",
    "NetworkSettings": {
        "Bridge": "docker0",
        "Gateway": "172.17.42.1",
        "IPAddress": "172.17.0.4",
        "IPPrefixLen": 16,
        "PortMapping": null,
        "Ports": {
            "5432/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "49155"
                }
            ]
        }
    },
    "Path": "/docker-entrypoint.sh",
    "ProcessLabel": "",
    "ResolvConfPath": "/etc/resolv.conf",
    "State": {
        "ExitCode": 0,
        "FinishedAt": "0001-01-01T00:00:00Z",
        "Paused": false,
        "Pid": 22251,
        "Running": true,
        "StartedAt": "2014-10-23T02:51:06.931436989Z"
    },
    "Volumes": {
        "/var/lib/postgresql/data": "/var/lib/docker/vfs/dir/8df8643eb4f255471b744a957a168fb9e1e57b1169ade6fba8b5f3a912681e88"
    },
    "VolumesRW": {
        "/var/lib/postgresql/data": true
    }
}

这个启动container后为container创建的接口.

# ifconfig
veth2d30  Link encap:Ethernet  HWaddr 42:9B:9F:1F:09:B0  
          inet6 addr: fe80::409b:9fff:fe1f:9b0/64 Scope:Link
          UP BROADCAST RUNNING  MTU:1500  Metric:1
          RX packets:6289977 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6290136 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:786242516 (749.8 MiB)  TX bytes:597549874 (569.8 MiB)
[root@db-172-16-3-221 ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
docker0         8000.429b9f1f09b0       no              veth2d30

测试如下, 执行select 1; : 

[root@db-172-16-3-221 ~]# cd
[root@db-172-16-3-221 ~]# psql -V
psql (PostgreSQL) 9.3.5
[root@db-172-16-3-221 ~]# vi test.sql
select 1;


通过NAT转换的性能 : 


[root@db-172-16-3-221 ~]# pgbench -M prepared -n -r -f ./test.sql -h 127.0.0.1 -p 49155 -U postgres -c 16 -j 4 -T 30 postgres
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 16
number of threads: 4
duration: 30 s
number of transactions actually processed: 889901
tps = 29662.517330 (including connections establishing)
tps = 29677.580497 (excluding connections establishing)
statement latencies in milliseconds:
        0.537902        select 1;



直接连接container访问的性能 : 



显然使用nat转换带来的性能损失非常大, TPS下降到了17%.



[root@db-172-16-3-221 ~]# pgbench -M prepared -n -r -f ./test.sql -h 172.17.0.4 -p 5432 -U postgres -c 16 -j 4 -T 30 postgres
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 16
number of threads: 4
duration: 30 s
number of transactions actually processed: 5222786
tps = 174089.814292 (including connections establishing)
tps = 174177.646184 (excluding connections establishing)
statement latencies in milliseconds:
        0.090595        select 1;


另外 , 附上直接访问宿主机本地的数据库的性能, 我们看到docker性能损失极少(约6%) : 


相比KVM虚拟化, CPU损耗方面, DOCKER的性能损耗确实非常低


[root@db-172-16-3-221 ~]# pgbench -M prepared -n -r -f ./test.sql -h 127.0.0.1 -p 5432 -U postgres -c 16 -j 4 -T 30 postgres
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 16
number of threads: 4
duration: 30 s
number of transactions actually processed: 5560383
tps = 185341.577666 (including connections establishing)
tps = 185435.133589 (excluding connections establishing)
statement latencies in milliseconds:
        0.084922        select 1;


在同一台宿主机下KVM虚拟机测试结果如下(kvm VCPU=48, 宿主机48核) : 


宿主机CPU : 


processor       : 47
vendor_id       : GenuineIntel
cpu family      : 6
model           : 46
model name      : Intel(R) Xeon(R) CPU           E7540  @ 2.00GHz
stepping        : 6
cpu MHz         : 1995.262
cache size      : 18432 KB
physical id     : 3
siblings        : 12
core id         : 11
cpu cores       : 6
apicid          : 119
initial apicid  : 119
fpu             : yes
fpu_exception   : yes
cpuid level     : 11
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm dca sse4_1 sse4_2 x2apic popcnt lahf_lm ida epb dts tpr_shadow vnmi flexpriority ept vpid
bogomips        : 3989.44
clflush size    : 64
cache_alignment : 64
address sizes   : 44 bits physical, 48 bits virtual
power management:

4SOCKET
6CORE
2TH


共48核心


虚拟机CPU(问题可能出自这里) : 


processor       : 47
vendor_id       : GenuineIntel
cpu family      : 6
model           : 26
model name      : Intel Core i7 9xx (Nehalem Class Core i7)
stepping        : 3
cpu MHz         : 1995.047
cache size      : 4096 KB
physical id     : 3
siblings        : 12
core id         : 5
cpu cores       : 6
apicid          : 59
initial apicid  : 59
fpu             : yes
fpu_exception   : yes
cpuid level     : 4
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx lm constant_tsc unfair_spinlock pni ssse3 cx16 sse4_1 sse4_2 x2apic popcnt hypervisor lahf_lm
bogomips        : 3990.09
clflush size    : 64
cache_alignment : 64
address sizes   : 44 bits physical, 48 bits virtual
power management:


KVM进程 : 


qemu     29307     1 55 15:57 ?        00:00:07 /usr/libexec/qemu-kvm -name test1 -S -M rhel6.5.0 -cpu Nehalem,+rdtscp,+x2apic,+dca,+pdcm,+xtpr,+tm2,+est,+vmx,+ds_cpl,+monitor,+dtes64,+pbe,+tm,+ht,+ss,+acpi,+ds,+vme -enable-kvm -m 8192 -realtime mlock=off -smp 48,sockets=4,cores=6,threads=2 -uuid 073dbc6f-f376-2c96-6323-e0cc488eda68 -nodefconfig -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/test1.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -no-shutdown -device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 -drive file=/var/lib/libvirt/images/test1.img,if=none,id=drive-virtio-disk0,format=raw,cache=none -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x5,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive if=none,media=cdrom,id=drive-ide0-1-0,readonly=on,format=raw -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0 -netdev tap,fd=21,id=hostnet0,vhost=on,vhostfd=22 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:d6:b2:07,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -device usb-tablet,id=input0 -vnc 127.0.0.1:0 -vga cirrus -device intel-hda,id=sound0,bus=pci.0,addr=0x4 -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x6


KVM性能(损耗68%) : 



[root@db-172-16-3-221 ~]# pgbench -M prepared -n -r -f ./test.sql -h 172.17.42.222 -p 5432 -U postgres -c 16 -j 4 -T 30 postgres
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 16
number of threads: 4
duration: 30 s
number of transactions actually processed: 1760790
tps = 58692.023756 (including connections establishing)
tps = 58743.201940 (excluding connections establishing)
statement latencies in milliseconds:
        0.271061        select 1;


计算密集型的话docker有明显优势.

最后再来一组unix socket的测试结果 : 


本机 : 


[root@db-172-16-3-221 ~]# pgbench -M prepared -n -r -f ./test.sql -h /data01/pgdata/pg_root -p 5432 -U postgres -c 16 -j 4 -T 30 postgres
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 16
number of threads: 4
duration: 30 s
number of transactions actually processed: 8137047
tps = 271232.160555 (including connections establishing)
tps = 271348.434446 (excluding connections establishing)
statement latencies in milliseconds:
        0.057746        select 1;




docker (性能损耗5%)


[root@db-172-16-3-221 ~]# docker run -t -i postgres:9.3.5 /bin/bash
root@48f8decd3ccb:/# more docker-entrypoint.sh 
#!/bin/bash
set -e

if [ "$1" = 'postgres' ]; then
        chown -R postgres "$PGDATA"

        if [ -z "$(ls -A "$PGDATA")" ]; then
                gosu postgres initdb

                sed -ri "s/^#(listen_addresses\s*=\s*)\S+/\1'*'/" "$PGDATA"/postgresql.conf

                { echo; echo 'host all all 0.0.0.0/0 trust'; } >> "$PGDATA"/pg_hba.conf

                if [ -d /docker-entrypoint-initdb.d ]; then
                        for f in /docker-entrypoint-initdb.d/*.sh; do
                                [ -f "$f" ] && . "$f"
                        done
                fi
        fi

        exec gosu postgres "$@"
fi

exec "$@"
root@48f8decd3ccb:/# ./docker-entrypoint.sh postgres
root@48f8decd3ccb:/# echo "select 1;" > ./test.sql
root@48f8decd3ccb:/# pgbench -M prepared -n -r -f ./test.sql -h /var/run/postgresql -p 5432 -U postgres -c 16 -j 4 -T 30 postgres   transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 16
number of threads: 4
duration: 30 s
number of transactions actually processed: 7723173
tps = 257437.255033 (including connections establishing)
tps = 257554.115083 (excluding connections establishing)
statement latencies in milliseconds:
        0.060943        select 1;




kvm (性能损耗77%)



postgres@localhost-> pgbench -M prepared -n -r -f ./test.sql -h /home/postgres/pgdata/pg_root -p 5432 -U postgres -c 16 -j 4 -T 30 postgres
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 16
number of threads: 4
duration: 30 s
number of transactions actually processed: 1866859
tps = 62217.477740 (including connections establishing)
tps = 62248.936343 (excluding connections establishing)
statement latencies in milliseconds:
        0.254873        select 1;



加大连接后, kvm有性能提升. 但是到顶也只能达到16.6W左右, 性能损失46.4%左右.


DOCKER的话, 超出核数后性能下降严重, 在达到物理核限制极限时, 损失12.7% 左右. 


KVM加大连接(CPU使用率1200%) : 


top - 17:54:56 up 43 days,  7:58,  3 users,  load average: 4.72, 1.43, 0.53
Tasks: 1115 total,   1 running, 1114 sleeping,   0 stopped,   0 zombie
Cpu(s): 24.9%us,  0.2%sy,  0.0%ni, 74.9%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  99045564k total, 13146680k used, 85898884k free,   272772k buffers
Swap:  1048568k total,        0k used,  1048568k free,  9688200k cached
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                
30008 qemu      20   0 11.8g 815m 5828 S 1204.4  0.8  20:56.29 /usr/libexec/qemu-kvm -name test1 -S -M rhel6.5.0 -cpu Nehalem,+rdtsc

postgres@localhost-> pgbench -M prepared -n -r -f ./test.sql -h /home/postgres/pgdata/pg_root -p 5432 -U postgres -c 48 -j 12 -T 30 postgres
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 48
number of threads: 12
duration: 30 s
number of transactions actually processed: 4988101
tps = 166212.113952 (including connections establishing)
tps = 166348.747712 (excluding connections establishing)
statement latencies in milliseconds:
        0.285856        select 1;

postgres@localhost-> pgbench -M prepared -n -r -f ./test.sql -h /home/postgres/pgdata/pg_root -p 5432 -U postgres -c 96 -j 12 -T 30 postgres
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 96
number of threads: 12
duration: 30 s
number of transactions actually processed: 4885569
tps = 162729.867472 (including connections establishing)
tps = 163017.799292 (excluding connections establishing)
statement latencies in milliseconds:
        0.585876        select 1;




本地加大连接 : 


[root@db-172-16-3-221 ~]# pgbench -M prepared -n -r -f ./test.sql -h /data01/pgdata/pg_root -p 5432 -U postgres -c 48 -j 12 -T 30 postgres
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 48
number of threads: 12
duration: 30 s
number of transactions actually processed: 10068364
tps = 335530.912484 (including connections establishing)
tps = 335757.764533 (excluding connections establishing)
statement latencies in milliseconds:
        0.141343        select 1;

[root@db-172-16-3-221 ~]# pgbench -M prepared -n -r -f ./test.sql -h /data01/pgdata/pg_root -p 5432 -U postgres -c 96 -j 12 -T 30 postgres
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 96
number of threads: 12
duration: 30 s
number of transactions actually processed: 9113013
tps = 303621.978815 (including connections establishing)
tps = 304050.332175 (excluding connections establishing)
statement latencies in milliseconds:
        0.313762        select 1;



docker加大连接 : 


root@48f8decd3ccb:/# pgbench -M prepared -n -r -f ./test.sql -h /var/run/postgresql -p 5432 -U postgres -c 96 -j 12 -T 30 postgres 
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 96
number of threads: 12
duration: 30 s
number of transactions actually processed: 5403085
tps = 180037.521722 (including connections establishing)
tps = 180248.827373 (excluding connections establishing)
statement latencies in milliseconds:
        0.527325        select 1;

root@48f8decd3ccb:/# pgbench -M prepared -n -r -f ./test.sql -h /var/run/postgresql -p 5432 -U postgres -c 40 -j 10 -T 30 postgres 
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 40
number of threads: 10
duration: 30 s
number of transactions actually processed: 7955103
tps = 265112.234836 (including connections establishing)
tps = 265278.332675 (excluding connections establishing)
statement latencies in milliseconds:
        0.147930        select 1;


[参考]

1.  http://docs.docker.com/userguide/