1 /* $Id: bam_test.cpp 621765 2020-12-16 19:23:32Z vasilche $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Authors: Eugene Vasilchenko
27 *
28 * File Description:
29 * Test application for User-Agent setting
30 *
31 */
32
33 #include <iostream>
34 #include <vector>
35 #include <klib/rc.h>
36 #include <klib/out.h>
37 #include <klib/log.h>
38 #include <klib/debug.h>
39 #include <kns/manager.h>
40 #include <vfs/manager.h>
41 #include <vfs/path.h>
42 #include <kfs/file.h>
43 #include <pthread.h>
44 #ifdef _MSC_VER
45 # include <io.h>
46 #else
47 # include <unistd.h>
48 #endif
49
50 using namespace std;
51
52 #define CALL(call) CheckRc((call), #call, __FILE__, __LINE__)
53
CheckRc(rc_t rc,const char * code,const char * file,int line)54 void CheckRc(rc_t rc, const char* code, const char* file, int line)
55 {
56 if ( rc ) {
57 char buffer1[4096];
58 size_t error_len;
59 RCExplain(rc, buffer1, sizeof(buffer1), &error_len);
60 char buffer2[8192];
61 unsigned len = sprintf(buffer2, "%s:%d: %s failed: %#x: %s\n",
62 file, line, code, rc, buffer1);
63 write(2, buffer2, len);
64 exit(1);
65 }
66 }
67
x_KFileRead(const KFile * file,uint64_t pos,char * buffer,size_t size,size_t * nread)68 rc_t x_KFileRead(const KFile* file, uint64_t pos, char* buffer, size_t size, size_t* nread)
69 {
70 *nread = 0;
71 while ( size ) {
72 size_t nread1;
73 rc_t rc = KFileRead(file, pos, buffer, size, &nread1);
74 if ( rc ) {
75 return rc;
76 }
77 buffer += nread1;
78 size -= nread1;
79 *nread += nread1;
80 }
81 return 0;
82 }
83
84 const char* kFileName[] = {
85 "http://ftp.ncbi.nlm.nih.gov/toolbox/gbench/samples/udc_seqgraphic_rmt_testing/remote_BAM_remap_UUD-324/human/grch38_wgsim_gb_accs.bam.bai",
86 "http://ftp.ncbi.nlm.nih.gov/toolbox/gbench/samples/udc_seqgraphic_rmt_testing/remote_BAM_remap_UUD-324/human/grch38_wgsim_rs_accs.bam.bai",
87 "http://ftp.ncbi.nlm.nih.gov/toolbox/gbench/samples/udc_seqgraphic_rmt_testing/remote_BAM_remap_UUD-324/human/grch38_wgsim_short.bam.bai",
88 "http://ftp.ncbi.nlm.nih.gov/toolbox/gbench/samples/udc_seqgraphic_rmt_testing/remote_BAM_remap_UUD-324/human/grch38_wgsim_mixed.bam.bai",
89 };
90 const size_t kFileCount = sizeof(kFileName)/sizeof(kFileName[0]);
91 const size_t kReadSize = 1000000;
92
x_Init()93 void x_Init()
94 {
95 CALL(KOutMsg("%c", 'c')); /* should not crash when KWrtInit was not called */
96
97 CALL(KWrtInit("test", 0));
98 CALL(KOutHandlerSetStdOut());
99 CALL(KDbgHandlerSetStdOut());
100 CALL(KLogHandlerSetStdOut());
101 CALL(KLogLibHandlerSetStdOut());
102 CALL(KDbgSetString("KNS"));
103 }
104
x_Read(const char * file_name,size_t read_id)105 void x_Read(const char* file_name, size_t read_id)
106 {
107 // prepare ids
108 const char* session_id = strrchr(file_name, '/')+1;
109 char client_ip[] = "1.2.3.1"; client_ip[6] += read_id;
110 const char* page_hit_id = session_id + 1;
111 // set ids
112 KNSManager* kns = 0;
113 CALL(KNSManagerMake(&kns));
114 CALL(KNSManagerSetSessionID(kns, session_id));
115 CALL(KNSManagerSetClientIP(kns, client_ip));
116 CALL(KNSManagerSetPageHitID(kns, page_hit_id));
117 CALL(KNSManagerRelease(kns)); kns = 0;
118
119 // read file
120 VFSManager* vfs = 0;
121 CALL(VFSManagerMake(&vfs));
122 VPath* path = 0;
123 CALL(VFSManagerMakePath(vfs, &path, file_name));
124 const KFile* file = 0;
125 CALL(VFSManagerOpenFileRead(vfs, &file, path));
126 vector<char> buffer(kReadSize);
127 size_t nread;
128 CALL(x_KFileRead(file, 0, buffer.data(), buffer.size(), &nread));
129 assert(nread == buffer.size());
130 CALL(KFileRelease(file)); file = 0;
131 CALL(VPathRelease(path)); path = 0;
132 CALL(VFSManagerRelease(vfs)); vfs = 0;
133 }
134
read_thread_func(void * arg)135 void* read_thread_func(void* arg)
136 {
137 size_t read_id = (size_t)arg;
138 x_Read(kFileName[read_id], read_id);
139 return 0;
140 }
141
LowLevelTest()142 int LowLevelTest()
143 {
144 cout << "Running test for User-Agent." << endl;
145
146 x_Init();
147
148 if ( 1 ) {
149 x_Read(kFileName[0], 0);
150 }
151 else {
152 pthread_t threads[kFileCount];
153 for ( size_t i = 0; i < kFileCount; ++i ) {
154 pthread_create(&threads[i], 0, read_thread_func, (void*)i);
155 }
156 for ( size_t i = 0; i < kFileCount; ++i ) {
157 void* ret = 0;
158 pthread_join(threads[i], &ret);
159 }
160 }
161
162 cout << "Success." << endl;
163 return 0;
164 }
165
main(int argc,const char * argv[])166 int main(int argc, const char* argv[])
167 {
168 return LowLevelTest();
169 }
170