剛剛寫完(一),本來想把兩篇寫在一起,但是發現關聯不大,最後覺得分開寫:
本文來自:http://blog.csdn.net/lengzijian/article/details/7729465
先寫上數據庫配置:
一個主數據庫,兩個從數據庫
主數據庫:Intel(R) Pentium(R) D CPU 2.80GHz *2 | MemTotal: 1027072 kB
從庫1 :Pentium(R) Dual-Core CPU E5200 @ 2.50GHz| MemTotal: 2066020 kB
從庫2 :Pentium(R) Dual-Core CPU E5200 @ 2.50GHz| MemTotal: 2066020 kB
本人自己寫了c語言的測試腳本,用到了libpq c庫,這裏附上源碼:
#include <stdio.h>
#include <libpq-fe.h>
#include <pthread.h>
#include <sys/time.h>
/*
首先創建一個簡單的表只有userid和name
CREATE TABLE t_user
(
userid character varying(25),
name character varying(25)
)
*/
void thread(char *count){
int i,j;
const char *conninfo;
char char_time[1024];
PGresult *res;
PGconn *conn;
j = atoi(count);
struct timeval tpstart,tpend;
float timeuser;
gettimeofday(&tpstart,NULL);
//數據庫配置(主機地址,用戶名,端口,數據庫名)
conninfo = "hostaddr=192.168.30.150 user=postgres port=5433 dbname=lengzijian";
conn = PQconnectdb(conninfo);
for(i = 0;i<j;i++){
sprintf(char_time,"insert into t_user values ('%d','lengzijian')",i);
res = PQexec(conn, char_time);
if(!res||PQresultStatus(res)!=PGRES_COMMAND_OK)
{
printf("insert failed:%s--[%d]\n",PQerrorMessage(conn),i);
PQclear(res);
}
PQclear(res);
}
gettimeofday(&tpend,NULL);
timeuser=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
timeuser /=1000000;
//輸出每個線程的運行時間,方便計算平均值
printf("Used Time:%f\n",timeuser);
PQfinish(conn);
}
//第一個變量表示線程數,第二個參數表示併發量(每個線程的插入測試)
int main(int argc,char *argv[])
{
int i, j;
j = atoi(argv[1]);
pthread_t id[j];
int ret;
if(argc < 3){
printf("Not enough arguments, exiting...");
return -1;
}
for(i=0;i<j;i++)
ret=pthread_create(&id[i],NULL,(void *) thread,argv[2]);
/* 關閉數據庫連接並清理 */
for(i=0;i<j;i++)
pthread_join(id[i],NULL);
}
編譯方法:
gcc test.c -lpq #千萬別忘了-lpq
使用方法:
./a.out 1 1000 #表示1個線程1000個插入操作
由於本人機器並非服務器,所以數據只用來參考,希望讀者可以用上面的代碼,測試服務器上主機後,發送給本人,本人不勝感激!!!!
直接上數據:
線程數量 |
每個線程插入次數 |
總時間(s) |
每秒處理次數 |
1 |
1000 |
0.684 |
1461.988304 |
1 |
10000 |
6.56 |
1524.390244 |
1 |
50000 |
33.008 |
1514.784295 |
1 |
100000 |
76.972 |
1299.173726 |
2 |
1000 |
0.83 |
2409.638554 |
2 |
10000 |
7.96 |
2512.562814 |
2 |
50000 |
41 |
2439.02439 |
2 |
100000 |
95.675 |
2090.410243 |
3 |
1000 |
1.15 |
2608.695652 |
3 |
10000 |
11.99 |
2502.085071 |
3 |
50000 |
70.98 |
2113.271344 |
3 |
100000 |
163.4 |
1835.985312 |
5 |
1000 |
1.22 |
4098.360656 |
5 |
10000 |
15.9 |
3144.654088 |
5 |
50000 |
82.9 |
3015.681544 |
5 |
100000 |
118.4034 |
4222.852386 |
10 |
1000 |
1.830302 |
5463.579234 |
10 |
10000 |
16.94417 |
5901.736326 |
10 |
50000 |
87.61617 |
5706.709487 |
10 |
100000 |
179.2664 |
5578.290561 |