Benchmark of integration test (?) I'm not even sure if this is efficient enough

blov · · 516 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello!</p> <p>So, I have this project I&#39;ve been working for a few months now. Here is one concern I have with it: benchmarking.</p> <p>I wrote an integration test, not large enough (read: covering the entire application) thought, which happen to be a benchmark test too, here is the code:</p> <pre><code>package main import ( &#34;log&#34; &#34;testing&#34; &#34;github.com/go-telegram-bot-api/telegram-bot-api&#34; mgo &#34;gopkg.in/mgo.v2&#34; redis &#34;gopkg.in/redis.v4&#34; ) // Borra la base de datos y limpia Redis func teardown() { config := LoadConfig() // MongoDB mongo, err := mgo.Dial(config.DB.Address) if err != nil { log.Fatalf(&#34;No se pudo conectar a MongoDB: %s&#34;, err) } defer mongo.Close() err = mongo.DB(config.DB.Name).DropDatabase() if err != nil { log.Fatalf(&#34;No se pudo borrar la base de datos: %s&#34;, err) } // Redis redis := redis.NewClient(&amp;redis.Options{ Addr: config.Redis.Address, Password: config.Redis.Password, DB: config.Redis.DB, }) err = redis.FlushDb().Err() if err != nil { log.Fatalf(&#34;No se pudo borrar la base de datos Redis: %s&#34;, err) } } func BenchmarkReaction(b *testing.B) { defer teardown() // preparamos al bot config := LoadConfig() fe, err := NewFrontend(config) if err != nil { log.Fatalf(&#34;No se pudo crear Frontend para pruebas de integración: %s&#34;, err) } fe.RegisterSessions( &amp;MainMenu{name: &#34;main menu&#34;}, &amp;SelectLanguage{name: &#34;select language&#34;}, ) // canal de actualizaciones sendupdates := make(chan tgbotapi.Update) // canal para recibir respuestas answers := make(chan tgbotapi.Chattable) go func() { for { select { case &lt;-answers: // hacemos nada, porque no nos interesa el // mensaje de vuelta } } }() // Iniciamos el bot go fe.Listen(sendupdates, answers) for n := 0; n &lt; b.N; n++ { // presentación sendupdates &lt;- tgbotapi.Update{ Message: &amp;tgbotapi.Message{ From: &amp;tgbotapi.User{ ID: n, FirstName: &#34;Dummy&#34;, LastName: &#34;MacDummy&#34;, UserName: &#34;DD&#34;, }, Text: &#34;/start&#34;, }, } // Registro luego de escoger el nombre de usuario sendupdates &lt;- tgbotapi.Update{ Message: &amp;tgbotapi.Message{ From: &amp;tgbotapi.User{ ID: n, FirstName: &#34;Dummy&#34;, LastName: &#34;MacDummy&#34;, UserName: &#34;DD&#34;, }, Text: &#34;Español&#34;, }, } } log.Printf(&#34;Registrado %d usuarios&#34;, b.N) } </code></pre> <p>The thing here tested is the first two interactions of the user with the app, he is registered in the first interaction (this is a Telegram Messenger bot) and by the second intereaction some data is retrieved from the database (What menu should be shown to the user) and the file system (Internationalization). Some stuff is cached on first read using Redis too.</p> <p>Anyway, I ran the benchmark a couple of times and these are the results:</p> <pre><code>→ go test -bench=. PASS BenchmarkReaction-2 2016/08/23 19:27:34 Roles y actividades registrados en la base de datos 2016/08/23 19:27:34 Registrado 1 usuarios 2016/08/23 19:27:35 Roles y actividades registrados en la base de datos 2016/08/23 19:27:35 Registrado 2 usuarios # [···] 2016/08/23 19:27:39 Registrado 50 usuarios 2016/08/23 19:27:39 Roles y actividades registrados en la base de datos 2016/08/23 19:27:40 Registrado 100 usuarios 100 10514644 ns/op ok bitbucket.org/capslockdev/betsy 6.344s → go test -bench=. PASS BenchmarkReaction-2 2016/08/23 19:28:31 Roles y actividades registrados en la base de datos 2016/08/23 19:28:31 Registrado 1 usuarios 1 2209585800 ns/op ok bitbucket.org/capslockdev/betsy 2.350s </code></pre> <p>And, I&#39;m very worried that registering and interacting with 100 users (right? that&#39;s how I should read it?) takes 0.00010514644 seconds for each user, or that it later took 2.2095858 seconds to register and interact with just ONE user...</p> <p>Well, actually what buggies me is the fact that I don&#39;t know if these numbers are something to call &#34;slow&#34;, I added the cache for my MongoDB before writing this benchmark, and I also don&#39;t feel like trusting these numbers I get from the benchmark, the sudden change from 100 to 1 and the time differences gave trust issues. I&#39;m testing in a Thinkpad T60 running GNU/Linux, it should be an acceptable environment for benchmarking I guess.</p> <p>So, to wrap this here: * am I doing this benchmarking thing right? * Is there a way to get more information than the time it took for the app to complete the test? * Exists any way to get &#34;control&#34; data to compare my benchmark, besides removing some performance-saving feature from my project (like the DB cache) and running the benchmark again?</p>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

516 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传