Skip to content

Instantly share code, notes, and snippets.

@shenli shenli/main.go
Created Feb 13, 2018

Embed
What would you like to do?
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"database/sql"
"flag"
"fmt"
"sync"
_ "github.com/go-sql-driver/mysql"
)
var (
host = flag.String("host", "0.0.0.0", "tidb server host")
port = flag.String("port", "4000", "tidb server port")
database = flag.String("db", "test", "The socket file to use for connection.")
tbl = flag.String("table", "t", "The socket file to use for connection.")
threads = flag.Int("threads", 10, "The socket file to use for connection.")
max = flag.Int("max", 540599999793, "The socket file to use for connection.")
wg sync.WaitGroup
)
func main() {
flag.Parse()
uri := "root:@tcp(" + *host + ":" + *port + ")/" + *database
fmt.Println("URI: ", uri)
db, err := sql.Open("mysql", uri)
if err != nil {
panic(err.Error()) // Just for example purpose. You should use proper error handling instead of panic
}
defer db.Close()
run(*max, *threads, db)
}
func run(max, threads int, db *sql.DB) {
step := max / threads
c := 0
fmt.Printf("Max %d, threads %d\n", max, threads)
for c <= max {
query := fmt.Sprintf("select * from `%s` where iid >= %d and iid <= %d;", *tbl, c, c+step)
c = c + step
fmt.Printf("Max %d, threads %d, c %d \n", max, threads, c)
wg.Add(1)
go dump(query, db)
}
wg.Wait()
}
func dump(query string, db *sql.DB) {
defer wg.Done()
fmt.Println("Run Query: ", query)
tx, err := db.Begin()
rows, err := tx.Query(query)
if err != nil {
panic(err.Error())
}
cnt := 0
for rows.Next() {
cnt++
}
err = rows.Err()
if err != nil {
panic(err.Error())
}
fmt.Printf("Get %d rows\n", cnt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.