<commit_before>package main

import (
	"database/sql"
	"flag"
	"fmt"
	"math/rand"
	"net/http"
	_ "net/http/pprof"
	"runtime"
	"sync"
	"time"

	_ "github.com/c4pt0r/mysql"
)

var (
	mysqlHost = flag.String("h", "127.0.0.1", "mysql host")
	mysqlPort = flag.Int("p", 3306, "mysql port")
	dbName    = flag.String("db", "test", "db name")

	testType = flag.String("t", "read", "test type: read | write | init")
	N        = flag.Int("n", 10000, "test row count")
	C        = flag.Int("c", 0, "concurrent worker")
)

var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

func randSeq(n int) string {
	b := make([]rune, n)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}
	return string(b)
}

type Result struct {
	Queries  int
	Duration time.Duration
	Allocs   uint64
	Bytes    uint64
}

func (res Result) String() string {
	return fmt.Sprintln(
		" "+res.Duration.String(), "\t   ",
		int(res.QueriesPerSecond()+0.5), "queries/sec\t   ",
		res.AllocsPerQuery(), "allocs/query\t   ",
		res.BytesPerQuery(), "B/query",
	)
}

func (res Result) QueriesPerSecond() float64 {
	return float64(res.Queries) / res.Duration.Seconds()
}

func (res Result) AllocsPerQuery() int {
	return int(res.Allocs) / res.Queries
}

func (res Result) BytesPerQuery() int {
	return int(res.Bytes) / res.Queries
}

var memStats runtime.MemStats

func run(db *sql.DB, testFunc func(*sql.DB) error) Result {
	runtime.GC()

	runtime.ReadMemStats(&memStats)
	var (
		startMallocs    = memStats.Mallocs
		startTotalAlloc = memStats.TotalAlloc
		startTime       = time.Now()
	)

	err := testFunc(db)
	if err != nil {
		panic(err)
	}

	endTime := time.Now()
	runtime.ReadMemStats(&memStats)

	return Result{
		Queries:  *N,
		Duration: endTime.Sub(startTime),
		Allocs:   memStats.Mallocs - startMallocs,
		Bytes:    memStats.TotalAlloc - startTotalAlloc,
	}
}

func NewDb() (*sql.DB, error) {
	dsn := fmt.Sprintf("root:@tcp(%s:%d)/%s", *mysqlHost, *mysqlPort, *dbName)
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		return nil, err
	}
	db.SetMaxIdleConns(50)
	db.SetMaxOpenConns(30)
	return db, nil
}

func mustExec(db *sql.DB, sql string, args ...interface{}) sql.Result {
	res, err := db.Exec(sql, args...)
	if err != nil {
		panic(err)
	}<commit_msg>set max idle connections to 100<commit_after>package main

import (
	"database/sql"
	"flag"
	"fmt"
	"math/rand"
	"net/http"
	_ "net/http/pprof"
	"runtime"
	"sync"
	"time"

	_ "github.com/c4pt0r/mysql"
)

var (
	mysqlHost = flag.String("h", "127.0.0.1", "mysql host")
	mysqlPort = flag.Int("p", 3306, "mysql port")
	dbName    = flag.String("db", "test", "db name")

	testType = flag.String("t", "read", "test type: read | write | init")
	N        = flag.Int("n", 10000, "test row count")
	C        = flag.Int("c", 0, "concurrent worker")
)

var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

func randSeq(n int) string {
	b := make([]rune, n)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}
	return string(b)
}

type Result struct {
	Queries  int
	Duration time.Duration
	Allocs   uint64
	Bytes    uint64
}

func (res Result) String() string {
	return fmt.Sprintln(
		" "+res.Duration.String(), "\t   ",
		int(res.QueriesPerSecond()+0.5), "queries/sec\t   ",
		res.AllocsPerQuery(), "allocs/query\t   ",
		res.BytesPerQuery(), "B/query",
	)
}

func (res Result) QueriesPerSecond() float64 {
	return float64(res.Queries) / res.Duration.Seconds()
}

func (res Result) AllocsPerQuery() int {
	return int(res.Allocs) / res.Queries
}

func (res Result) BytesPerQuery() int {
	return int(res.Bytes) / res.Queries
}

var memStats runtime.MemStats

func run(db *sql.DB, testFunc func(*sql.DB) error) Result {
	runtime.GC()

	runtime.ReadMemStats(&memStats)
	var (
		startMallocs    = memStats.Mallocs
		startTotalAlloc = memStats.TotalAlloc
		startTime       = time.Now()
	)

	err := testFunc(db)
	if err != nil {
		panic(err)
	}

	endTime := time.Now()
	runtime.ReadMemStats(&memStats)

	return Result{
		Queries:  *N,
		Duration: endTime.Sub(startTime),
		Allocs:   memStats.Mallocs - startMallocs,
		Bytes:    memStats.TotalAlloc - startTotalAlloc,
	}
}

func NewDb() (*sql.DB, error) {
	dsn := fmt.Sprintf("root:@tcp(%s:%d)/%s", *mysqlHost, *mysqlPort, *dbName)
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		return nil, err
	}
	db.SetMaxIdleConns(100)
	db.SetMaxOpenConns(100)
	return db, nil
}

func mustExec(db *sql.DB, sql string, args ...interface{}) sql.Result {
	res, err := db.Exec(sql, args...)
	if err != nil {
		panic(err)
	}
{'fraction_non_alphanumeric': 0.16637323943661972, 'fraction_numerical': 0.015184859154929578, 'mean_word_length': 3.734375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4016014', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1804, 'n_words': 397}