0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
QueryThread.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #include <Common/Compat.h>
23 
24 #include "QueryThread.h"
25 
26 #include <Hypertable/Lib/Client.h>
28 #include <Hypertable/Lib/Config.h>
29 #include <Hypertable/Lib/Cells.h>
30 
31 #include <chrono>
32 #include <cmath>
33 #include <ctime>
34 #include <thread>
35 
36 using namespace Hypertable;
37 using namespace std;
38 
40  double clocks_per_usec = (double)CLOCKS_PER_SEC / 1000000.0;
41 
42  std::lock_guard<std::mutex> lock(m_state.mutex);
43 
44  m_state.total_cells = 0;
45  m_state.total_bytes = 0;
46  m_state.cum_latency = 0;
47  m_state.cum_sq_latency = 0;
48  m_state.min_latency = 0;
49  m_state.max_latency = 0;
50 
51  Stopwatch stopwatch;
52 
53  try {
54  ScanSpecBuilder scan_spec;
55  Cell cell;
56  clock_t start_clocks, stop_clocks;
57  int32_t delay = 0;
58  if (has("query-delay"))
59  delay = get_i32("query-delay");
60 
61  DataGenerator dg(m_props, true);
62 
63  for (DataGenerator::iterator iter = dg.begin(); iter != dg.end(); iter++) {
64 
65  if (delay)
66  std::this_thread::sleep_for(std::chrono::milliseconds(delay));
67 
68  scan_spec.clear();
69  scan_spec.add_column((*iter).column_family);
70  scan_spec.add_row((*iter).row_key);
71 
72  start_clocks = clock();
73 
74  TableScanner *scanner = m_table->create_scanner(scan_spec.get());
75  Cell cell;
76  while (scanner->next(cell)) {
77  (*m_progress) += 1;
78  m_state.total_cells++;
79  m_state.total_bytes += strlen(cell.row_key)
80  + strlen(cell.column_family) + cell.value_len + 16;
81  }
82  delete scanner;
83 
84  int64_t latency = 0;
85  stop_clocks = clock();
86  if (stop_clocks < start_clocks)
87  latency += (int64_t)((double)((std::numeric_limits<clock_t>::max() - start_clocks) + stop_clocks) / clocks_per_usec);
88  else
89  latency += (int64_t)((double)(stop_clocks - start_clocks) / clocks_per_usec);
90  m_state.cum_latency += latency;
91  m_state.cum_sq_latency += ::pow(latency, 2);
92  if (latency < m_state.min_latency)
93  m_state.min_latency = latency;
94  if (latency > m_state.max_latency)
95  m_state.max_latency = latency;
96  }
97  }
98  catch (Exception &e) {
99  HT_ERROR_OUT << e << HT_END;
100  exit(EXIT_FAILURE);
101  }
102 
103  m_state.elapsed_time = stopwatch.elapsed();
104 }
double elapsed()
Returns the elapsed time.
Definition: Stopwatch.h:72
ScanSpec & get()
Returns the built ScanSpec object.
Definition: ScanSpec.h:566
void clear()
Clears the state.
Definition: ScanSpec.h:555
void add_row(const string &str)
Adds a row to be returned in the scan.
Definition: ScanSpec.h:441
STL namespace.
bool has(const String &name)
Check existence of a configuration value.
Definition: Config.h:57
bool next(Cell &cell)
Gets the next cell.
Definition: TableScanner.cc:49
Compatibility Macros for C/C++.
const char * row_key
Definition: Cell.h:66
#define HT_END
Definition: Logger.h:220
Helper class for building a ScanSpec.
Definition: ScanSpec.h:318
Synchronous table scanner.
Definition: TableScanner.h:39
#define HT_ERROR_OUT
Definition: Logger.h:301
Hypertable definitions
void add_column(const string &str)
Adds a column family to be returned by the scan.
Definition: ScanSpec.h:408
Provides an STL-style iterator on DataGenerator objects.
const char * column_family
Definition: Cell.h:67
The Stopwatch class measures elapsed time between instantiation (or a call to start) and a call to st...
Definition: Stopwatch.h:40
This is a generic exception class for Hypertable.
Definition: Error.h:314
uint32_t value_len
Definition: Cell.h:72
Encapsulates decomposed key and value.
Definition: Cell.h:32