github.com/nakagami/firebirdsql@v0.9.10/decode_speed_test.go (about)

     1  /*******************************************************************************
     2  The MIT License (MIT)
     3  
     4  Copyright (c) 2018 Chris Heald
     5  
     6  Permission is hereby granted, free of charge, to any person obtaining a copy of
     7  this software and associated documentation files (the "Software"), to deal in
     8  the Software without restriction, including without limitation the rights to
     9  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
    10  the Software, and to permit persons to whom the Software is furnished to do so,
    11  subject to the following conditions:
    12  
    13  The above copyright notice and this permission notice shall be included in all
    14  copies or substantial portions of the Software.
    15  
    16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    18  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    19  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    20  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    21  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    22  *******************************************************************************/
    23  
    24  package firebirdsql
    25  
    26  import (
    27  	"database/sql"
    28  	"log"
    29  	"testing"
    30  )
    31  
    32  func BenchmarkRead(b *testing.B) {
    33  	b.StopTimer()
    34  	conn, err := sql.Open("firebirdsql_createdb", GetTestDSN("test_basic_"))
    35  	query := `
    36  	CREATE TABLE PERFTEST (
    37  		A SMALLINT,
    38  		B INT,
    39  		C BIGINT,
    40  		D VARCHAR(255),
    41  		B1 INT,
    42  		B2 INT,
    43  		B3 INT,
    44  		B4 INT,
    45  		B5 INT,
    46  		B6 INT,
    47  		B7 INT,
    48  		B8 INT,
    49  		B9 INT,
    50  		B11 INT,
    51  		B12 INT,
    52  		B13 INT,
    53  		B14 INT,
    54  		B15 INT,
    55  		B16 INT,
    56  		B17 INT,
    57  		B18 INT,
    58  		B19 INT
    59  	);
    60  	`
    61  	_, err = conn.Exec(query)
    62  	if err != nil {
    63  		b.Error(err)
    64  		log.Fatal(err)
    65  	}
    66  
    67  	tx, err := conn.Begin()
    68  	if err != nil {
    69  		b.Error(err)
    70  	}
    71  
    72  	stmt, err := tx.Prepare("INSERT INTO PERFTEST (A,B,C,D,B1,B2,B3,B4,B5,B6,B7,B8,B9,B11,B12,B13,B14,B15,B16,B17,B18,B19) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
    73  	if err != nil {
    74  		b.Error(err)
    75  		log.Fatal(err)
    76  	}
    77  
    78  	for i := 0; i < b.N; i++ {
    79  		stmt.Exec(i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i, i)
    80  	}
    81  	tx.Commit()
    82  	b.StartTimer()
    83  
    84  	for i := 0; i < b.N; i++ {
    85  		rows, err := conn.Query("SELECT * FROM PERFTEST")
    86  		if err != nil {
    87  			b.Error(err)
    88  		}
    89  
    90  		var vals []interface{}
    91  
    92  		for rows.Next() {
    93  			rows.Scan(vals...)
    94  		}
    95  		rows.Close()
    96  	}
    97  }