Cache: Allow number of items + sizes to be specified in tests

This commit is contained in:
Johan Wikman
2016-12-16 13:46:57 +02:00
parent f929932fc0
commit 23556cdfa2
5 changed files with 113 additions and 7 deletions

View File

@ -49,3 +49,11 @@ struct hash<CACHE_KEY>
std::string cache_key_to_string(const CACHE_KEY& key); std::string cache_key_to_string(const CACHE_KEY& key);
class CacheKey : public CACHE_KEY
{
public:
CacheKey()
{
memset(data, 0, sizeof(data));
}
};

View File

@ -136,12 +136,36 @@ GWBUF* Tester::gwbuf_from_string(const std::string& s)
GWBUF* pBuf = gwbuf_alloc(gwbuf_len); GWBUF* pBuf = gwbuf_alloc(gwbuf_len);
if (pBuf)
{
*((unsigned char*)((char*)GWBUF_DATA(pBuf))) = payload_len; *((unsigned char*)((char*)GWBUF_DATA(pBuf))) = payload_len;
*((unsigned char*)((char*)GWBUF_DATA(pBuf) + 1)) = (payload_len >> 8); *((unsigned char*)((char*)GWBUF_DATA(pBuf) + 1)) = (payload_len >> 8);
*((unsigned char*)((char*)GWBUF_DATA(pBuf) + 2)) = (payload_len >> 16); *((unsigned char*)((char*)GWBUF_DATA(pBuf) + 2)) = (payload_len >> 16);
*((unsigned char*)((char*)GWBUF_DATA(pBuf) + 3)) = 0x00; *((unsigned char*)((char*)GWBUF_DATA(pBuf) + 3)) = 0x00;
*((unsigned char*)((char*)GWBUF_DATA(pBuf) + 4)) = 0x03; *((unsigned char*)((char*)GWBUF_DATA(pBuf) + 4)) = 0x03; // COM_QUERY
memcpy((char*)GWBUF_DATA(pBuf) + 5, s.c_str(), len); memcpy((char*)GWBUF_DATA(pBuf) + 5, s.c_str(), len);
}
return pBuf;
}
// static
GWBUF* Tester::gwbuf_from_vector(const std::vector<uint8_t>& v)
{
size_t len = v.size();
size_t payload_len = len;
size_t gwbuf_len = MYSQL_HEADER_LEN + payload_len;
GWBUF* pBuf = gwbuf_alloc(gwbuf_len);
if (pBuf)
{
*((unsigned char*)((char*)GWBUF_DATA(pBuf))) = payload_len;
*((unsigned char*)((char*)GWBUF_DATA(pBuf) + 1)) = (payload_len >> 8);
*((unsigned char*)((char*)GWBUF_DATA(pBuf) + 2)) = (payload_len >> 16);
*((unsigned char*)((char*)GWBUF_DATA(pBuf) + 3)) = 0x00;
memcpy((char*)GWBUF_DATA(pBuf) + 4, v.data(), len);
}
return pBuf; return pBuf;
} }

View File

@ -133,7 +133,7 @@ public:
virtual ~Tester(); virtual ~Tester();
/** /**
* Convert a string to a COM_QUERY GWBUF. * Converts a string to a COM_QUERY GWBUF.
* *
* @param s The string to be converted. * @param s The string to be converted.
* *
@ -141,6 +141,18 @@ public:
*/ */
static GWBUF* gwbuf_from_string(const std::string& s); static GWBUF* gwbuf_from_string(const std::string& s);
/**
* Converts a vector to a GWBUF.
*
* NOTE: The data is used verbatim and placed directly after the header; no
* interpretation whatsoever.
*
* @param v The vector to be converted.
*
* @return A GWBUF or NULL if memory allocation failed.
*/
static GWBUF* gwbuf_from_vector(const std::vector<uint8_t>& v);
/** /**
* Returns statements from a MySQL/MariaDB server test file. * Returns statements from a MySQL/MariaDB server test file.
* *

View File

@ -159,6 +159,47 @@ int TesterStorage::run(size_t n_threads, size_t n_seconds, std::istream& in)
return rv; return rv;
} }
int TesterStorage::run(size_t n_threads,
size_t n_seconds,
size_t n_items,
size_t n_min_size,
size_t n_max_size)
{
int rv = EXIT_SUCCESS;
CacheItems cache_items;
size_t i = 0;
while ((rv == EXIT_SUCCESS) && (i < n_items))
{
size_t size = n_min_size + ((static_cast<double>(random()) / RAND_MAX) * (n_max_size - n_min_size));
ss_dassert(size >= n_min_size);
ss_dassert(size <= n_max_size);
CacheKey key;
sprintf(key.data, "%lu", i);
vector<uint8_t> value(size, static_cast<uint8_t>(i));
GWBUF* pBuf = gwbuf_from_vector(value);
if (pBuf)
{
cache_items.push_back(std::make_pair(key, pBuf));
}
else
{
rv = EXIT_FAILURE;
}
}
clear_cache_items(cache_items);
return rv;
}
int TesterStorage::execute_tasks(size_t n_threads, int TesterStorage::execute_tasks(size_t n_threads,
size_t n_seconds, size_t n_seconds,
const CacheItems& cache_items, const CacheItems& cache_items,

View File

@ -83,6 +83,27 @@ public:
*/ */
virtual int run(size_t n_threads, size_t n_seconds, std::istream& in); virtual int run(size_t n_threads, size_t n_seconds, std::istream& in);
/**
* Creates cache items with the size varying between the specified minimum
* and maximum sizes.
*
* Will call back into the virtual @c execute function below.
*
* @param n_threads How many threads to use.
* @param n_seconds For how many seconds to run the test.
* @param n_items How many items to create.
* @param n_min_size The minimum size of a cache value.
* @param n_max_size The maximum size of a cache value.
*
* @return EXIT_SUCCESS or EXIT_FAILURE.
*
*/
virtual int run(size_t n_threads,
size_t n_seconds,
size_t n_items,
size_t n_min_size,
size_t n_max_size);
/** /**
* Execute tests; implemented by derived class. * Execute tests; implemented by derived class.
* *