Golang Test Instance

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The purpose of this article is to perform unit testing and performance testing on Mymysql.

Preparatory work:

1 Go get Github.com/ziutek/mymysql/thrsafe

2 build table and initialize data in MySQL (DB is test)

drop table if exists admin;  CREATE TABLE ' admin ' (    ' Adminid ' INT (TEN) UNSIGNED not NULL auto_increment,    ' username ' varchar () ' is not null DEFAULT ' COMMENT ' background username ',    ' password ' char (+) not NULL DEFAULT ' COMMENT ' password, MD5 save ',    PRIMARY KEY (' Adminid ') ' after comment= ' User Information table ' collate= ' utf8_general_ci ' engine=innodb;insert into admin set adminid=1, username= ' admin ', password= ' 21232F297A57A5A743894A0E4A801FC3 ';

Build Mymysql under 3 Gopath

Code for 4 Mymysql.go:

Package Mymysqlimport (     "Log"     "Github.com/ziutek/mymysql/mysql"     _ "github.com/ziutek/mymysql/native") Func getadmin (Adminid int) (string, string) {     db: = MySQL. New ("TCP", "", "127.0.0.1:3306", "root", "password", "test")     err: = db. Connect ()     if err! = Nil {          panic (err)     }     rows, res, err: = db.  Query ("select * from admin where adminid=%d", Adminid)     if err! = Nil {          panic (err)     }     If Len (rows) < 1 {          log. Panic ("Rows Error")     }     row: = Rows[0] First     : = Res. Map ("username")     Second: = Res. Map ("password")     username, Password: = row. STR (first), row. STR (second)     return username, password}

Good understanding, get user name and password according to Adminid

Code for 5 Mymysql_test.go:

Package Mymysqlimport (     "testing") Func test_getadmin (t *testing. T) {    username, _: = Getadmin (1)    if (username! = "Admin") {         t.error ("Getadmin Get Data Error")}    }
Here do the unit tests, test the Getadmin function

You can run go test on the command line.

There is a-v parameter, and if you do not add this parameter, only the wrong test case will be displayed, otherwise all test cases (Success + error) will be displayed.

6 Perform performance tests below

Mymysql_b_test.go's Code:

Package Mymysqlimport (     "testing") func Benchmark_getadmin (b *testing. B) {for     I: = 0, i < B.N; i++ {//use b.n for looping            getadmin (1)    }}

Then run go test-v-bench= ". *"

The-bench here is a use case that can be specified to run

The return result indicates that the test case ran 2000 times in 1s, with approximately 891898ns per call

7 Generating a CPU state graph with Performance tests

Use the command:

Go test-bench= ". *"-cpuprofile=cpu.prof-c

Cpuprofile is the CPU profile that represents the build

-C is the generation of an executable binary, which is required to generate a state diagram, which generates an executable file in this directory Mymysql.test

Then use the Go tool pprof tools

Go tool pprof Mymysql.test cpu.prof

Call the Web (requires installation of Graphviz)

The display SVG file has been generated

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.