Skip to content

Commit e8659db

Browse files
committed
Debug: benchmark SNMP session related operations
1 parent f0822cc commit e8659db

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

snmp_bulkget.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,14 @@ unsigned int uptime = 0, sleep_usecs = 0;
9595
unsigned int lastcheck = 0;
9696
unsigned long global_timeout = DFLT_TIMEOUT;
9797

98+
static
9899
int ifNumber = 0;
99100

101+
#ifdef DEBUG
102+
static
103+
char *implode_result;
104+
#endif
105+
100106
int
101107
main(int argc, char *argv[])
102108
{
@@ -374,11 +380,17 @@ main(int argc, char *argv[])
374380
if (getenv("MIBS") == NULL)
375381
setenv("MIBS", "", 1);
376382

383+
#ifdef DEBUG
384+
benchmark_start("Start SNMP session");
385+
#endif
377386
if (user)
378387
/* use snmpv3 */
379388
ss=start_session_v3(&session, user, auth_proto, auth_pass, priv_proto, priv_pass, hostname);
380389
else
381390
ss=start_session(&session, community, hostname);
391+
#ifdef DEBUG
392+
benchmark_end();
393+
#endif
382394

383395
if (mode == NONBULK) {
384396
oid_ifp = oid_if_get;
@@ -428,8 +440,16 @@ main(int argc, char *argv[])
428440
snmp_add_null_var(pdu, lastOid.name, lastOid.name_len);
429441
}
430442

443+
#ifdef DEBUG
444+
implode_result = implode(", ", oid_ifp + count);
445+
benchmark_start("Send SNMP request for OIDs: %s", implode_result);
446+
#endif
431447
/* send the request */
432448
status = snmp_synch_response(ss, pdu, &response);
449+
#ifdef DEBUG
450+
benchmark_end();
451+
free(implode_result);
452+
#endif
433453
if (sleep_usecs)
434454
usleep(sleep_usecs);
435455

@@ -596,8 +616,16 @@ main(int argc, char *argv[])
596616
snmp_add_null_var(pdu, lastOid.name, lastOid.name_len);
597617
}
598618

619+
#ifdef DEBUG
620+
implode_result = implode(", ", oid_aliasp + count);
621+
benchmark_start("Send SNMP request for OIDs: %s", implode_result);
622+
#endif
599623
/* send the request */
600624
status = snmp_synch_response(ss, pdu, &response);
625+
#ifdef DEBUG
626+
benchmark_end();
627+
free(implode_result);
628+
#endif
601629
if (sleep_usecs) usleep(sleep_usecs);
602630

603631
if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
@@ -1068,7 +1096,13 @@ main(int argc, char *argv[])
10681096
}
10691097
printf("\n%*s", (int)perf.len, perf.text);
10701098

1099+
#ifdef DEBUG
1100+
benchmark_start("Close SNMP session");
1101+
#endif
10711102
snmp_close(ss);
1103+
#ifdef DEBUG
1104+
benchmark_end();
1105+
#endif
10721106

10731107
SOCK_CLEANUP;
10741108
return ((errorflag)?2:((warnflag)?1:0));
@@ -1448,7 +1482,15 @@ int create_request(netsnmp_session *ss, struct OIDStruct **OIDpp, char **oid_lis
14481482

14491483
*OIDpp = OIDp;
14501484

1485+
#ifdef DEBUG
1486+
implode_result = implode(", ", oid_list);
1487+
benchmark_start("Send SNMP request for OIDs: %s", implode_result);
1488+
#endif
14511489
status = snmp_synch_response(ss, pdu, response);
1490+
#ifdef DEBUG
1491+
benchmark_end();
1492+
free(implode_result);
1493+
#endif
14521494
if (sleep_usecs) usleep(sleep_usecs);
14531495

14541496
if (status == STAT_SUCCESS && (*response)->errstat == SNMP_ERR_NOERROR) {

utils.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#include <stdarg.h>
2+
#include <stddef.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <time.h>
17
#include "utils.h"
28

39
/*
@@ -113,3 +119,84 @@ int gauge_to_si(u64 bignum, char **str)
113119

114120
}
115121

122+
static
123+
struct timespec benchmark_start_time;
124+
125+
static
126+
char *benchmark_task;
127+
128+
void benchmark_start(char const *format, ...)
129+
{
130+
{
131+
va_list args;
132+
va_start(args, format);
133+
int benchmark_task_length = vsnprintf(NULL, 0u, format, args);
134+
va_end(args);
135+
benchmark_task = (char*)malloc(benchmark_task_length + 1);
136+
benchmark_task[benchmark_task_length] = 0;
137+
}
138+
{
139+
va_list args;
140+
va_start(args, format);
141+
vsprintf(benchmark_task, format, args);
142+
va_end(args);
143+
}
144+
fprintf(stderr, "[Starting benchmark] %s\n", benchmark_task);
145+
clock_gettime(CLOCK_MONOTONIC, &benchmark_start_time);
146+
}
147+
148+
void benchmark_end(void)
149+
{
150+
{
151+
struct timespec benchmark_end_time;
152+
clock_gettime(CLOCK_MONOTONIC, &benchmark_end_time);
153+
fprintf(
154+
stderr,
155+
"[Finished benchmark after %f ms] %s\n",
156+
((double)benchmark_end_time.tv_sec * 1000.0 + (double)benchmark_end_time.tv_nsec / 1000000.0)
157+
- ((double)benchmark_start_time.tv_sec * 1000.0 + (double)benchmark_start_time.tv_nsec / 1000000.0),
158+
benchmark_task
159+
);
160+
}
161+
free(benchmark_task);
162+
}
163+
164+
char *implode(char const *glue, char **pieces)
165+
{
166+
size_t total_len = 0u;
167+
char **walk_pieces = pieces;
168+
while (*walk_pieces != NULL) {
169+
total_len += strlen(*walk_pieces++);
170+
}
171+
172+
ptrdiff_t walk_pieces_diff = walk_pieces - pieces;
173+
if (walk_pieces_diff >= 2) {
174+
total_len += strlen(glue) * (size_t)(walk_pieces_diff - 1);
175+
}
176+
177+
char *result = (char*)malloc(total_len + 1u);
178+
179+
if (walk_pieces_diff > 0) {
180+
strcpy(result, *pieces);
181+
if (walk_pieces_diff >= 2) {
182+
char *walk_result = result;
183+
walk_pieces = pieces + 1;
184+
while (*walk_pieces != NULL) {
185+
while (*walk_result) {
186+
++walk_result;
187+
}
188+
strcpy(walk_result, glue);
189+
190+
while (*walk_result) {
191+
++walk_result;
192+
}
193+
strcpy(walk_result, *walk_pieces++);
194+
}
195+
}
196+
} else {
197+
*result = 0;
198+
}
199+
200+
return result;
201+
}
202+

utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ int addstrold(char **, size_t *, const char *, ...);
2828
int addstr(String *, const char *, ...);
2929
void strcpy_nospaces(char *, char *);
3030
int gauge_to_si(u64, char **);
31+
void benchmark_start(char const *, ...);
32+
void benchmark_end(void);
33+
char *implode(char const *, char **);

0 commit comments

Comments
 (0)