|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import argparse |
| 3 | +import ydb |
| 4 | +import time |
| 5 | +import os |
| 6 | + |
| 7 | +ydb.interceptor.monkey_patch_event_handler() |
| 8 | + |
| 9 | + |
| 10 | +def timestamp(): |
| 11 | + return int(1000 * time.time()) |
| 12 | + |
| 13 | + |
| 14 | +def table_name_with_timestamp(): |
| 15 | + return os.path.join("column_table_" + str(timestamp())) |
| 16 | + |
| 17 | + |
| 18 | +class Workload(object): |
| 19 | + def __init__(self, endpoint, database, duration): |
| 20 | + self.database = database |
| 21 | + self.driver = ydb.Driver(ydb.DriverConfig(endpoint, database)) |
| 22 | + self.pool = ydb.SessionPool(self.driver, size=200) |
| 23 | + self.duration = duration |
| 24 | + |
| 25 | + def __enter__(self): |
| 26 | + return self |
| 27 | + |
| 28 | + def __exit__(self, exc_type, exc_val, exc_tb): |
| 29 | + self.pool.stop() |
| 30 | + self.driver.stop() |
| 31 | + |
| 32 | + def create_table(self, table_name): |
| 33 | + with self.pool.checkout() as s: |
| 34 | + try: |
| 35 | + s.execute_scheme( |
| 36 | + """ |
| 37 | + CREATE TABLE %s ( |
| 38 | + id Int64 NOT NULL, |
| 39 | + i64Val Int64, |
| 40 | + PRIMARY KEY(id) |
| 41 | + ) |
| 42 | + PARTITION BY HASH(id) |
| 43 | + WITH ( |
| 44 | + STORE = COLUMN |
| 45 | + ) |
| 46 | + """ |
| 47 | + % table_name |
| 48 | + ) |
| 49 | + |
| 50 | + print("Table %s created" % table_name) |
| 51 | + except ydb.SchemeError as e: |
| 52 | + print(e) |
| 53 | + |
| 54 | + def drop_table(self, table_name): |
| 55 | + with self.pool.checkout() as s: |
| 56 | + try: |
| 57 | + s.drop_table(self.database + "/" + table_name) |
| 58 | + |
| 59 | + print("Table %s dropped" % table_name) |
| 60 | + except ydb.SchemeError as e: |
| 61 | + print(e) |
| 62 | + |
| 63 | + def run(self): |
| 64 | + started_at = time.time() |
| 65 | + |
| 66 | + while time.time() - started_at < self.duration: |
| 67 | + table_name = table_name_with_timestamp() |
| 68 | + self.create_table(table_name) |
| 69 | + |
| 70 | + time.sleep(5) |
| 71 | + |
| 72 | + self.drop_table(table_name) |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + parser = argparse.ArgumentParser( |
| 77 | + description="olap stability workload", formatter_class=argparse.RawDescriptionHelpFormatter |
| 78 | + ) |
| 79 | + parser.add_argument('--endpoint', default='localhost:2135', help="An endpoint to be used") |
| 80 | + parser.add_argument('--database', default=None, required=True, help='A database to connect') |
| 81 | + parser.add_argument('--duration', default=10**9, type=lambda x: int(x), help='A duration of workload in seconds.') |
| 82 | + args = parser.parse_args() |
| 83 | + with Workload(args.endpoint, args.database, args.duration) as workload: |
| 84 | + workload.run() |
0 commit comments