(thread_class)
| 135 | |
| 136 | |
| 137 | def benchmark(thread_class): |
| 138 | options, args = parse_options() |
| 139 | for conn_class in options.supported_reactors: |
| 140 | setup(options) |
| 141 | log.info("==== %s ====" % (conn_class.__name__,)) |
| 142 | |
| 143 | kwargs = {'metrics_enabled': options.enable_metrics, |
| 144 | 'connection_class': conn_class} |
| 145 | if options.protocol_version: |
| 146 | kwargs['protocol_version'] = options.protocol_version |
| 147 | cluster = Cluster(options.hosts, **kwargs) |
| 148 | session = cluster.connect(options.keyspace) |
| 149 | |
| 150 | log.debug("Sleeping for two seconds...") |
| 151 | time.sleep(2.0) |
| 152 | |
| 153 | |
| 154 | # Generate the query |
| 155 | if options.read: |
| 156 | query = "SELECT * FROM {0} WHERE thekey = '{{key}}'".format(TABLE) |
| 157 | else: |
| 158 | query = "INSERT INTO {0} (thekey".format(TABLE) |
| 159 | for i in range(options.num_columns): |
| 160 | query += ", col{0}".format(i) |
| 161 | |
| 162 | query += ") VALUES ('{key}'" |
| 163 | for i in range(options.num_columns): |
| 164 | query += ", {0}".format(COLUMN_VALUES[options.column_type]) |
| 165 | query += ")" |
| 166 | |
| 167 | values = None # we don't use that anymore. Keeping it in case we go back to prepared statements. |
| 168 | per_thread = options.num_ops // options.threads |
| 169 | threads = [] |
| 170 | |
| 171 | log.debug("Beginning {0}...".format('reads' if options.read else 'inserts')) |
| 172 | start = time.time() |
| 173 | try: |
| 174 | for i in range(options.threads): |
| 175 | thread = thread_class( |
| 176 | i, session, query, values, per_thread, |
| 177 | cluster.protocol_version, options.profile) |
| 178 | thread.daemon = True |
| 179 | threads.append(thread) |
| 180 | |
| 181 | for thread in threads: |
| 182 | thread.start() |
| 183 | |
| 184 | for thread in threads: |
| 185 | while thread.is_alive(): |
| 186 | thread.join(timeout=0.5) |
| 187 | |
| 188 | end = time.time() |
| 189 | finally: |
| 190 | cluster.shutdown() |
| 191 | teardown(options) |
| 192 | |
| 193 | total = end - start |
| 194 | log.info("Total time: %0.2fs" % total) |
no test coverage detected