Want to share your product creation script?
I’ve dramatically shortened my script and posted this version below.
It shows the same behavior as my original script. I’ve ran it with the following four configurations resulting in the specified memory usage increase per product:
- Linux with caching: about 116KB per product
- Linux without caching: about 180KB per product
- Window with caching: about 10KB per product
- Windows without caching: about 2KB per product
So Windows behaves as I would expect. But Linux behaves very strange.
<?php
function generateRandomString($length) {
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function generateRandomPrice($min, $max, $decimals = 2){
$randomfloat = $min + mt_rand() / mt_getrandmax() * ($max - $min);
return number_format($randomfloat, $decimals);
}
function createRandomProductData() {
$result = [ 'product' => [
'title' => generateRandomString(10),
'type' => 'simple',
'status' => 'publish',
'virtual' => false,
'sku' => generateRandomString(4),
'managing_stock' => false,
'downloadable' => false,
'review_allowed' => false,
'short_description' => generateRandomString(20),
'regular_price' => generateRandomPrice(10.0, 99.0)
] ];
return $result;
}
function instantiateWooCommerce() {
$wpLoadPath = 'C:/inetpub/wwwroot/wordpress/wp-load.php';
//$wpLoadPath = '/opt/wordpress-3.9.1-1/apps/wordpress/htdocs/wp-load.php';
require_once $wpLoadPath;
$wooCommercePath = realpath(WP_PLUGIN_DIR . '/woocommerce/woocommerce.php');
require_once $wooCommercePath;
WC()->api->includes();
WC()->api->register_resources(new WC_API_Server( '/' ));
$credentials = [
'user_login' => 'FP_product_uploader',
'user_password' => '12345678'
];
$user = wp_signon($credentials, false);
wp_set_current_user($user->ID);
}
function printMemoryUsage($label) {
$memoryUsage = memory_get_usage();
echo $label . ': ' . $memoryUsage . "\n";
}
instantiateWooCommerce();
wp_suspend_cache_addition(true);
$api = WC()->api->WC_API_Products;
printMemoryUsage('before');
for($i = 0; $i < 10; ++$i) {
printMemoryUsage($i);
$productData = createRandomProductData();
$api->create_product($productData);
}
printMemoryUsage('after');
Meanwhile I’ve tried a different Linux WordPress installation (running PHP 5.6.12). The result is similar to my Windows result. So this is an issue of the specific WordPress installation (whatever that may be).
Still there is an increase in memory usage of 2 to 3KB per product – even after turning off WordPress database caching. This is a memory leak I can live with. But I would prefer to get rid of it, of course. So I appreciate any help on this.
After creation, the new/populated product is returned. This will be retrieved using https://developer.wordpress.org/reference/functions/get_post/ which has its own caches etc. Thats going to be stored in memory to save on future get_post calls for the same product.