mirror of
https://github.com/zeromq/libzmq.git
synced 2024-12-26 23:01:04 +08:00
Move perf helper functions to perf/helpers.cpp
This commit is contained in:
parent
05b4a7ae78
commit
606c77368c
@ -232,23 +232,6 @@ ZMQ_EXPORT int zmq_poll (zmq_pollitem_t *items, int nitems, long timeout);
|
||||
|
||||
ZMQ_EXPORT int zmq_device (int device, void * insocket, void* outsocket);
|
||||
|
||||
/******************************************************************************/
|
||||
/* Helper functions. */
|
||||
/******************************************************************************/
|
||||
|
||||
/* Helper functions are used by perf tests so that they don't have to care */
|
||||
/* about minutiae of time-related functions on different OS platforms. */
|
||||
|
||||
/* Starts the stopwatch. Returns the handle to the watch. */
|
||||
ZMQ_EXPORT void *zmq_stopwatch_start ();
|
||||
|
||||
/* Stops the stopwatch. Returns the number of microseconds elapsed since */
|
||||
/* the stopwatch was started. */
|
||||
ZMQ_EXPORT unsigned long zmq_stopwatch_stop (void *watch_);
|
||||
|
||||
/* Sleeps for specified number of seconds. */
|
||||
ZMQ_EXPORT void zmq_sleep (int seconds_);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -1,15 +1,16 @@
|
||||
INCLUDES = -I$(top_builddir)/include
|
||||
|
||||
noinst_PROGRAMS = local_lat remote_lat local_thr remote_thr
|
||||
EXTRA_DIST = helpers.h
|
||||
|
||||
local_lat_LDADD = $(top_builddir)/src/libzmq.la
|
||||
local_lat_SOURCES = local_lat.cpp
|
||||
local_lat_SOURCES = local_lat.cpp helpers.cpp
|
||||
|
||||
remote_lat_LDADD = $(top_builddir)/src/libzmq.la
|
||||
remote_lat_SOURCES = remote_lat.cpp
|
||||
remote_lat_SOURCES = remote_lat.cpp helpers.cpp
|
||||
|
||||
local_thr_LDADD = $(top_builddir)/src/libzmq.la
|
||||
local_thr_SOURCES = local_thr.cpp
|
||||
local_thr_SOURCES = local_thr.cpp helpers.cpp
|
||||
|
||||
remote_thr_LDADD = $(top_builddir)/src/libzmq.la
|
||||
remote_thr_SOURCES = remote_thr.cpp
|
||||
remote_thr_SOURCES = remote_thr.cpp helpers.cpp
|
||||
|
86
perf/helpers.cpp
Normal file
86
perf/helpers.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
/*
|
||||
Copyright (c) 2007-2010 iMatix Corporation
|
||||
|
||||
This file is part of 0MQ.
|
||||
|
||||
0MQ is free software; you can redistribute it and/or modify it under
|
||||
the terms of the Lesser GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
0MQ is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Lesser GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Lesser GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#ifdef _WIN32
|
||||
# include <windows.h>
|
||||
#else
|
||||
# include <sys/time.h>
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
#include "../src/stdint.hpp"
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
static uint64_t now ()
|
||||
{
|
||||
// Get the high resolution counter's accuracy.
|
||||
LARGE_INTEGER ticksPerSecond;
|
||||
QueryPerformanceFrequency (&ticksPerSecond);
|
||||
|
||||
// What time is it?
|
||||
LARGE_INTEGER tick;
|
||||
QueryPerformanceCounter (&tick);
|
||||
|
||||
// Convert the tick number into the number of seconds
|
||||
// since the system was started.
|
||||
double ticks_div = (double) (ticksPerSecond.QuadPart / 1000000);
|
||||
return (uint64_t) (tick.QuadPart / ticks_div);
|
||||
}
|
||||
|
||||
void perf_sleep (int seconds_)
|
||||
{
|
||||
Sleep (seconds_ * 1000);
|
||||
}
|
||||
|
||||
#else /* not _WIN32 */
|
||||
|
||||
static uint64_t now ()
|
||||
{
|
||||
struct timeval tv;
|
||||
int rc;
|
||||
|
||||
rc = gettimeofday (&tv, NULL);
|
||||
assert (rc == 0);
|
||||
return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec);
|
||||
}
|
||||
|
||||
void perf_sleep (int seconds_)
|
||||
{
|
||||
sleep (seconds_);
|
||||
}
|
||||
|
||||
#endif /* _WIN32 */
|
||||
|
||||
void *stopwatch_start ()
|
||||
{
|
||||
uint64_t *watch = (uint64_t*) malloc (sizeof (uint64_t));
|
||||
assert (watch);
|
||||
*watch = now ();
|
||||
return (void*) watch;
|
||||
}
|
||||
|
||||
unsigned long stopwatch_stop (void *watch_)
|
||||
{
|
||||
uint64_t end = now ();
|
||||
uint64_t start = *(uint64_t*) watch_;
|
||||
free (watch_);
|
||||
return (unsigned long) (end - start);
|
||||
}
|
40
perf/helpers.h
Normal file
40
perf/helpers.h
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
Copyright (c) 2007-2010 iMatix Corporation
|
||||
|
||||
This file is part of 0MQ.
|
||||
|
||||
0MQ is free software; you can redistribute it and/or modify it under
|
||||
the terms of the Lesser GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
0MQ is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
Lesser GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the Lesser GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __HELPERS_H_INCLUDED__
|
||||
#define __HELPERS_H_INCLUDED__
|
||||
|
||||
/******************************************************************************/
|
||||
/* Helper functions. */
|
||||
/******************************************************************************/
|
||||
|
||||
/* Helper functions are used by perf tests so that they don't have to care */
|
||||
/* about minutiae of time-related functions on different OS platforms. */
|
||||
|
||||
/* Starts the stopwatch. Returns the handle to the watch. */
|
||||
void *stopwatch_start ();
|
||||
|
||||
/* Stops the stopwatch. Returns the number of microseconds elapsed since */
|
||||
/* the stopwatch was started. */
|
||||
unsigned long stopwatch_stop (void *watch_);
|
||||
|
||||
/* Sleeps for specified number of seconds. */
|
||||
void perf_sleep (int seconds_);
|
||||
|
||||
#endif
|
@ -20,6 +20,7 @@
|
||||
#include "../include/zmq.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "helpers.h"
|
||||
|
||||
int main (int argc, char *argv [])
|
||||
{
|
||||
@ -88,7 +89,7 @@ int main (int argc, char *argv [])
|
||||
return -1;
|
||||
}
|
||||
|
||||
zmq_sleep (1);
|
||||
perf_sleep (1);
|
||||
|
||||
rc = zmq_close (s);
|
||||
if (rc != 0) {
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include "../include/zmq.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "helpers.h"
|
||||
|
||||
int main (int argc, char *argv [])
|
||||
{
|
||||
@ -87,7 +88,7 @@ int main (int argc, char *argv [])
|
||||
return -1;
|
||||
}
|
||||
|
||||
watch = zmq_stopwatch_start ();
|
||||
watch = stopwatch_start ();
|
||||
|
||||
for (i = 0; i != message_count - 1; i++) {
|
||||
rc = zmq_recv (s, &msg, 0);
|
||||
@ -101,7 +102,7 @@ int main (int argc, char *argv [])
|
||||
}
|
||||
}
|
||||
|
||||
elapsed = zmq_stopwatch_stop (watch);
|
||||
elapsed = stopwatch_stop (watch);
|
||||
if (elapsed == 0)
|
||||
elapsed = 1;
|
||||
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "helpers.h"
|
||||
|
||||
int main (int argc, char *argv [])
|
||||
{
|
||||
@ -70,7 +71,7 @@ int main (int argc, char *argv [])
|
||||
}
|
||||
memset (zmq_msg_data (&msg), 0, message_size);
|
||||
|
||||
watch = zmq_stopwatch_start ();
|
||||
watch = stopwatch_start ();
|
||||
|
||||
for (i = 0; i != roundtrip_count; i++) {
|
||||
rc = zmq_send (s, &msg, 0);
|
||||
@ -89,7 +90,7 @@ int main (int argc, char *argv [])
|
||||
}
|
||||
}
|
||||
|
||||
elapsed = zmq_stopwatch_stop (watch);
|
||||
elapsed = stopwatch_stop (watch);
|
||||
|
||||
rc = zmq_msg_close (&msg);
|
||||
if (rc != 0) {
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include "../include/zmq.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "helpers.h"
|
||||
|
||||
int main (int argc, char *argv [])
|
||||
{
|
||||
@ -80,7 +81,7 @@ int main (int argc, char *argv [])
|
||||
}
|
||||
}
|
||||
|
||||
zmq_sleep (10);
|
||||
perf_sleep (10);
|
||||
|
||||
rc = zmq_close (s);
|
||||
if (rc != 0) {
|
||||
|
58
src/zmq.cpp
58
src/zmq.cpp
@ -663,61 +663,3 @@ int zmq_device (int device_, void *insocket_, void *outsocket_)
|
||||
return EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined ZMQ_HAVE_WINDOWS
|
||||
|
||||
static uint64_t now ()
|
||||
{
|
||||
// Get the high resolution counter's accuracy.
|
||||
LARGE_INTEGER ticksPerSecond;
|
||||
QueryPerformanceFrequency (&ticksPerSecond);
|
||||
|
||||
// What time is it?
|
||||
LARGE_INTEGER tick;
|
||||
QueryPerformanceCounter (&tick);
|
||||
|
||||
// Convert the tick number into the number of seconds
|
||||
// since the system was started.
|
||||
double ticks_div = (double) (ticksPerSecond.QuadPart / 1000000);
|
||||
return (uint64_t) (tick.QuadPart / ticks_div);
|
||||
}
|
||||
|
||||
void zmq_sleep (int seconds_)
|
||||
{
|
||||
Sleep (seconds_ * 1000);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static uint64_t now ()
|
||||
{
|
||||
struct timeval tv;
|
||||
int rc;
|
||||
|
||||
rc = gettimeofday (&tv, NULL);
|
||||
assert (rc == 0);
|
||||
return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec);
|
||||
}
|
||||
|
||||
void zmq_sleep (int seconds_)
|
||||
{
|
||||
sleep (seconds_);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void *zmq_stopwatch_start ()
|
||||
{
|
||||
uint64_t *watch = (uint64_t*) malloc (sizeof (uint64_t));
|
||||
zmq_assert (watch);
|
||||
*watch = now ();
|
||||
return (void*) watch;
|
||||
}
|
||||
|
||||
unsigned long zmq_stopwatch_stop (void *watch_)
|
||||
{
|
||||
uint64_t end = now ();
|
||||
uint64_t start = *(uint64_t*) watch_;
|
||||
free (watch_);
|
||||
return (unsigned long) (end - start);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user