I am developing application on Rails and have troubles with performance. Below I have described and provided all technologies, configurations and methods I used. I tried many methods to solve this problem but nothing helped. This is also described below. Can anyone see what the problem is? Thank you!
The problem
Let's open site several times and see log:
INFO -- : Completed 200 OK in 171ms (Views: 149.3ms | ActiveRecord: 7.3ms)
INFO -- : Completed 200 OK in 217ms (Views: 183.7ms | ActiveRecord: 8.9ms)
INFO -- : Completed 200 OK in 221ms (Views: 188.2ms | ActiveRecord: 11.7ms)
INFO -- : Completed 200 OK in 165ms (Views: 143.3ms | ActiveRecord: 7.1ms)
Fine.
Now let's generate load with wrk -t10 -c10 -d10s http://example.com and see log again:
INFO -- : Completed 200 OK in 178ms (Views: 157.6ms | ActiveRecord: 8.1ms)
INFO -- : Completed 200 OK in 270ms (Views: 241.6ms | ActiveRecord: 8.8ms)
INFO -- : Completed 200 OK in 505ms (Views: 460.7ms | ActiveRecord: 12.0ms)
INFO -- : Completed 200 OK in 501ms (Views: 350.8ms | ActiveRecord: 136.0ms)
INFO -- : Completed 200 OK in 777ms (Views: 468.1ms | ActiveRecord: 269.1ms)
INFO -- : Completed 200 OK in 936ms (Views: 624.7ms | ActiveRecord: 285.1ms)
...
INFO -- : Completed 200 OK in 881ms (Views: 617.4ms | ActiveRecord: 251.6ms)
...
INFO -- : Completed 200 OK in 3289ms (Views: 2614.2ms | ActiveRecord: 326.4ms)
INFO -- : Completed 200 OK in 3369ms (Views: 2624.0ms | ActiveRecord: 409.4ms)
INFO -- : Completed 200 OK in 3203ms (Views: 2369.2ms | ActiveRecord: 382.1ms)
...
INFO -- : Completed 200 OK in 431ms (Views: 281.1ms | ActiveRecord: 126.3ms)
...
What are this terrible slowdowns?
wrk -t10 -c10 -d10s http://example.com
Running 10s test # http://example.com
10 threads and 10 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.26s 96.38ms 1.39s 75.00%
Req/Sec 0.00 0.00 0.00 100.00%
36 requests in 10.09s, 0.97MB read
Socket errors: connect 0, read 0, write 0, timeout 32
Requests/sec: 3.57
Transfer/sec: 98.62KB
Rails
I am using latest Rails version - 4.2.4.
bundle list output - github gist
application.rb - github gist
production.rb - github gist
database.yml - github gist
I am using acts_as_paranoid for all my models. (WHERE deleted_at IS NULL)
All N+1 queries are eliminated (analyzed them manually and with bullet gem).
All tables are InnoDB and have a lot of indices. 99% of tables have 50 or less rows, the rest 1% - 200 or less. That's very low amount of data.
Page I am benchmarking requires around 30-35 SQL queries and around 30 partials.
Queries using...
- COUNT - 0
- GROUP BY - 1
- ORDER BY - 10
- LIMIT - 10
- JOIN - 1 (joins one table)
- Use of MAX, MIN, CONCAT and other functions - NO
My templates are written in Slim.
Server configuration
CPU - Intel one core Sandy Bridge CPU
RAM - 1GM
Disk Type - SSD
Operating System
Ubuntu 14.04 LTS 64bit
Swap 1GB. Optimized swap settings:
cat /proc/sys/vm/swappiness => 10
cat /proc/sys/vm/vfs_cache_pressure => 50
Database
MySQL 5.5. Tried to optimize with mysqltuner and tuning-primer.
Current configuration: github gist
Latest mysqltuner report: perl mysqltuner.pl --buffers --dbstat --outputfile result_mysqltuner.txt github gist
Latest tuning-primer report: ./tuning-primer.sh github gist
I have notices the warning Temporary tables created on disk. That's because I have TEXT columns in MySQL. Googling I found that mounting MySQL temporary directory in memory is good idea. So I did it: tmpdir /var/mysqltmp. Followed this guide.
mytop output:
Queries: 37.4k qps: 0 Slow: 1.9k Se/In/Up/De(%): 91/00/00/00
Sorts: 0 qps now: 1 Slow qps: 0.0 Threads: 4 ( 1/ 2) 00/00/00/00
Cache Hits: 29.0k Hits/s: 0.4 Hits now: 0.0 Ratio: 85.5%
Ratio now: 0.0%
Key Efficiency: 99.4% Bps in/out: 75.8/ 1.9k Now in/out: 22.6/ 2.1k
Id User Host/IP DB Time Cmd State Query
-- ---- ------- -- ---- --- ----- ----------
1444 root localhost site 73737 Sleep
2337 root localhost site 658 Sleep
2340 root localhost site 322 Sleep
2342 root localhost 0 Query show full processlist
Ruby interpreter
Rbenv: rbenv -v => rbenv 0.4.0-154-g9e664b5
Ruby: ruby -v => ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]
My ruby is compiled with jemalloc (better memory allocation):
RbConfig::CONFIG['LIBS'] => "-lpthread -ljemalloc -ldl -lcrypt -lm "
Followed this guide.
I have tried both ruby with default and custom memory allocation. Ruby with jemalloc seems to be faster.
Web server
nginx -v => nginx version: nginx/1.8.0
Current nginx.conf (simplified): github gist
Current nginx site conf (simplified): github gist
Application server
I have tried both Passenger and Puma. But without result.
Phusion Passenger
passenger -v => Phusion Passenger version 5.0.18
Configuration: github gist
Puma
bundle exec puma -v => puma version 2.13.4
Also I have tried Puma server in both cluster (1 worker) and thread models.
Here is latest config for Puma in thread mode: github gist
Related
I am a beginner to OpenOCD and I am trying to flash 3 STM32 targets in a daisy chain with an ST-Link v2 debugger or OLIMEX as shown below using OpenOCD.
The code that I use works if only one Target is connected. But if I connect it to more than one Target, OpenOCD throws an error stating that init failed.
"C:\Program Files\GNU ARM Eclipse\OpenOCD\0.10.0-201601101000-dev\bin\openocd" -f "C:\Program Files\GNU ARM Eclipse\OpenOCD\0.10.0-201601101000-dev\scripts\interface\stlink-v2.cfg" -f "C:\Program Files\GNU ARM Eclipse\OpenOCD\0.10.0-201601101000-dev\scripts\target\stm32f3x.cfg" -c init -c targets -c "halt" -c "flash erase_sector 0 0 127" -c "reset halt" -c "flash write_image C:/Users/Buero-1/Desktop/openOCD/init.hex" -c "verify_image C:/Users/Buero-1/Desktop/openOCD/init.hex" -c "reset run" -c shutdown
A successful result I get when this code is executed is shown below.
GNU ARM Eclipse 64-bits Open On-Chip Debugger 0.10.0-dev-00287-g85cec24-dirty (2016-01-10-10:13)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
none separate
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : STLINK v2 JTAG v29 API v2 SWIM v7 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.223311
Info : stm32f3x.cpu: hardware has 6 breakpoints, 4 watchpoints
TargetName Type Endian TapName State
-- ------------------ ---------- ------ ------------------ ------------
0* stm32f3x.cpu hla_target little stm32f3x.cpu halted
Info : device id = 0x20006432
Info : flash size = 256kbytes
erased sectors 0 through 127 on flash bank 0 in 0.025984s
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
adapter speed: 950 kHz
stm32f3x.cpu: target state: halted
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc
Info : Padding image section 0 with 31880 bytes
Info : Padding image section 1 with 1 bytes
stm32f3x.cpu: target state: halted
target halted due to breakpoint, current mode: Thread
xPSR: 0x61000000 pc: 0x2000003a msp: 0xfffffffc
wrote 47676 bytes from file Z:/Elektronik/GSV13/Fertigung_GSV-13iu/Init/GSV13init_Ver1_6.hex in 1.635796s (28.462 KiB/s)
stm32f3x.cpu: target state: halted
target halted due to breakpoint, current mode: Thread
xPSR: 0x61000000 pc: 0x2000002e msp: 0xfffffffc
stm32f3x.cpu: target state: halted
target halted due to breakpoint, current mode: Thread
xPSR: 0x61000000 pc: 0x2000002e msp: 0xfffffffc
verified 15795 bytes in 0.483325s (31.914 KiB/s)
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
adapter speed: 950 kHz
shutdown command invoked
But as mentioned, if I connect multiple targets in a JTAG chain, the process stops at init and the program ends.
The config files are target/stm32f3x.cfg, interface/ftdi/olimex-arm-usb-ocd-h.cfg and interface/stlink-v2.cfg.
Please excuse me if my question was very basic. It would be of great help if I am provided an update on how to proceed with my problem.
Thank you.
As far as I know STLink v2 does not support daisy chain.
https://community.st.com/s/question/0D50X00009XkZTdSAN/does-stlink-utility-support-multiple-devices-on-jtag-chain
I have a small site running and only 20 suppliers used to access this sites for queries. The server is running on high load during the peak hours. Please find the output below:
top - 10:15:42 up 32 days, 20:08, 4 users, load average: 2.20, 2.06, 1.94
Tasks: 500 total, 1 running, 498 sleeping, 0 stopped, 1 zombie
Cpu(s): 7.1%us, 2.3%sy, 0.0%ni, 90.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 32931056k total, 3124852k used, 29806204k free, 49508k buffers
Swap: 3999740k total, 0k used, 3999740k free, 1364836k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
10130 mysql 20 0 6207m 567m 5468 S 232 1.8 14306:04 mysqld
27534 worldsto 20 0 307m 20m 5364 S 5 0.1 0:01.97 apache2
29237 worldsto 20 0 299m 12m 3696 S 2 0.0 0:00.07 apache2
29003 worldsto 20 0 299m 13m 3716 S 1 0.0 0:00.12 apache2
root#server70:~# ps -ef | grep apache | wc
434 2368 17756
CPU(s): 24
RAM size: 32 GB
From what I have seen from the Apache logs, all the connections are coming from suppliers and company IP addresses. I am sure there is something wrong with the Apache process so that MYSQL is using more CPU load.
Please someone help me to identify and fix this problem. Thanks
The best troubleshooting step you can do is this:
connect to your MySQL server process, and type:
SHOW FULL PROCESSLIST
That will show you every query that's running. You will probably see the same query showing up multiple times, perhaps with different ID's - maybe something like:
SELECT * FROM foo WHERE fooid='1'
SELECT * FROM foo WHERE fooid='2'
...etc...
That means you need an index on 'fooid'.
We have Perl mytop of Jeremy Zawodny version 2009-04-06 installed on Debian Squeeze OS, with MySQLd version 5.1.53, using apt-get command. I redo the "apt-get install mytop" that indicates that no newer version of mytop is available.
This version of mytop seems outdated, as it gives systematically very low value of queries done by MySQLd. In fact, it uses the status query to get the total queries since uptime:
SHOW STATUS LIKE 'Questions';
It yields an error result in the new version mysqld. In effect to get mysql total number of queries since uptime, the new mysqld server shifted the number of queries to 'Queries' instead of 'Questions':
SHOW STATUS LIKE 'Queries';
You can see an enormous difference between the two variables by:
mysql> SHOW STATUS LIKE 'Que%';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| Queries | 135903 |
| Questions | 160 |
+---------------+--------+
2 rows in set (0.00 sec)
that gives the both values of 'Queries' and 'Questions'.
mytop -uJohnDoe2 -ppassword
Here is the original mytop output:
MySQL on localhost (5.1.63-0+squeeze1-log) up 0+01:42:35 [13:36:44]
Queries: 265.0 qps: 0 Slow: 0.0 Se/In/Up/De(%): 14760/00/00/00
qps now: 0 Slow qps: 0.0 Threads: 5 ( 1/ 5) 1500/00/00/00
Key Efficiency: 100.0% Bps in/out: 0.9/173.8 Now in/out: 8.3/ 1.5k
Id User Host/IP DB Time Cmd Query or State
-- ---- ------- -- ---- --- --------------
28019 root localhost 0 Query show full processlist
....
I copied mytop to mytop.pl, and replaced in the source Perl code the string "Questions" by "Queries", and run
mytop.pl -uJohnDoe2 -ppassword
And the modifed mytop.pl gives a more realistic monitoring:
MySQL on localhost (5.1.63-0+squeeze1-log) up 0+01:42:23 [13:36:32]
Queries: 136.1k qps: 23 Slow: 0.0 Se/In/Up/De(%): 28/00/00/00
qps now: 18 Slow qps: 0.0 Threads: 5 ( 1/ 5) 27/00/00/00
Key Efficiency: 100.0% Bps in/out: 0.1/ 18.3 Now in/out: 8.4/ 1.5k
Id User Host/IP DB Time Cmd Query or State
-- ---- ------- -- ---- --- --------------
30789 root localhost 0 Query show full processlist
....
Have you observed this problem in your system? i.e.
Perl monitor of MySQL is now invalid in terms of Queries/Questions since MySQLd 5.1.63?
ADDED:
After reading the answer of Shlomi Noach, I added this link for modified Perl script file:
mytop.pl.
I have indeed noticed this change, and blogged about it: questions or queries?
Apparently this came as a surprise to other monitoring tools (Innotop, MonYOG) developers.
With regard you case you have two very simple options:
Switch to innotop instead
Modify the source code for mytop and replace Questions with Queries.
The change was made in 5.1.31; in aforementioned post you can also read comment by an Oracle employee.
Application details :
Rails 3.1.0
Ruby 1.9.2
unicorn 4.2.0
resque 1.20.0
nginx/1.0.14
redis 2.4.8
I am using active_admin gem, for all URL's getting response 200,
but only one URL giving 502 error on production.
rake routes :
admin_links GET /admin/links(.:format) {:action=>"index", :controller=>"admin/links"}
And its working on local(development).
localhost log : response code 200
Started GET "/admin/links" for 127.0.0.1 at 2013-02-12 11:05:21 +0530
Processing by Admin::LinksController#index as */*
Parameters: {"link"=>{}}
Geokit is using the domain: localhost
AdminUser Load (0.2ms) SELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 3 LIMIT 1
(0.1ms) SELECT 1 FROM `links` LIMIT 1 OFFSET 0
(0.1ms) SELECT COUNT(*) FROM `links`
(0.2ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM `links` LIMIT 10 OFFSET 0) subquery_for_count
CACHE (0.0ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM `links` LIMIT 10 OFFSET 0) subquery_for_count
Link Load (0.6ms) SELECT `links`.* FROM `links` ORDER BY `links`.`id` desc LIMIT 10 OFFSET 0
Link Load (6677.2ms) SELECT `links`.* FROM `links`
Rendered /usr/local/rvm/gems/ruby-1.9.2-head/gems/activeadmin-0.4.2/app/views/active_admin/resource/index.html.arb (14919.0ms)
Completed 200 OK in 15663ms (Views: 8835.0ms | ActiveRecord: 6682.8ms | Solr: 0.0ms)
production log : 502 response
Started GET "/admin/links" for 103.9.12.66 at 2013-02-12 05:25:37 +0000
Processing by Admin::LinksController#index as */*
Parameters: {"link"=>{}}
NGinx error log
2013/02/12 07:36:16 [error] 32401#0: *1948 upstream prematurely closed connection while reading response header from upstream
don't know what's happening, could some buddy help me out.
You have a timeout problem.
Tackling it
HTTP/1.1 502 Bad Gateway
Indicates, that nginx had a problem to talk to its configured upstream.
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#502
2013/02/12 07:36:16 [error] 32401#0: *1948 upstream prematurely closed connection while reading response header from upstream
Nginx error log tells you Nginx was actually able to connect to the configured upstream but the process closed the connection before the answer was (fully) received.
Your development environment:
Completed 200 OK in 15663ms
Apparently you need around 15 seconds to generate the response on your development machine.
In contrast to proxy_connect_timeout, this timeout will catch a server
that puts you in it's connection pool but does not respond to you with
anything beyond that. Be careful though not to set this too low, as
your proxy server might take a longer time to respond to requests on
purpose (e.g. when serving you a report page that takes some time to
compute). You are able though to have a different setting per
location, which enables you to have a higher proxy_read_timeout for
the report page's location.
http://wiki.nginx.org/HttpProxyModule#proxy_read_timeout
On the nginx side the proxy_read_timeout is at a default of 60 seconds, so that's safe
I have no idea how ruby (on rails) works, check the error log - the timeout happens in that part of your stack
I have placed an after_commit callback in the RequestToken model that outputs "Committed Request Token xx". You can see in the log I included below, that the token record is committed and the next request the lookup on the object says it cannot be found. The issue occurs intermittently and if I refresh the page the record is found and the request goes through.
Environment
AWS EC2 + RDS, Ubuntu 10.04, Rails 3.2.8, MySQL2 0.3.11 gem, apache2 2.2.14, phusion passenger 3.0.11
Has anyone seen this before? Any suggestions?
Committed Request Token S8j311QckvEjnDftNW0e7FPHsavGWTelONcsE3X1
Rendered text template (0.0ms)
Completed 200 OK in 28ms (Views: 0.6ms | ActiveRecord: 21.8ms | Sphinx: 0.0ms)
Started GET "/oauth/authorize?oauth_token=S8j311QckvEjnDftNW0e7FPHsavGWTelONcsE3X1" for 96.236.148.63 at 2012-10-15 22:07:32 +0000
Processing by OauthController#authorize as HTML
Parameters: {"oauth_token"=>"S8j311QckvEjnDftNW0e7FPHsavGWTelONcsE3X1"}
Completed 500 Internal Server Error in 5ms
ActiveRecord::RecordNotFound (Couldn't find RequestToken with token = S8j311QckvEjnDftNW0e7FPHsavGWTelONcsE3X1):
200 Doesn't mean it saved. Probably failed a validation.