Archive for the 'Code' Category

Free your Mac memory for free


Mac OSX is beautiful, not only in its design, but also in its memory management. However, sometimes the inactive memory takes long time before released.

According to Apple support Inactive memory is

information in memory is not actively being used, but was recently used.

For example, if you’ve been using Mail and then quit it, the RAM that Mail was using is marked as Inactive memory. This Inactive memory is available for use by another application, just like Free memory.

However, if you open Mail before its Inactive memory is used by a different application, Mail will open quicker because its Inactive memory is converted to Active memory, instead of loading Mail from the slower hard disk.

In many cases you will need to jump from a heavy application into another, and then you will need either to accept the system performance, which is not deadly bad, or use a simple command line to free your memory.

First, how to monitor your memory?
Mac OS X comes with Activity Monitor Application that gives you a glimpse about your system resources, one major resource is your memory.
Read Apple help on Reading system memory usage in Activity Monitor

Now, how to free the inactive memory without having to reboot?

Open your terminal app and run
purge

Checking the ManPage of Purge, purge task defined as force disk cache to be purged (flushed and emptied) with a description Purge can be used to approximate initial boot conditions with a cold disk buffer cache for performance analysis. It does not affect anonymous memory that has been allocated through malloc, vm_allocate, etc.

No need to download an app to free your memory and moreover, you do not need to buy an app for that purpose, it comes for free.

Posted on Friday, December 30th, 2011
Under: Code, Community, Mac, Software | No Comments »

Postfix options


Last week I got call from one of my colleagues complaining about the amount of spam she’s receiving, so I installed SA and implemented some restricted options in postfix

smtpd_recipient_restrictions =
permit_sasl_authenticated,
permit_mynetworks,
reject_unauth_destination,
reject_invalid_hostname,
reject_non_fqdn_hostname,
reject_non_fqdn_sender,
reject_rbl_client list.dsbl.org,
reject_rbl_client cbl.abuseat.org,
reject_rbl_client, dnsbl.sorbs.net,
reject_rbl_client spam.dnsbl.sorbs.net,
reject_rbl_client korea.services.net,
reject_rbl_client zen.spamhaus.org,
reject_rbl_client pbl.spamhaus.org,
reject_rbl_client sbl.spamhaus.org,
permit

but later she called complaining that she cannot send emails using our SMTP server so for me it was what the hell is going one? later I found that Postfix is picky about the order of your options so if you do reject_non_fqdn_hostname before permitting sasl authenticated users, those authenticated will not be able to send emails.

and this was new to me.

Tags: , , , , , , , , , , , ,

Posted on Tuesday, February 13th, 2007
Under: Code, HowTo, Linux | No Comments »

Testing your code


Perhaps no other coding practice is as important as testing your code. Also in the nature of Business Development, where parts of your code always change on the request of a client (including Management), or even when you want to make your code run with better performance, Automated tests are highly needed, you can’t just spread your print statements all over your code every time you need to test it.
With automated tests, you can just be sure that your interface is not broken after some change, you just run your tests, if they succeed, you know that you didn’t break your code (this means you should right good tests).
Let’s start by a simple example, Imagine that we have been asked to test PHP’s built-in Array. One bit of functionality to test is the function sizeof(). For a newly created array we expect the sizeof() function to return 0. After we add an element, sizeof() should return 1. (I know it’s not a big deal, but it’s just an example).
you can do it with simple print statements (i.e. print (1 == sizeof($myArray) ? "OK":"Failure").PHP_EOL; ), but here we are going to do it by using an assertion function.


< ?php
$fixture = Array();
assertTrue(sizeof($fixture) == 0);

$fixture[] = "element";
assertTrue(sizeof($fixture) == 1);

function assertTrue($condition) {
if (!$condition) {
throw new Exception("Assertion failed.");
}
}
?>

