summaryrefslogtreecommitdiffstats
path: root/rasodmg/test/test_benchmark.cc
blob: 2997d684c7624bdbeebc9375dc72d29333a8ba89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * SOURCE: test_benchmark.cc
 *
 * MODULE: rasodmg
 *
 * COMMENTS:
 *			None
*/

#ifdef EARLY_TEMPLATE
#define __EXECUTABLE__
#include "raslib/template_inst.hh"
#endif

#ifdef __VISUALC__
#include <strstrea.h>
#else
#include <strstream.h>
#endif

#include <iostream>
#include <fstream.h>
#include <string.h>

#include "rasodmg/transaction.hh"
#include "rasodmg/database.hh"
#include "rasodmg/ref.hh"
#include "rasodmg/set.hh"
#include "rasodmg/marray.hh"
#include "rasodmg/iterator.hh"
#include "rasodmg/oqlquery.hh"

#include "raslib/type.hh"
#include "raslib/shhopt.h"
#include "raslib/rmdebug.hh"

#include <sys/time.h>

static int numExec = 1;  // number of repetitions for each query
static int readEach = 0; // number of repeated queries in query file  
static int qNum = 0;     // counter for current query

RMTimer execTimer("test_benchmark", "r_oql_execute()");
RMTimer wholeTimer("test_benchmark", "execQuery()");

int checkArguments( int argc, char** argv, const char* searchText, 
		    int& optionValueIndex )
{
  int found = 0;
  int i=1;

  while( !found && i<argc )
    found = !strcmp( searchText, argv[i++] );   

  if( found && i<argc && !strchr(argv[i],'-') )
    optionValueIndex = i;
  else
    optionValueIndex = 0;
  
  return found;
}

void execQuery( char* serverName, char* baseName, char* queryBuffer )
{
}

static void 
printUsage(void)
{
  cout << "Usage: test_benchmark [options] server_name base_name file_name" 
       << endl;
  cout << "  -h, --help         Print this message and exit." << endl;
  cout << "  -n, --nrepeat      Followed by number of repeated" << endl
       << "                     executions for each query in file_name."
       << endl;
  cout << "  -e, --readeach     Followed by number of queries read from" 
       << endl
       << "                     the query file treated as variations of" 
       << endl
       << "                     the same query." << endl;
  exit(0);
}

void execQuery(char* serverName, char* baseName, char* comment, char* query)
{
  int i;
  r_Database db;
  r_Transaction ta;

  RMInit::bmOut << comment;
  RMInit::bmOut << "Query " << (qNum/numExec)+1 << "." << (qNum%numExec)+1
		<< ": " << query;
  qNum++;

  wholeTimer.start();

  r_Set< r_Ref_Any > image_set;
  db.set_servername( serverName );

  try
    {
      r_OQL_Query q1( query );

      db.open( baseName );

      if( q1.is_update_query() )
        ta.begin();
      else
        ta.begin( r_Transaction::read_only );
  
      // here the query is executed and times are taken

      try
	{
	  execTimer.start();
	  if( q1.is_update_query() )
	    r_oql_execute( q1 );
	  else
	    r_oql_execute( q1, image_set );
	  execTimer.stop();
	}

      catch( r_Error& errorObj )
	{
	  execTimer.stop();
	  cerr << endl << "QUERY FAILED" << endl << errorObj.what() << endl;
	  ta.commit();
	  db.close();
	}

      ta.commit();
      db.close();

      wholeTimer.stop();
    }
  catch( r_Error& errorObj )
    {
      wholeTimer.stop();
      cerr << endl << "FAILED" << endl << errorObj.what() << endl;
    }
}

void execQueries(char* serverName, char* baseName, char* comment, char* query)
{
  if(!readEach) {
    for(int i = 0; i < numExec; i++)
      execQuery(serverName, baseName, comment, query);
  }
  else {
    execQuery(serverName, baseName, comment, query);
  }
}

void parseFile(ifstream& fileStream, char* serverName, char* baseName)
{
  enum legalStates { FIRSTCOMMENT, COMMENT, QUERY };
  legalStates parseState = FIRSTCOMMENT;
  ostrstream commentStream; 
  ostrstream queryStream;
  char buf[256];
  char dummy;

  while( fileStream.get( buf, 255, '\n' ) ) {
    // read end of line
    fileStream.get(dummy);
    // checking for empty line
    if(buf[0] == 0) {
      if(parseState == FIRSTCOMMENT) {
	// end of first comment, print it and switch to normal comment
	commentStream << ends;
	RMInit::bmOut << commentStream.str() << endl;
	commentStream.rdbuf()->freeze(0);
	commentStream.seekp(0, ios::beg);
	parseState = COMMENT;
      }
    }
    // checking for comment
    else if(buf[0] == '/' && buf[1] == '/') {
      if(parseState == QUERY) {
	// execute the query, reset buffers
	queryStream << endl << ends;
	commentStream << ends;
	execQueries(serverName, baseName, commentStream.str(), 
		    queryStream.str());
	queryStream.rdbuf()->freeze(0);
	queryStream.seekp(0, ios::beg);
	commentStream.rdbuf()->freeze(0);
	commentStream.seekp(0, ios::beg);
	parseState = COMMENT;
      }
      commentStream << buf << endl; 
    } 
    // must be a query
    else {
      parseState = QUERY;
      queryStream << buf;
      queryStream << " ";
    }
  }
  // last query
  if(parseState == QUERY) {
    // execute the query, reset buffers
    queryStream << endl << ends;
    commentStream << ends;
    execQueries(serverName, baseName, commentStream.str(), 
		queryStream.str());
    queryStream.rdbuf()->freeze(0);
    queryStream.seekp(0, ios::beg);
    commentStream.rdbuf()->freeze(0);
    commentStream.seekp(0, ios::beg);
  }
}

int main( int argc, char** argv )
{   
  int  optionValueIndex; 
  char serverName[255];
  char baseName[255];
  char fileName[255];

  optStruct testBenchmarkOpt[] = {
    /* short long           type        var/func        special    */
    { 'h',   "help",        OPT_FLAG,   printUsage,     OPT_CALLFUNC },
    { 'n',   "nrepeat",     OPT_INT,    &numExec,       0 },
    { 'e',   "readeach",    OPT_INT,    &readEach,      0 },
    { 0, 0, OPT_END, 0, 0 }  /* no more options */
  };

  /* parse all options */
  optParseOptions(&argc, argv, testBenchmarkOpt, 0);

  if( argc < 4 )
    printUsage();

  if(readEach)
    numExec = readEach;

  strcpy( serverName, argv[argc-3] );
  strcpy( baseName, argv[argc-2] );
  strcpy( fileName, argv[argc-1] );

  ifstream fileStream( fileName );
  if( !fileStream )
  {
    cout << "Error: File not found." << endl;
    return -1;
  }

  parseFile(fileStream, serverName, baseName);
  return 0;
}