1
0
Fork 0

added multi-threaded ZeroMQ

This commit is contained in:
Frank Celler 2012-06-17 17:44:20 +02:00
parent 2635b48208
commit 292b78002d
2 changed files with 0 additions and 192 deletions

View File

@ -1,134 +0,0 @@
//
// Hello World client
// Connects REQ socket to tcp://localhost:5555
// Sends "Hello" to server, expects "World" back
//
#include "BasicsC/common.h"
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
void* context = 0;
size_t loop = 10000;
char const* connection = "tcp://localhost:5555";
#include <sys/prctl.h>
// Aufruf:
//
// -C <zeromq-connection>
// -u <path> where <path> is of the form "/_api/version"
// -h <key> <value> where <key> is converted to all lowercase
// -v <key> <value>
// -P <pipeline> send <number> request in one message
// -c <concurrency> use <concurrency> parallel requests
// -n <count> send a total of <count> requests
// -C <concurrency> ZeroMQ concurrency
void* ThreadStarter (void* data) {
int res;
size_t n;
void *requester;
requester = zmq_socket(context, ZMQ_REQ);
res = zmq_connect(requester, connection);
if (res != 0) {
printf("ERROR zmq_connect: %d\n", errno);
}
prctl(PR_SET_NAME, "zclient-W", 0, 0, 0);
for (n = 0; n < loop; n++) {
zmq_msg_t request;
zmq_msg_t reply;
// send
zmq_msg_init_size (&request, 5);
memcpy (zmq_msg_data (&request), "Hello", 5);
res = zmq_send (requester, &request, 0);
if (res != 0) {
printf("ERROR zmq_send: %d %d %s\n", (int) res, (int) errno, strerror(errno));
}
zmq_msg_close (&request);
// receive
zmq_msg_init (&reply);
res = zmq_recv (requester, &reply, 0);
if (res != 0) {
printf("ERROR zmq_recv: %d %d %s\n", (int) res, (int) errno, strerror(errno));
}
zmq_msg_close (&reply);
}
zmq_close (requester);
return 0;
}
int main (int argc, char* argv[])
{
int conc = 1;
int t1;
int t2;
int rt;
int i;
pthread_t* threads;
if (1 < argc) {
loop = atoi(argv[1]);
}
if (2 < argc) {
conc = atoi(argv[2]);
}
if (3 < argc) {
connection = argv[3];
}
printf("Looping %d times pro thread, total %d\n", (int) loop, (int)(loop * conc));
printf("Concurrency %d\n", conc);
printf("Connection %s\n", connection);
context = zmq_init(16);
// Socket to talk to server
printf ("Connecting to hello world server…\n");
t1 = time(0);
threads = (pthread_t*) malloc(sizeof(pthread_t) * conc);
for (i = 0; i < conc; ++i) {
pthread_create(&threads[i], 0, &ThreadStarter, 0);
}
for (i = 0; i < conc; ++i) {
pthread_join(threads[i], 0);
}
t2 = time(0);
rt = t2 - t1;
printf("runtime %d, req/sec %f\n", rt, 1.0 * loop * conc / rt);
zmq_term (context);
return 0;
}

View File

@ -1,58 +0,0 @@
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
void* context = 0;
char const* connection = "tcp://*:5555";
void* ThreadStarter (void* data) {
void *responder;
if (2 < argc) {
conc = atoi(argv[2]);
}
if (3 < argc) {
connection = argv[3];
}
// Socket to talk to clients
responder = zmq_socket (context, ZMQ_REP);
zmq_bind (responder, connection);
while (1) {
zmq_msg_t request;
zmq_msg_t reply;
// Wait for next request from client
zmq_msg_init (&request);
zmq_recv (responder, &request, 0);
// printf ("Received Hello\n");
zmq_msg_close (&request);
// do some work
// Send reply back to client
zmq_msg_init_size (&reply, 5);
memcpy (zmq_msg_data (&reply), "World", 5);
zmq_send (responder, &reply, 0);
zmq_msg_close (&reply);
}
// We never get here but if we did, this would be how we end
zmq_close (responder);
}
int main (int argc, char* argv[])
{
if (1 < argc) {
connection = argv[1];
}
context = zmq_init (16);
zmq_term (context);
return 0;
}