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