[TOC] # 多线程RTT(单次来回请求响应占用的时间) ```C #include <stdio.h> #include <pthread.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <unistd.h> #include <sys/time.h> #include <signal.h> #include <errno.h> #define MAX_PTHREADS 10000 typedef struct t_node { pthread_t pid; double span; }T_NODE,*PT_NODE; extern void* threadFun(void* arg); extern void testapp(unsigned long int pid); static void app_exit(int signum); pthread_t pid[MAX_PTHREADS]; int pid_num = 0; int main(int argc, const char * argv[]) { struct sigaction sa_exit; sa_exit.sa_flags = 0; sa_exit.sa_handler = app_exit; //信号处理函数 sigaction(SIGINT, &sa_exit, NULL); sigaction(SIGSTOP, &sa_exit, NULL); pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); printf("input thread nums:(quit:\"-1\" num:\"1~%d\"):",MAX_PTHREADS); scanf("%d", &pid_num); setbuf(stdin, NULL); if(pid_num == -1) { return 0; } int i = 0; for (i = 0;i < pid_num;i++) { pid[i] = -1; } double total_time = 0.0; for(i = 0;i < pid_num;i++) { pthread_create(&pid[i], NULL, threadFun, NULL); } double *retVal = NULL; for(i = 0;i < pid_num;i++) { pthread_join(pid[i], (void **)&retVal); total_time += *retVal; } printf("average RTT(%d tests):%lf\n",pid_num,(total_time/pid_num)); free(retVal); return 0; } void* threadFun(void* arg) { struct timeval tvs,tve; //开始计数 gettimeofday(&tvs,NULL); //调用app testapp(pthread_self()); //结束计数 gettimeofday(&tve,NULL); double *span = (double *)malloc(1*sizeof(double)); *span = tve.tv_sec-tvs.tv_sec + (tve.tv_usec-tvs.tv_usec)/1000000.0; return ((void*)span); } void testapp(unsigned long int pid) { int ret; char mac[18]; int i = 100000; while(i--) return ; } static void app_exit(int signum) { if (signum == SIGINT||signum == SIGSTOP) { printf("\nSIGINT received\n"); int i; for(i = 0;i < pid_num;i++) { if(pid[i] == -1) continue; pthread_cancel(pid[i]); } exit(EXIT_SUCCESS); } else { printf("\nother %d received\n", signum); } } ``` # 单线程TPS(单线程每秒钟能够执行请求响应多少次,单线程上通过循环执行任务队列) ```C #include <stdio.h> #include <pthread.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <unistd.h> #include <sys/time.h> #include <signal.h> #include <errno.h> #define MAX_SECONDS 20 extern double threadFun(void); extern void signal_handle(int signal); extern void set_timer(); extern void testapp(); static int count = 0; int main(int argc, const char * argv[]) { double total_item = 0; int seconds = 0; printf("Please enter the number of seconds:(quit:\"-1\" seconds:\"1~%d\"):",MAX_SECONDS); scanf("%d", &seconds); setbuf(stdin, NULL); if(seconds <-1||seconds >MAX_SECONDS) { printf("input error!\n"); exit(EXIT_FAILURE); }else if(seconds == -1){ exit(EXIT_SUCCESS); } signal(SIGALRM,signal_handle); set_timer(); while(count < seconds) { threadFun(); total_item++; } printf("average tps(%d seconds):%lf\n",seconds,(total_item/seconds)); return 0; } double threadFun(void) { struct timeval tvs,tve; //开始计数 gettimeofday(&tvs,NULL); //测试函数 testapp(); //结束计数 gettimeofday(&tve,NULL); double span; span = tve.tv_sec-tvs.tv_sec + (tve.tv_usec-tvs.tv_usec)/1000000.0; return (span); } void testapp() { int ret; char mac[64]; int i = 100000; while(i--) return ; } void signal_handle(int signal) { count++; printf("count:%d\n",count); } void set_timer() { struct itimerval itv; itv.it_interval.tv_sec = 1; itv.it_interval.tv_usec= 0; itv.it_value.tv_sec= 1; itv.it_value.tv_usec=0; setitimer(ITIMER_REAL,&itv,NULL); } ``` # 多线程TPS(多线程每秒钟能够执行请求响应多少次,每个线程只有1个任务队列) ```C #include <stdio.h> #include <pthread.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <unistd.h> #include <sys/time.h> #include <signal.h> #include <errno.h> #define MAX_PTHREADS 1000000 typedef struct t_node { pthread_t pid; double span; }T_NODE,*PT_NODE; extern void* threadFun(void* arg); extern void testapp(unsigned long int pid); static void app_exit(int signum); pthread_t pid[MAX_PTHREADS]; int pid_num = 0; int main(int argc, const char * argv[]) { struct sigaction sa_exit; sa_exit.sa_flags = 0; sa_exit.sa_handler = app_exit; //信号处理函数 sigaction(SIGINT, &sa_exit, NULL); sigaction(SIGSTOP, &sa_exit, NULL); pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); printf("input items:(quit:\"-1\" items:\"1~%d\"):",MAX_PTHREADS); scanf("%d", &pid_num); setbuf(stdin, NULL); if(pid_num == -1) { return 0; } int i = 0; long int total_item = 0; double total_time = 0.0; for(i = 0;i < pid_num;i++) { pthread_create(&pid[i], NULL, threadFun, NULL); } double *retVal = NULL; for(i = 0;i < pid_num;i++) { pthread_join(pid[i], (void **)&retVal); total_time += *retVal; total_item += 1; } printf("average TPS(%ld tests):%lf\n",total_item,(double)(total_item/total_time)); free(retVal); return 0; } void* threadFun(void* arg) { struct timeval tvs,tve; //开始计数 gettimeofday(&tvs,NULL); //调用app testapp(pthread_self()); //结束计数 gettimeofday(&tve,NULL); double *span = (double *)malloc(1*sizeof(double)); *span = tve.tv_sec-tvs.tv_sec + (tve.tv_usec-tvs.tv_usec)/1000000.0; return ((void*)span); } void testapp(unsigned long int pid) { int ret; char mac[18]; int i = 100000; while(i--) return ; } static void app_exit(int signum) { if (signum == SIGINT||signum == SIGSTOP) { printf("\nSIGINT received\n"); int i; for(i = 0;i < pid_num;i++) { pthread_cancel(pid[pid_num]); } exit(EXIT_SUCCESS); } else { printf("\nother %d received\n", signum); } } ``` # 多线程多队列TPS(多线程多队列每秒钟能够执行请求响应多少次,每个线程都有多个任务队列) ```C #include <stdio.h> #include <pthread.h> #include <string.h> #include <time.h> #include <stdlib.h> #include <unistd.h> #include <sys/time.h> #include <signal.h> #include <errno.h> /************* 多线程,每个线程都是多任务 ************/ #define MAX_PTHREADS 5000 typedef struct t_node { pthread_t pid; double span; }T_NODE,*PT_NODE; extern void* threadFun(void* arg); extern void testapp(int n); static void app_exit(int signum); pthread_t pid[MAX_PTHREADS]; int pid_num = 0; int s_jobs = 0; int main(int argc, const char * argv[]) { struct sigaction sa_exit; sa_exit.sa_flags = 0; sa_exit.sa_handler = app_exit; //信号处理函数 sigaction(SIGINT, &sa_exit, NULL); sigaction(SIGSTOP, &sa_exit, NULL); pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); printf("input threads:(quit:\"-1\" items:\"1~%d\"):",MAX_PTHREADS); scanf("%d", &pid_num); setbuf(stdin, NULL); printf("input jobs:(quit:\"-1\" jobs:\"1~5000\"):"); scanf("%d", &s_jobs); if(pid_num == -1) { return 0; } int i = 0; for(i = 0;i<pid_num;i++) { pid[i] = -1; } long int total_item = 0; double total_time = 0.0; for(i = 0;i < pid_num;i++) { pthread_create(&pid[i], NULL, threadFun,&s_jobs ); } double *retVal = NULL; for(i = 0;i < pid_num;i++) { pthread_join(pid[i], (void **)&retVal); total_time += *retVal; total_item += 1; } //printf("total item:%ld\n",total_item); //printf("total time:%lf\n",total_time); printf("average TPS(%ld tests):%lf\n",total_item,(double)(total_item/total_time)); free(retVal); return 0; } void* threadFun(void* arg) { struct timeval tvs,tve; int *n = (int *)arg; //开始计数 gettimeofday(&tvs,NULL); //调用app testapp(*n); //结束计数 gettimeofday(&tve,NULL); double *span = (double *)malloc(1*sizeof(double)); *span = tve.tv_sec-tvs.tv_sec + (tve.tv_usec-tvs.tv_usec)/1000000.0; return ((void*)span); } void testapp(int n) { int ret; char mac[18]; int i = 100000; while(i--) return ; } static void app_exit(int signum) { if (signum == SIGINT||signum == SIGSTOP) { printf("\nSIGINT received\n"); int i; for(i = 0;i < pid_num;i++) { if(pid[i] == -1) continue; pthread_cancel(pid[pid_num]); } exit(EXIT_SUCCESS); } else { printf("\nother %d received\n", signum); } } ```