1 /*------------------------------------------------------------------------------
2  *
3  * Copyright (c) 2011-2021, EURid vzw. All rights reserved.
4  * The YADIFA TM software product is provided under the BSD 3-clause license:
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  *        * Redistributions of source code must retain the above copyright
11  *          notice, this list of conditions and the following disclaimer.
12  *        * Redistributions in binary form must reproduce the above copyright
13  *          notice, this list of conditions and the following disclaimer in the
14  *          documentation and/or other materials provided with the distribution.
15  *        * Neither the name of EURid nor the names of its contributors may be
16  *          used to endorse or promote products derived from this software
17  *          without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  *------------------------------------------------------------------------------
32  *
33  */
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 
41 #include <dnscore/fdtools.h>
42 #include <dnscore/dns_resource_record.h>
43 #include <dnscore/file_input_stream.h>
44 #include <dnscore/file_output_stream.h>
45 #include <dnscore/buffer_input_stream.h>
46 #include <dnscore/buffer_output_stream.h>
47 
48 #define JOURNAL_CJF_BASE 1
49 
50 #include <dnsdb/zdb.h>
51 #include <dnsdb/zdb_utils.h>
52 #include <dnsdb/journal-cjf-common.h>
53 #include <dnsdb/journal-cjf-page-cache.h>
54 #include <dnsdb/journal-jnl.h>
55 #include <dnsdb/rrsig.h>
56 
57 static ya_result
jnl_read_from_serial(journal * jnl,u32 serial_from,dns_resource_record * rr,const char * filepath)58 jnl_read_from_serial(journal *jnl, u32 serial_from, dns_resource_record *rr, const char *filepath)
59 {
60     s64 ts_begin = timeus();
61 
62     input_stream is;
63 
64     ya_result ret;
65 
66     ret = journal_get_ixfr_stream_at_serial(jnl, serial_from, &is, rr);
67 
68     s64 ts_opened = timeus();
69 
70     if(FAIL(ret))
71     {
72         formatln("; jnl: '%s' file cannot read from its announced first serial %i : %r", filepath, serial_from, ret);
73         return ERROR;
74     }
75 
76     for(;;)
77     {
78         ret = dns_resource_record_read(rr, &is);
79 
80         if(ret <= 0)
81         {
82             if(FAIL(ret))
83             {
84                 formatln("; jnl: '%s' failed to read next record: %r", filepath,  ret);
85             }
86 
87             break;
88         }
89     }
90 
91     s64 ts_end = timeus();
92 
93     double t_open = (ts_opened - ts_begin) / 1000000.0;
94     double t_read = (ts_end - ts_opened) / 1000000.0;
95     double t_total = (ts_end - ts_begin) / 1000000.0;
96 
97     formatln("serial: %9u open: %9.6fs read: %9.6fs total: %9.6fs", serial_from, t_open, t_read, t_total);
98 
99     return SUCCESS;
100 }
101 
102 static void
jnl_speed_test(const char * filepath)103 jnl_speed_test(const char *filepath)
104 {
105     journal *jnl = NULL;
106     const char *filename = strrchr(filepath, '/');
107     size_t filename_len;
108     dns_resource_record rr;
109     ya_result ret;
110     u32 serial_from = 0;
111     u32 serial_to = 0;
112 
113     u8 origin[MAX_DOMAIN_LENGTH];
114 
115     if(filename == NULL)
116     {
117         filename = filepath;
118     }
119     else
120     {
121         ++filename;
122     }
123 
124     filename_len = strlen(filename);
125     if(filename_len < 5)
126     {
127         formatln("; jnl: '%s' name is too small to parse", filepath);
128         return;
129     }
130 /*
131     if(memcmp(&filename[filename_len - 4], ".jnl", 4) != 0)
132     {
133         formatln("jnl: '%s' does not end with .jnl", filepath);
134         return;
135     }
136 */
137     if(FAIL(ret = cstr_to_dnsname_with_check_len(origin, filename, filename_len - 4)))
138     {
139         formatln("; jnl: '%s' cannot be parsed for origin: %r", filepath, ret);
140         return;
141     }
142 
143 
144     if(FAIL(ret = journal_jnl_open_file(&jnl, filepath, origin, FALSE)))
145     {
146         formatln("; jnl: '%s' file cannot be opened as a journal: %r", filepath, ret);
147         return;
148     }
149 
150     ++jnl->rc;
151 
152     if(FAIL(ret = journal_get_serial_range(jnl, &serial_from, &serial_to)))
153     {
154         formatln("; jnl: '%s' file cannot get serial range: %r", filepath, ret);
155         return;
156     }
157 
158     dns_resource_record_init(&rr);
159 
160     formatln("; jnl: '%s' serial range: %u to %u", filepath, serial_from, serial_to);
161 
162     if((serial_to + 1) == serial_from)
163     {
164         formatln("; jnl: '%s' serial range looks funny (%u to %u)", filepath, serial_from, serial_to);
165         return;
166     }
167 
168     // forward read
169 
170     for(u32 serial = serial_from; serial_le(serial, serial_to); ++serial)
171     {
172         jnl_read_from_serial(jnl, serial, &rr, filepath);
173     }
174 
175     // backward read
176 
177     for(u32 serial = serial_to; serial_ge(serial, serial_from); --serial)
178     {
179         jnl_read_from_serial(jnl, serial, &rr, filepath);
180     }
181 
182     dns_resource_record_finalize(&rr);
183 
184     journal_release(jnl);
185 }
186 
187 /*
188  *
189  */
main(int argc,char ** argv)190 int main(int argc, char** argv)
191 {
192     dnscore_init();
193     zdb_init();
194 
195     for(int i = 1; i < argc; ++i)
196     {
197         jnl_speed_test(argv[i]);
198     }
199 
200     zdb_finalize();
201     dnscore_finalize();
202     return (EXIT_SUCCESS);
203 }
204