Skip to content

Commit 823864e

Browse files
committed
PHPC-180: Cache the fixtures
Don't be regenerating the fixtures all the time, is super slow. We now store them in /tmp/PHONGO-FIXTURES.json and read it from there, as long as its mtime is newer then the fixtures-users.inc Since we are caching these, bump the generated count to 1024 to force few roundtrips
1 parent 1228956 commit 823864e

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

tests/utils/fixtures-users.inc

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@ if ( ! is_readable(__DIR__ . '/../../vendor/autoload.php')) {
44
exit('skip composer dependencies not available');
55
}
66

7+
$LIMIT = isset($LIMIT) ? (int)$LIMIT : 8192;
8+
79
require_once __DIR__ . '/basic.inc';
810
require_once __DIR__ . '/../../vendor/autoload.php';
911

10-
$faker = Faker\Factory::create();
11-
$faker->seed(1234);
12-
13-
$manager = new MongoDB\Driver\Manager(STANDALONE);
14-
$bulk = new MongoDB\Driver\BulkWrite;
15-
1612
function createUser($faker)
1713
{
1814
return array(
@@ -53,12 +49,51 @@ function createGeoJsonPoint($faker)
5349
);
5450
}
5551

56-
for ($i = 0; $i < 100; $i++) {
57-
$bulk->insert(createUser($faker));
52+
$CACHE = sys_get_temp_dir() . "/PHONGO-FIXTURES.json";
53+
54+
$manager = new MongoDB\Driver\Manager(STANDALONE);
55+
$bulk = new MongoDB\Driver\BulkWrite;
56+
57+
58+
if (file_exists($CACHE) && filemtime($CACHE) < filemtime(__FILE__)) {
59+
$data = file_get_contents($CACHE);
60+
$users = json_decode($data, true);
61+
} else {
62+
$faker = Faker\Factory::create();
63+
$faker->seed(1234);
64+
65+
$users = array();
66+
for ($i = 0; $i < 1024; $i++) {
67+
$users[] = $user = createUser($faker);
68+
}
69+
70+
file_put_contents($CACHE, json_encode($users));
5871
}
5972

73+
foreach($users as $n => $user) {
74+
$bulk->insert($user);
75+
76+
if ($n >= $LIMIT) {
77+
break;
78+
}
79+
}
6080
$retval = $manager->executeBulkWrite(NS, $bulk);
6181

62-
if ($retval->getInsertedCount() !== 100) {
63-
exit(sprintf('skip Fixtures were not loaded (expected: %d, actual: %d)', 100, $retval->getInsertedCount()));
82+
if ($retval->getInsertedCount() !== 1024) {
83+
exit(sprintf('skip Fixtures were not loaded (expected: %d, actual: %d)', 1024, $retval->getInsertedCount()));
6484
}
85+
86+
87+
$cmd = array(
88+
"createIndexes" => COLLECTION_NAME,
89+
"indexes" => array(
90+
array(
91+
"key" => array("_id" => 1, "username" => 1),
92+
"name" => "cursorid_test",
93+
"unique" => true,
94+
),
95+
),
96+
);
97+
$index = new MongoDB\Driver\Command($cmd);
98+
$retval = $manager->executeCommand(DATABASE_NAME, $index);
99+

0 commit comments

Comments
 (0)