@@ -1035,3 +1035,87 @@ func TestConcurrent(t *testing.T) {
10351035 dbt .Logf ("Reached %d concurrent connections \r \n " , max )
10361036 })
10371037}
1038+
1039+ // BENCHMARKS
1040+ var sample []byte
1041+
1042+ func initBenchmarks () ([]byte , int , int ) {
1043+ if sample == nil {
1044+ sample = []byte (strings .Repeat ("0123456789abcdef" , 1024 * 1024 ))
1045+ }
1046+ return sample , 16 , len (sample )
1047+ }
1048+
1049+ func BenchmarkRoundtripText (b * testing.B ) {
1050+ sample , min , max := initBenchmarks ()
1051+ db , err := sql .Open ("mysql" , dsn )
1052+ if err != nil {
1053+ b .Fatalf ("crashed" )
1054+ }
1055+ defer db .Close ()
1056+ var result string
1057+ for i := 0 ; i < b .N ; i ++ {
1058+ length := min + i
1059+ if length > max {
1060+ length = max
1061+ }
1062+ test := string (sample [0 :length ])
1063+ rows , err := db .Query ("SELECT \" " + test + "\" " )
1064+ if err != nil {
1065+ b .Fatalf ("crashed" )
1066+ }
1067+ if ! rows .Next () {
1068+ rows .Close ()
1069+ b .Fatalf ("crashed" )
1070+ }
1071+ err = rows .Scan (& result )
1072+ if err != nil {
1073+ rows .Close ()
1074+ b .Fatalf ("crashed" )
1075+ }
1076+ if result != test {
1077+ rows .Close ()
1078+ b .Errorf ("mismatch" )
1079+ }
1080+ rows .Close ()
1081+ }
1082+ }
1083+
1084+ func BenchmarkRoundtripPrepared (b * testing.B ) {
1085+ sample , min , max := initBenchmarks ()
1086+ db , err := sql .Open ("mysql" , dsn )
1087+ if err != nil {
1088+ b .Fatalf ("crashed" )
1089+ }
1090+ defer db .Close ()
1091+ var result string
1092+ stmt , err := db .Prepare ("SELECT ?" )
1093+ if err != nil {
1094+ b .Fatalf ("crashed" )
1095+ }
1096+ for i := 0 ; i < b .N ; i ++ {
1097+ length := min + i
1098+ if length > max {
1099+ length = max
1100+ }
1101+ test := string (sample [0 :length ])
1102+ rows , err := stmt .Query (test )
1103+ if err != nil {
1104+ b .Fatalf ("crashed" )
1105+ }
1106+ if ! rows .Next () {
1107+ rows .Close ()
1108+ b .Fatalf ("crashed" )
1109+ }
1110+ err = rows .Scan (& result )
1111+ if err != nil {
1112+ rows .Close ()
1113+ b .Fatalf ("crashed" )
1114+ }
1115+ if result != test {
1116+ rows .Close ()
1117+ b .Errorf ("mismatch" )
1118+ }
1119+ rows .Close ()
1120+ }
1121+ }
0 commit comments