Golang is killing PHP

In the last few years on the market, in my purely personal opinion, golang has been pushing PHP out of the market, and many companies believe that projects that are currently written and running in PHP should be rewritten in golang to be good.

This approach is only partially true. In reality, each programming language is designed to solve a specific type of problem, and performance issues largely depend on the developer, not the programming language.

Seeing this unfair treatment of PHP, I want to publish a series of articles in which I talk about the specifics of development, issues that need to be taken into account, as well as development participants who are also often forgotten, such as devops engineers.

Before I started writing the article, I tested one theory that golang is faster and more productive than PHP. For this, I took a simple function – converting a string to json.

Program in golang

func main() {
	// var data []byte
	// var err error
	data, err := os.ReadFile("test.json")
	if err != nil {
		log.Fatalln("Error reading file ", err)
		return
	}
	start := makeTimestamp()

	wg := &sync.WaitGroup{}
	for i := 0; i < 1000000000; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			var test *Test
			err = json.Unmarshal(data, &test)

			if err != nil {
				fmt.Println("Error reading file ", err)
				return
			}
		}()
	}

	wg.Wait()
	end := makeTimestamp()

	fmt.Printf("%.3fms \n", (end-start)/1e9)
}

PHP program

<?php

$start = microtime(true);

\Swoole\Coroutine\run(function () {
    $cnt = 1_000_000_000;

    $wg = new \Swoole\Coroutine\WaitGroup();

    $fp = fopen(__DIR__ . '/test.json', 'rb');
    if ($fp === false) {
        echo 'Error' . PHP_EOL;
        return;
    }

    defer(function() use($fp) {
        fclose($fp);
    });

    $result="";
    while ($line = fread($fp, 1024)) {
        $result .= $line;
    }

    for ($i = 0; $i < $cnt; $i++) {
        $wg->add();
        \Swoole\Coroutine::create(function () use ($wg, $i, $result) {
            $decoded = json_decode($result);
            unset($decoded);

            $wg->done();
        });
    }
    $wg->wait();
});

$end = microtime(true);

file_put_contents('php://stdout', sprintf("%.3fms \n", ($end - $start)));

As a result of executing these commands I received:

Golang compiled for the local machine executed the command – 452.430ms

PHP in docker image executed command – 390.859ms

I agree that this example does not prove that PHP is faster than Golang, but at the same time, it dispels the myth that golang is definitely better than PHP.

I also took measurements when running the http server and PHP showed itself to be on the good side, in a docker image, with a connection to the database, inserting a line into the database and then selecting data and the database and transferring a json response, the http server to PHP withstood 10,000 rps, with an average response time of 40 ms, a maximum of 500 ms and a minimum of 200 microseconds, but I will write about this in more detail in the following articles.

What is the ultimate goal of the future series of articles:

  • Show that PHP is a good programming language that can be used to make web projects

  • Share app development practices, what to pay attention to and what tools to choose

  • Create a test application where you can look at one of the options for organizing work in a team, so that each team member (and for me this is: backend developer, frontend developer, devops engineer, qa engineer, business/systems analyst and product owner/manager, architect)

  • Show an example of unit economics, division of product features, calculation of development costs and project maintenance in different programming languages. (I will rely on PHP and Golang)

The next article – what PHP server options exist, what a developer needs to know about them.

I plan to publish articles on Sundays. It would be great to get feedback from you, dear readers of Habr, whether you are interested in this direction, in the comments.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *