Skip to content

Instantly share code, notes, and snippets.

@amitmurthy
Last active December 22, 2015 16:34
Show Gist options
  • Save amitmurthy/945843c9f0c61bc7d9e0 to your computer and use it in GitHub Desktop.
Save amitmurthy/945843c9f0c61bc7d9e0 to your computer and use it in GitHub Desktop.
echo tests
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <time.h>
int main(int argc, char ** argv){
int sock;
char buffer[1024];
struct sockaddr_in addr;
socklen_t addr_size;
sock = socket(PF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(8500);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(addr.sin_zero, '\0', sizeof addr.sin_zero);
addr_size = sizeof addr;
if (connect(sock, (struct sockaddr *) &addr, addr_size) < 0) {
printf("connect error \n");
}
int i = 0;
int n = atoi(argv[1]);
struct timespec tstart={0,0}, tend={0,0};
clock_gettime(CLOCK_MONOTONIC, &tstart);
for (i = 0; i < n; i++) {
if (send(sock,buffer,1024,0) < 1024) {
printf("Send error\n");
exit(1);
}
int cnt = 0;
while (cnt < 1024) {
int ret = recv(sock, buffer+cnt, 1024-cnt, 0);
if (ret < 0) {
printf("recv error\n");
exit(1);
}
cnt += ret;
}
}
clock_gettime(CLOCK_MONOTONIC, &tend);
printf("%d request-response of 1024 took %.5f seconds\n", n,
((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) -
((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec));
return 0;
}
function foo(sock,n)
Base.disable_nagle(sock)
for i in 1:n
write(sock,UInt8[0 for x in 1:1024])
readbytes(sock, 1024)
end
end
function client()
@time foo(connect("localhost", 8502), 10^4)
end
client()
addprocs(1)
@everywhere echo(x) = x
function recho(n)
a=UInt8[0 for x in 1:1024]
for i in 1:n
remotecall_fetch(echo, 2, a)
end
end
recho(1)
tic()
recho(10^5)
toc()
function foo(sock)
Base.disable_nagle(sock)
n = 10^5
for i in 1:n
a=readbytes(sock, 1024)
write(sock,a)
end
end
function srvr()
const srv = listen(8502)
while true
sock = accept(srv)
@async foo(sock)
end
end
srvr()
C code (10^6 request-responses of 1K each)
------
./server 1000000 &
./client 1000000
8.95035 seconds
9.11186 seconds
0MQ C code (10^6 request-responses of 1K each)
----------
./zserver 1000000 &
./zclient 1000000
36.91122 seconds
36.92812 seconds
Julia TCP sockets 10^4 requests-responses of 1K each
-----------------
julia jserver.jl &
julia jclient.jl
7.927375 seconds (260.33 k allocations: 28.702 MB, 0.30% gc time)
7.583553 seconds (260.33 k allocations: 28.702 MB, 0.32% gc time)
Julia remotecall_fetch 10^5 requests-responses of 1K each
-----------------
julia julia_remotecall_fetch.jl
elapsed time: 8.995498505 seconds
elapsed time: 9.335054655 seconds
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
int main(int argc, char ** argv){
int lsock, asock;
char buffer[1024];
struct sockaddr_in addr;
socklen_t addr_size;
lsock = socket(PF_INET, SOCK_STREAM, 0);
int enable = 1;
if (setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
printf("setsockopt(SO_REUSEADDR) failed\n");
addr.sin_family = AF_INET;
addr.sin_port = htons(8500);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(addr.sin_zero, '\0', sizeof addr.sin_zero);
bind(lsock, (struct sockaddr *) &addr, sizeof(addr));
if(listen(lsock,5) < 0) {
printf("listen error\n");
exit(1);
}
addr_size = sizeof addr;
asock = accept(lsock, (struct sockaddr *) &addr, &addr_size);
int i = 0;
int n = atoi(argv[1]);
for (i = 0; i < n; i++) {
int cnt = 0;
while (cnt < 1024) {
int ret = recv(asock, buffer+cnt, 1024-cnt, 0);
if (ret < 0) {
printf("recv error\n");
exit(1);
}
cnt += ret;
}
if (send(asock,buffer,1024,0) < 1024) {
printf("Send error\n");
exit(1);
}
}
return 0;
}
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, char ** argv)
{
void *context = zmq_ctx_new ();
void *requester = zmq_socket (context, ZMQ_REQ);
zmq_connect (requester, "tcp://localhost:8501");
int n = atoi(argv[1]);
char buffer [1024];
struct timespec tstart={0,0}, tend={0,0};
clock_gettime(CLOCK_MONOTONIC, &tstart);
while (n > 0) {
zmq_send (requester, buffer, 1024, 0);
zmq_recv (requester, buffer, 1024, 0);
n--;
}
zmq_close (requester);
zmq_ctx_destroy (context);
clock_gettime(CLOCK_MONOTONIC, &tend);
printf("%d request-response of 1024 took %.5f seconds\n", atoi(argv[1]),
((double)tend.tv_sec + 1.0e-9*tend.tv_nsec) -
((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec));
return 0;
}
#include <zmq.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
int main (int argc, char ** argv)
{
// Socket to talk to clients
void *context = zmq_ctx_new ();
void *responder = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (responder, "tcp://*:8501");
assert (rc == 0);
int n = atoi(argv[1]);
char buffer [1024];
while (n > 0) {
zmq_recv (responder, buffer, 1024, 0);
zmq_send (responder, buffer, 1024, 0);
n--;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment