|
| 1 | +from vbench.api import Benchmark |
| 2 | +from datetime import datetime |
| 3 | + |
| 4 | +common_setup = """from pandas_vb_common import * |
| 5 | +import sqlite3 |
| 6 | +import sqlalchemy |
| 7 | +from sqlalchemy import create_engine |
| 8 | +
|
| 9 | +engine = create_engine('sqlite:///:memory:') |
| 10 | +con = sqlite3.connect(':memory:') |
| 11 | +""" |
| 12 | + |
| 13 | +sdate = datetime(2014, 6, 1) |
| 14 | + |
| 15 | + |
| 16 | +#------------------------------------------------------------------------------- |
| 17 | +# to_sql |
| 18 | + |
| 19 | +setup = common_setup + """ |
| 20 | +index = [rands(10) for _ in xrange(10000)] |
| 21 | +df = DataFrame({'float1' : randn(10000), |
| 22 | + 'float2' : randn(10000), |
| 23 | + 'string1' : ['foo'] * 10000, |
| 24 | + 'bool1' : [True] * 10000, |
| 25 | + 'int1' : np.random.randint(0, 100000, size=10000)}, |
| 26 | + index=index) |
| 27 | +""" |
| 28 | + |
| 29 | +sql_write_sqlalchemy = Benchmark("df.to_sql('test1', engine, if_exists='replace')", |
| 30 | + setup, start_date=sdate) |
| 31 | + |
| 32 | +sql_write_fallback = Benchmark("df.to_sql('test1', con, if_exists='replace')", |
| 33 | + setup, start_date=sdate) |
| 34 | + |
| 35 | + |
| 36 | +#------------------------------------------------------------------------------- |
| 37 | +# read_sql |
| 38 | + |
| 39 | +setup = common_setup + """ |
| 40 | +index = [rands(10) for _ in xrange(10000)] |
| 41 | +df = DataFrame({'float1' : randn(10000), |
| 42 | + 'float2' : randn(10000), |
| 43 | + 'string1' : ['foo'] * 10000, |
| 44 | + 'bool1' : [True] * 10000, |
| 45 | + 'int1' : np.random.randint(0, 100000, size=10000)}, |
| 46 | + index=index) |
| 47 | +df.to_sql('test2', engine, if_exists='replace') |
| 48 | +df.to_sql('test2', con, if_exists='replace') |
| 49 | +""" |
| 50 | + |
| 51 | +sql_read_query_sqlalchemy = Benchmark("read_sql_query('SELECT * FROM test2', engine)", |
| 52 | + setup, start_date=sdate) |
| 53 | + |
| 54 | +sql_read_query_fallback = Benchmark("read_sql_query('SELECT * FROM test2', con)", |
| 55 | + setup, start_date=sdate) |
| 56 | + |
| 57 | +sql_read_table_sqlalchemy = Benchmark("read_sql_table('test2', engine)", |
| 58 | + setup, start_date=sdate) |
| 59 | + |
| 60 | + |
| 61 | +#------------------------------------------------------------------------------- |
| 62 | +# type specific write |
| 63 | + |
| 64 | +setup = common_setup + """ |
| 65 | +df = DataFrame({'float' : randn(10000), |
| 66 | + 'string' : ['foo'] * 10000, |
| 67 | + 'bool' : [True] * 10000, |
| 68 | + 'datetime' : date_range('2000-01-01', periods=10000, freq='s')}) |
| 69 | +df.loc[1000:3000, 'float'] = np.nan |
| 70 | +""" |
| 71 | + |
| 72 | +sql_float_write_sqlalchemy = \ |
| 73 | + Benchmark("df[['float']].to_sql('test_float', engine, if_exists='replace')", |
| 74 | + setup, start_date=sdate) |
| 75 | + |
| 76 | +sql_float_write_fallback = \ |
| 77 | + Benchmark("df[['float']].to_sql('test_float', con, if_exists='replace')", |
| 78 | + setup, start_date=sdate) |
| 79 | + |
| 80 | +sql_string_write_sqlalchemy = \ |
| 81 | + Benchmark("df[['string']].to_sql('test_string', engine, if_exists='replace')", |
| 82 | + setup, start_date=sdate) |
| 83 | + |
| 84 | +sql_string_write_fallback = \ |
| 85 | + Benchmark("df[['string']].to_sql('test_string', con, if_exists='replace')", |
| 86 | + setup, start_date=sdate) |
| 87 | + |
| 88 | +sql_datetime_write_sqlalchemy = \ |
| 89 | + Benchmark("df[['datetime']].to_sql('test_datetime', engine, if_exists='replace')", |
| 90 | + setup, start_date=sdate) |
| 91 | + |
| 92 | +#sql_datetime_write_fallback = \ |
| 93 | +# Benchmark("df[['datetime']].to_sql('test_datetime', con, if_exists='replace')", |
| 94 | +# setup3, start_date=sdate) |
| 95 | + |
| 96 | +#------------------------------------------------------------------------------- |
| 97 | +# type specific read |
| 98 | + |
| 99 | +setup = common_setup + """ |
| 100 | +df = DataFrame({'float' : randn(10000), |
| 101 | + 'datetime' : date_range('2000-01-01', periods=10000, freq='s')}) |
| 102 | +df['datetime_string'] = df['datetime'].map(str) |
| 103 | +
|
| 104 | +df.to_sql('test_type', engine, if_exists='replace') |
| 105 | +df[['float', 'datetime_string']].to_sql('test_type', con, if_exists='replace') |
| 106 | +""" |
| 107 | + |
| 108 | +sql_float_read_query_sqlalchemy = \ |
| 109 | + Benchmark("read_sql_query('SELECT float FROM test_type', engine)", |
| 110 | + setup, start_date=sdate) |
| 111 | + |
| 112 | +sql_float_read_table_sqlalchemy = \ |
| 113 | + Benchmark("read_sql_table('test_type', engine, columns=['float'])", |
| 114 | + setup, start_date=sdate) |
| 115 | + |
| 116 | +sql_float_read_query_fallback = \ |
| 117 | + Benchmark("read_sql_query('SELECT float FROM test_type', con)", |
| 118 | + setup, start_date=sdate) |
| 119 | + |
| 120 | +sql_datetime_read_as_native_sqlalchemy = \ |
| 121 | + Benchmark("read_sql_table('test_type', engine, columns=['datetime'])", |
| 122 | + setup, start_date=sdate) |
| 123 | + |
| 124 | +sql_datetime_read_and_parse_sqlalchemy = \ |
| 125 | + Benchmark("read_sql_table('test_type', engine, columns=['datetime_string'], parse_dates=['datetime_string'])", |
| 126 | + setup, start_date=sdate) |
0 commit comments