1

I found that my Sinatra application was so freaking slow, only happens on VMWare image on i3, i don't know why, it doesn't happened on same VMWare image on AMD APU, the line that causes the slowness is:

DataMapper.setup(:default, 'postgres://myuser:mypassword@127.0.0.1/mydbname')

it tooks almost 40-45 seconds to run that line wnen on VMWare image on i3, and i don't know why, is there any way to overcome this? this happens on Pry/IRB, on Ruby and JRuby.

It doesn't happened when using active_record or psql command line:

ActiveRecord::Base.establish_connection( adapter: 'postgresql', host: '127.0.0.1', database: 'mydbname', username: 'mypassword', password: 'mypassword', port: 5432 )

Ruby version: ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]

PostgreSQL version: postgres (PostgreSQL) 9.3.1

JRuby version: jruby 1.7.5 (1.9.3p392) 2013-10-07 74e9291 on OpenJDK Client VM 1.7.0_40-b60 [linux-i386]

Kokizzu
  • 24,974
  • 37
  • 137
  • 233
  • 1
    Check /etc/hosts - I'm not sure about the specifics around 127.0.0.1 but it may be that for some reason you don't have a localhost entry there and dns lookups are being performed? Though this should raise unknown host exception. And 127.0.0.1 is supposed to always be local loopback. – mcfinnigan Oct 16 '13 at 07:38
  • my /etc/hosts content: `127.0.0.1 localhost.localdomain localhost` and `::1 localhost.localdomain localhost` – Kokizzu Oct 16 '13 at 07:40
  • the `dig localhost` command are about 5ms, `dig 127.0.0.1` about 0ms.. – Kokizzu Oct 16 '13 at 07:43
  • 1
    What happens if you update your connection string to point at `localhost` rather than `127.0.0.1` - does it make any difference? Also, are you able to run any sort of packet inspection to see whether there is any evidence of traffic during those 45 seconds? – mcfinnigan Oct 16 '13 at 07:47
  • i receive no data when `tcpdump 'port 5432' -i lo` when running DataMapper.setup using pry/irb, but receive some data when using `psql -h localhost -U myusername mydbname` – Kokizzu Oct 16 '13 at 08:12
  • `localhost` and `127.0.0.1` has no difference, still ~45 seconds – Kokizzu Oct 16 '13 at 08:13
  • 1
    That is very odd. What postgres jdbc driver are you using? Also, is there any chance of testing this with ActiveRecord so that you can determine whether it's a driver issue or a system issue? – mcfinnigan Oct 16 '13 at 08:24
  • i've tried with `active-record`'s `ActiveRecord::Base.establish_connection` completed in less than a second. – Kokizzu Oct 16 '13 at 08:45
  • 1
    You might want to add that to your original question body. I'm out of ideas, hopefully someone more knowledgable will be able to step in and help :-) – mcfinnigan Oct 16 '13 at 08:46
  • 1
    As others said that's for sure networking issue: For some reason it your application can't find the localhost/127.0.0.1 right away. – patm Oct 16 '13 at 09:37
  • 1
    Time to hit the debugger and step through the DataMapper setup method and see where it hangs. – Casper Oct 16 '13 at 09:41
  • after i trace it using pry, yes, the problem was slow `gethostbyname`, so i add one more line `127.0.0.1 myhostname` to `/etc/hosts`, it works fast, only 1 second needed to connect ^^ thank you all.. – Kokizzu Oct 21 '13 at 01:47

1 Answers1

0

this command solves my problem:

echo 127.0.0.1 `cat /etc/hostname` >> /etc/hosts
Kokizzu
  • 24,974
  • 37
  • 137
  • 233