Well, we could know if something went wrong if some Exception was raised, of course assertTrue is not the only thing you want to do, UnitTesting has gone far beyond that, usually UnitTesting frameworks provide a whole lot of features that you need for your testing.
Lot’s of Testing frameworks are available for PHP, I’m going to point to 2 of them, the first one is SimpleTest (http://simpletest.org/), it’s written in PHP 4 (Although they are going to migrate to PHP 5 when they reach version 2), so you can use it on both PHP 4 and 5 programs, also it includes a nice HTML reporter for some good looking html results the same test above could be written using this framework as:

< ?php
require_once 'simpletest/unit_tester.php';
require_once 'simpletest/reporter.php';

class TestingFixtureArray extends UnitTestCase {
function TestArray() {
$this->UnitTestCase(“Testing Fixture Array”);
$fixture = Array();
$this->assertTrue(sizeof($fixture) == 0);

$fixture[] = “element”;
$this->assertTrue(sizeof($fixture) == 1);
}
}

//run the Test
$test = new TestingFixtureArray();
$test->run(new HtmlReporter());
?>

Another Well know testing framework is known as PHPUnit2 (http://www.phpunit.de/), it’s available through the PEAR Repository, you can install it as
$ pear install PHPUnit2
It’s written in PHP 5, and is so widely used within PHP Developers, a good resource for learning how to use it is the free available online book PHPUnit Pocket Guide (http://www.phpunit.de/pocket_guide/index.en.php), the same example could be:

< ?php
require_once 'PHPUnit2/Framework/TestCase.php';

class ArrayTest extends PHPUnit2_Framework_TestCase {
public function testNewArrayIsEmpty() {
// Create the Array fixture.
$fixture = Array();

// Assert that the size of the Array fixture is 0.
$this->assertEquals(0, sizeof($fixture));
}

public function testArrayContainsAnElement() {
// Create the Array fixture.
$fixture = Array();

// Add an element to the Array fixture.
$fixture[] = ‘Element’;

// Assert that the size of the Array fixture is 1.
$this->assertEquals(1, sizeof($fixture));
}
}
?>

and you can run it on command line as
$ phpunit ArrayTest

Additional benefits that you can realize by thoroughly testing your code:

  • Testing forces you to write code that is easily testable. This leads to looser coupling, flexible designs, and good modularity.
  • Writing tests forces you to explicitly clarify your expectations of how your code is to behave, distilling your design into sharper focus from the beginning. Writing tests forces you to consider the universe of possible inputs and the corresponding results.
  • Tests are very explicit way of communicating the intent of your code. In other words, test cases act as example and documentation, showing exactly how a given class, method, or function should behave. A test case defines how code works in a non-ambiguous way.

Finally, if your test suite – your set of test cases – is very thorough, you can say your code is complete when all of your test pass. Interestingly, that notion is one of the hallmarks of Test Driven Development.
Test Driven Development (TDD), also referred to as Test First Coding, is a methodology that takes testing one step further: you write your tests before you ever write any code. A nice, brief summary of the tenants of TDD is available at http://xprogramming.com/xpmag/testFirstGuidelines.htm, and a good introductory book on the strategy is “Test Driven Development: By Example” by Kent Beck. (The book’s examples are in Java, but it’s a quick read and gives you a very good overview and introduction to the subject.)

Agile Development
Recently, unit testing – in particular Test Driven Development – has been associated with agile Development methodologies such as Extreme Programming (XP)g that focus on rapid iterations of releasing functional code to customers and welcoming changing customer requirements as a natural part of the development process. Some good online resources for learning about agile development include:

Tags: , , , , , , ,

Posted on Tuesday, December 12th, 2006
Under: Code, HowTo, PHP | 8 Comments »

Advanced Caching Technique – Block Randomization


Introduction

I’m currenlty working on a site where I want to improve performance of dynamic pages. One of the greatest techniques to do is to cache dynamic content and serve the generated output (HTML).

It’s not as easy as we all want it to be when you have all sorts of weird blocks on the page: User login area, random content, ..etc

As I had a very pleasent experience with eZ components last week, I decided to take a look at the components, but then i remembered it works on PHP5. This project is on PHP4, I had to look for an alternative and decided to use PEAR::Cache_Lite.

Read the rest of this entry »

Posted on Monday, August 21st, 2006
Under: Code, HowTo, PEAR, PHP | 6 Comments »

eZ components “Mail” to save the day


A few days ago I was asked to develop a simple script for a solidarity campaign. The idea is that people send their photos as attachements to some email. The script would download all images attached and insert a record for that in the database.

I got introduced to eZ components during my last visit to Norway to attend the eZ systems conference. I decided to give it a shot, and oh boy it’s just amazing, probably the cleanest and simplest API ever.

I managed to find the package “Mail”:

The components allows you construct Mail messages conforming to the RFCs. It has support for attachments, multipart messages and HTML mail. It also interfaces with SMTP to send the e-mail. Reading and parsing mail messages comes in version 1.1.

The package is well documented and includes many examples. Here’s the script I ended up writing (edited version):
Read the rest of this entry »

Posted on Monday, August 14th, 2006
Under: Code, Frameworks, PHP | 4 Comments »

Danel Software Software Board Weddle Software Software PC Original Software MMM Software WS Software