~ubuntu-branches/ubuntu/trusty/syslog-ng/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/unit/test_dnscache.c

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS), Gergely Nagy
  • Date: 2011-10-11 14:30:48 UTC
  • mfrom: (1.3.7)
  • Revision ID: package-import@ubuntu.com-20111011143048-r1iljux9xbvj3lwh
Tags: 3.3.1.dfsg-1
* New upstream release with important fixes from upstream git tree with
  non-free manpages removed.
* Drop syslog-ng.conf(5) (closes: #496521).
* syslog-ng(8) is generated, and does not mention -Q anymore
  (closes: #616069).
* Supports CAP_SYSLOG on recent kernels (closes: #630172).
* Does not use g_timeout_add_seconds anymore (closes: #609154).

[ Gergely Nagy <algernon@madhouse-project.org> ]
* Update debian/copyright to DEP-5 format.
* Simplified the logrotate file by merging identical entries.
* Include local configuration files from /etc/syslog-ng/conf.d/ (Closes:
  #609050).
* Update syslog-ng.conf to be fully 3.3 compliant.
* Compress both source and binaries (except the syslog-ng meta
  package) with xz, instead of gzip.
* Use dpkg triggers to restart syslog-ng when appropriate.
* Include DFSG-free manual pages for all binaries.
* Build with Hardening enabled.
* Mention syslog(3) in /etc/default/syslog-ng, instead of
  <linux/kernel.h> (Closes: #608605)
* Support 'status' in the init script.
  Patch from Peter Eisentraut <petere@debian.org> (Closes: #644458)
* Build-Depend on libevtlog-dev (>= 0.2.12-5~) for correct shlibs.
* Use [linux-any] in Build-Depends instead of hardcoded links.
  (Closes: #634715)
* Use $SYSLOGNG_OPTS in the init script when reloading syslog-ng.
  (Closes: #589081)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "dnscache.h"
2
2
#include "apphook.h"
 
3
#include "timeutils.h"
 
4
 
3
5
#include <sys/types.h>
4
6
#include <sys/socket.h>
5
7
#include <netinet/in.h>
7
9
#include <string.h>
8
10
#include <stdio.h>
9
11
#include <unistd.h>
 
12
#include <stdlib.h>
 
13
 
 
14
void
 
15
test_expiration(void)
 
16
{
 
17
  gint i;
 
18
  const gchar *hn = NULL;
 
19
  gboolean positive;
 
20
 
 
21
  dns_cache_init();
 
22
  dns_cache_set_params(50000, 3, 1, NULL);
 
23
 
 
24
  for (i = 0; i < 10000; i++)
 
25
    {
 
26
      guint32 ni = htonl(i);
 
27
 
 
28
      dns_cache_store(FALSE, AF_INET, (void *) &ni, i < 5000 ? "hostname" : NULL, i < 5000);
 
29
    }
 
30
 
 
31
  for (i = 0; i < 10000; i++)
 
32
    {
 
33
      guint32 ni = htonl(i);
 
34
 
 
35
      hn = NULL;
 
36
      positive = FALSE;
 
37
      if (!dns_cache_lookup(AF_INET, (void *) &ni, &hn, &positive))
 
38
        {
 
39
          fprintf(stderr, "hmmm cache forgot the cache entry too early, i=%d, hn=%s\n", i, hn);
 
40
          exit(1);
 
41
        }
 
42
      else
 
43
        {
 
44
          if (i < 5000)
 
45
            {
 
46
              if (!positive || strcmp(hn, "hostname") != 0)
 
47
                {
 
48
                  fprintf(stderr, "hmm, cached returned an positive match, but cached name invalid, i=%d, hn=%s\n", i, hn);
 
49
                  exit(1);
 
50
                }
 
51
            }
 
52
          else
 
53
            {
 
54
              if (positive || hn != NULL)
 
55
                {
 
56
                  fprintf(stderr, "hmm, cache returned a positive match, where a negative match was expected, i=%d, hn=%s\n", i, hn);
 
57
                  exit(1);
 
58
                }
 
59
            }
 
60
 
 
61
        }
 
62
    }
 
63
 
 
64
  /* negative entries should expire by now, positive ones still present */
 
65
  sleep(2);
 
66
  invalidate_cached_time();
 
67
 
 
68
  for (i = 0; i < 10000; i++)
 
69
    {
 
70
      guint32 ni = htonl(i);
 
71
 
 
72
      hn = NULL;
 
73
      positive = FALSE;
 
74
      if (i < 5000)
 
75
        {
 
76
          if (!dns_cache_lookup(AF_INET, (void *) &ni, &hn, &positive) || !positive)
 
77
            {
 
78
              fprintf(stderr, "hmmm cache forgot positive entries too early, i=%d\n", i);
 
79
              exit(1);
 
80
            }
 
81
        }
 
82
      else
 
83
        {
 
84
          if (dns_cache_lookup(AF_INET, (void *) &ni, &hn, &positive) || positive)
 
85
            {
 
86
              fprintf(stderr, "hmmm cache didn't forget negative entries in time, i=%d\n", i);
 
87
              exit(1);
 
88
            }
 
89
        }
 
90
    }
 
91
 
 
92
  /* everything should be expired by now */
 
93
 
 
94
  sleep(2);
 
95
  invalidate_cached_time();
 
96
 
 
97
  for (i = 0; i < 10000; i++)
 
98
    {
 
99
      guint32 ni = htonl(i);
 
100
 
 
101
      hn = NULL;
 
102
      positive = FALSE;
 
103
      if (dns_cache_lookup(AF_INET, (void *) &ni, &hn, &positive))
 
104
        {
 
105
          fprintf(stderr, "hmmm cache did not forget an expired entry, i=%d\n", i);
 
106
          exit(1);
 
107
        }
 
108
    }
 
109
}
 
110
 
 
111
void
 
112
test_dns_cache_benchmark(void)
 
113
{
 
114
  GTimeVal start, end;
 
115
  const gchar *hn;
 
116
  gboolean positive;
 
117
  gint i;
 
118
 
 
119
  dns_cache_init();
 
120
  dns_cache_set_params(50000, 600, 300, NULL);
 
121
 
 
122
  for (i = 0; i < 10000; i++)
 
123
    {
 
124
      guint32 ni = htonl(i);
 
125
 
 
126
      dns_cache_store(FALSE, AF_INET, (void *) &ni, "hostname", TRUE);
 
127
    }
 
128
 
 
129
  g_get_current_time(&start);
 
130
  /* run benchmarks */
 
131
  for (i = 0; i < 10000; i++)
 
132
    {
 
133
      guint32 ni = htonl(i % 10000);
 
134
 
 
135
      hn = NULL;
 
136
      if (!dns_cache_lookup(AF_INET, (void *) &ni, &hn, &positive))
 
137
        {
 
138
          fprintf(stderr, "hmm, dns cache entries expired during benchmarking, this is unexpected\n, i=%d", i);
 
139
        }
 
140
    }
 
141
  g_get_current_time(&end);
 
142
  printf("DNS cache speed: %12.3f iters/sec\n", i * 1e6 / g_time_val_diff(&end, &start));
 
143
}
 
144
 
 
145
void
 
146
test_inet_ntop_benchmark(void)
 
147
{
 
148
  GTimeVal start, end;
 
149
  gint i;
 
150
  gchar buf[32];
 
151
 
 
152
  g_get_current_time(&start);
 
153
  /* run benchmarks */
 
154
  for (i = 0; i < 10000; i++)
 
155
    {
 
156
      guint32 ni = htonl(i % 10000);
 
157
 
 
158
      inet_ntop(AF_INET, (void *) &ni, buf, sizeof(buf));
 
159
    }
 
160
  g_get_current_time(&end);
 
161
  printf("inet_ntop speed: %12.3f iters/sec\n", i * 1e6 / g_time_val_diff(&end, &start));
 
162
}
10
163
 
11
164
int
12
165
main()
13
166
{
14
 
  int i;
15
 
 
16
167
  app_startup();
17
 
  dns_cache_init();
18
 
  dns_cache_set_params(50000, 2, 1, NULL);
19
 
 
20
 
  for (i = 0; i < 10000; i++)
21
 
    {
22
 
      guint32 ni = htonl(i);
23
 
 
24
 
      dns_cache_store(FALSE, AF_INET, (void *) &ni, "hostname");
25
 
    }
26
 
 
27
 
  for (i = 0; i < 10000; i++)
28
 
    {
29
 
      guint32 ni = htonl(i);
30
 
      const gchar *hn = NULL;
31
 
 
32
 
      if (!dns_cache_lookup(AF_INET, (void *) &ni, &hn) || strcmp(hn, "hostname") != 0)
33
 
        {
34
 
          fprintf(stderr, "hmmm cache forgot the hostname, i=%d, hn=%s\n", i, hn);
35
 
          return 1;
36
 
        }
37
 
 
38
 
    }
39
 
 
40
 
  sleep(3);
41
 
 
42
 
  for (i = 0; i < 10000; i++)
43
 
    {
44
 
      guint32 ni = htonl(i);
45
 
      const gchar *hn = NULL;
46
 
 
47
 
      if (dns_cache_lookup(AF_INET, (void *) &ni, &hn))
48
 
        {
49
 
          fprintf(stderr, "hmmm cache did not forget the hostname, i=%d\n", i);
50
 
          return 1;
51
 
        }
52
 
    }
 
168
 
 
169
  test_expiration();
 
170
  test_dns_cache_benchmark();
 
171
  test_inet_ntop_benchmark();
53
172
 
54
173
  app_shutdown();
55
174
  return 0;