Friday, July 29, 2016

TIP: running 2N+1 Gunicorn workers

If you read, people tend to say that for web services, run 2N+1 workers, where N is the number of CPUs.

It turns out the Intarwebs are wrong about exactly how to do this. Here's my solution based on "epicbrew"'s work on Stack Overflow:

gunicorn --workers=$((2 * $(getconf _NPROCESSORS_ONLN) + 1)) wsgi:application
The cool thing is that it works on both macOS and Linux!

Thursday, July 28, 2016

pragmatic Test Driven Development

As a dev, I write mostly unit tests and care about test coverage increasing over time.  My view has shifted recently.

Although web UI tests can be fragile, they present a lot of bang for the buck, coverage-wise.  It turns out they also have a lot of unique business value; that is, if we're testing what our users see, then when we change something we can immediately know that business value is being affected.

- I recommend view-level tests. Django directly supports and advocates this style. They're fast and easy to create like Unit tests, but have direct user-level (thus business-level) value.

- for UI tests, set an ID on everything that you are testing. (Never ever use XPath.)  This makes UI tests more durable and reliable. 

- Kent Beck has been the main proponent of TDD from the very beginning, as part of Extreme Programming. “I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence”

- I really enjoy Brian Okken's entirely pragmatic view on testing:
My reaction to “Is TDD Dead?”  His podcast is in my Top 3: Python Testing

Wednesday, July 20, 2016

TIP: kill service on port in macOS

Example: Postgres, which runs on port 5432:

lsof -Pn | awk '/:5432..LISTEN/ {print $2}' | xargs -t kill

Does anyone know if Brew or something has the equivalent of good old Linux fuser -k 5432 ?

Tuesday, July 12, 2016

TIP: edit files with Git merge conflicts

subl $(git diff --name-only --diff-filter=U)

This is even easier if you have a Git alias:

subl $(git unmerged)

In both cases it uses the Sublime text editor to open any file that has a Git merge conflict.

Monday, July 4, 2016

TIP: Git Grep searches your project really fast

Searching inside a large project of source code can be a challenge

In my codebase there's lots of "delete" methods, but only a few "_delete_" methods. If I search for "delete", I'll get zillions of false positives. By using the super-fast Git Grep and also "\W" for matching non-word-characters, I can find all references of the target string "_delete_":

$ git grep '\W_delete_\W'
app/looks_services.py:390:        order._delete_()
app/models.py:2634:    def _delete_(self, **kwargs):

talk: Functional Programming and Django QuerySets

I'll be giving this talk Tuesday!  As this is the Django Meetup, I'm expanding the Django section and adding more general testing tips.

I'll post a link on this site and the twitters when the slides and video are available.