00001
00002
00003
00004
00005
00006
00007
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <arpa/inet.h>
00011 #include <math.h>
00012 #include <string.h>
00013 #include <unistd.h>
00014
00015 #define START '1'
00016 #define STOP '9'
00017 #define STATS '5'
00018
00019 struct sockaddr_in serv_addr;
00020 int * fd;
00021
00022 struct statistics {
00023 unsigned long long wrongAnswerCount;
00024 unsigned long long correctAnswerCount;
00025 unsigned long long hitCount;
00026 unsigned long long missCount;
00027 unsigned long long sentSetsCount;
00028 unsigned long long sentGetsCount;
00029 unsigned long long storedCount;
00030 unsigned long long notStoredCount;
00031 double totalTime;
00032 double totalSetResponseTime;
00033 double totalGetResponseTime;
00034 };
00035
00036 #define TOTAL_STATS_SIZE sizeof(struct statistics)
00037
00038 struct statistics stats;
00039 struct statistics totalStats;
00040
00041 int main(int argc, char** argv) {
00042
00043 if (argc < 3) {
00044 printf("Usage: %s host [host ...] port\n", argv[0]);
00045 return 1;
00046 }
00047
00048 int hostCount = argc - 2;
00049 fd = (int*) malloc(hostCount * sizeof(int));
00050
00051 char input[256];
00052
00053 while (1) {
00054
00055 printf("command> ");
00056 fflush(stdout);
00057
00058 scanf("%256s", input);
00059
00060 char signal;
00061
00062 if (strcmp(input, "start") == 0) {
00063 signal = START;
00064 } else if (strcmp(input, "stop") == 0) {
00065 signal = STOP;
00066 } else if (strcmp(input, "stats") == 0) {
00067 signal = STATS;
00068 } else if (strcmp(input, "exit") == 0) {
00069 printf("exiting...\n");
00070 break;
00071 } else {
00072 printf("Available commands: start stop stats exit\n");
00073 continue;
00074 }
00075
00076 memset(&totalStats, 0, sizeof(totalStats));
00077
00078 int i;
00079 for (i = 0; i < hostCount; i++) {
00080
00081 fd[i] = socket(AF_INET, SOCK_STREAM, 0);
00082 memset(&serv_addr, 0, sizeof(serv_addr));
00083 serv_addr.sin_family = AF_INET;
00084 serv_addr.sin_addr.s_addr = inet_addr(argv[1 + i]);
00085 serv_addr.sin_port = htons(atoi(argv[argc - 1]));
00086
00087 if (connect(fd[i], (struct sockaddr*) &serv_addr, sizeof(serv_addr))
00088 == -1)
00089 printf("Connection failed.\n");
00090 else
00091 printf("Connected.\n");
00092
00093 if (send(fd[i], &signal, 1, 0)) {
00094 printf("Sent %s signal.\n", input);
00095 }
00096
00097 if (signal == STATS) {
00098
00099 char recBuf[TOTAL_STATS_SIZE];
00100
00101 struct statistics stats;
00102
00103
00104 if (recv(fd[i], (void*) recBuf, TOTAL_STATS_SIZE, 0)
00105 == TOTAL_STATS_SIZE) {
00106 printf("Received stats from %s:%s\n", argv[1 + i],
00107 argv[argc - 1]);
00108 memcpy(&stats, (void*) recBuf, TOTAL_STATS_SIZE);
00109 totalStats.sentSetsCount += stats.sentSetsCount;
00110 totalStats.storedCount += stats.storedCount;
00111 totalStats.notStoredCount += stats.notStoredCount;
00112 totalStats.sentGetsCount += stats.sentGetsCount;
00113 totalStats.hitCount += stats.hitCount;
00114 totalStats.missCount += stats.missCount;
00115 totalStats.correctAnswerCount += stats.correctAnswerCount;
00116 totalStats.wrongAnswerCount += stats.wrongAnswerCount;
00117 totalStats.totalGetResponseTime += stats.totalGetResponseTime;
00118 totalStats.totalSetResponseTime += stats.totalSetResponseTime;
00119 totalStats.totalTime = stats.totalTime;
00120
00121 }
00122 }
00123
00124 close(fd[i]);
00125 }
00126
00127 if (signal == STATS) {
00128 printf("\nsent sets:%llu\n", totalStats.sentSetsCount);
00129 printf("stored:%llu\n", totalStats.storedCount);
00130 printf("not stored:%llu\n", totalStats.notStoredCount);
00131 printf("sent gets:%llu\n", totalStats.sentGetsCount);
00132 printf("hits:%llu\n", totalStats.hitCount);
00133 printf("misses:%llu\n", totalStats.missCount);
00134 printf("correct:%llu\n", totalStats.correctAnswerCount);
00135 printf("wrong:%llu\n", totalStats.wrongAnswerCount);
00136 printf("total time:%lg\n", totalStats.totalTime);
00137 printf("avg set response time s:%lg\n",
00138 totalStats.totalSetResponseTime / (totalStats.storedCount
00139 + totalStats.notStoredCount));
00140 printf("avg get response time s:%lg\n",
00141 totalStats.totalGetResponseTime / (totalStats.hitCount
00142 + totalStats.missCount));
00143
00144 if (totalStats.totalTime != 0) {
00145 printf("req/s:%lg\n", (double) (totalStats.storedCount
00146 + totalStats.notStoredCount + totalStats.hitCount
00147 + totalStats.missCount) / totalStats.totalTime);
00148 }
00149 }
00150
00151 }
00152
00153 free(fd);
00154
00155 return 0;
00156 }