зеркало из https://github.com/microsoft/L4.git
Update examples.
This commit is contained in:
Родитель
388433785a
Коммит
355eeedcaa
|
@ -1,97 +1,70 @@
|
|||
#include "Log/IPerfLogger.h"
|
||||
#include "LocalMemory/HashTableService.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <tchar.h>
|
||||
#include <stdarg.h>
|
||||
#include <memory>
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include "LocalMemory/HashTableService.h"
|
||||
|
||||
using namespace L4;
|
||||
|
||||
class ConsolePerfLogger : public IPerfLogger
|
||||
int main()
|
||||
{
|
||||
virtual void Log(const IData& perfData) override
|
||||
{
|
||||
for (auto i = 0; i < static_cast<std::uint16_t>(ServerPerfCounter::Count); ++i)
|
||||
{
|
||||
std::cout << c_serverPerfCounterNames[i] << ": "
|
||||
<< perfData.GetServerPerfData().Get(static_cast<ServerPerfCounter>(i)) << std::endl;
|
||||
}
|
||||
|
||||
const auto& hashTablesPerfData = perfData.GetHashTablesPerfData();
|
||||
EpochManagerConfig epochConfig{ 1000, std::chrono::milliseconds(100), 1 };
|
||||
LocalMemory::HashTableService service{ epochConfig };
|
||||
|
||||
for (const auto& entry : hashTablesPerfData)
|
||||
{
|
||||
std::cout << "Hash table '" << entry.first << "'" << std::endl;
|
||||
|
||||
for (auto j = 0; j < static_cast<std::uint16_t>(HashTablePerfCounter::Count); ++j)
|
||||
{
|
||||
std::cout << c_hashTablePerfCounterNames[j] << ": "
|
||||
<< entry.second.get().Get(static_cast<HashTablePerfCounter>(j)) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
LocalMemory::HashTableService service;
|
||||
|
||||
auto index = service.AddHashTable(
|
||||
auto hashTableIndex = service.AddHashTable(
|
||||
HashTableConfig("Table1", HashTableConfig::Setting{ 1000000 }));
|
||||
|
||||
static constexpr int keySize = 100;
|
||||
static constexpr int valSize = 2000;
|
||||
std::vector<std::pair<std::string, std::string>> keyValuePairs =
|
||||
{
|
||||
{ "key1", "value1"},
|
||||
{ "key2", "value2" },
|
||||
{ "key3", "value3" },
|
||||
{ "key4", "value4" },
|
||||
{ "key5", "value5" },
|
||||
};
|
||||
|
||||
char bufKey[keySize];
|
||||
char bufVal[valSize];
|
||||
|
||||
IWritableHashTable::Key key;
|
||||
key.m_data = reinterpret_cast<std::uint8_t*>(bufKey);
|
||||
IWritableHashTable::Value val;
|
||||
val.m_data = reinterpret_cast<std::uint8_t*>(bufVal);
|
||||
|
||||
std::ifstream file;
|
||||
file.open(argv[1], std::ifstream::in);
|
||||
std::cout << "Opening " << argv[1] << std::endl;
|
||||
static const int BufferLength = 4096;
|
||||
|
||||
char buffer[BufferLength];
|
||||
|
||||
auto totalTime = 0U;
|
||||
int numLines = 0;
|
||||
while (file.getline(buffer, BufferLength))
|
||||
// Write data.
|
||||
{
|
||||
auto context = service.GetContext();
|
||||
auto& hashTable = context[hashTableIndex];
|
||||
|
||||
auto& hashTable = context[index];
|
||||
for (const auto& keyValuePair : keyValuePairs)
|
||||
{
|
||||
const auto& keyStr = keyValuePair.first;
|
||||
const auto& valStr = keyValuePair.second;
|
||||
|
||||
char* nextToken = nullptr;
|
||||
const char* keyStr = strtok_s(buffer, "\t", &nextToken);
|
||||
const char* valStr = strtok_s(nullptr, "\t", &nextToken);
|
||||
IWritableHashTable::Key key;
|
||||
key.m_data = reinterpret_cast<const std::uint8_t*>(keyStr.c_str());
|
||||
key.m_size = keyStr.size();
|
||||
|
||||
key.m_data = reinterpret_cast<const std::uint8_t*>(keyStr);
|
||||
key.m_size = static_cast<std::uint16_t>(strlen(keyStr));
|
||||
IWritableHashTable::Value val;
|
||||
val.m_data = reinterpret_cast<const std::uint8_t*>(valStr.c_str());
|
||||
val.m_size = valStr.size();
|
||||
|
||||
val.m_data = reinterpret_cast<const std::uint8_t*>(valStr);
|
||||
val.m_size = static_cast<std::uint32_t>(strlen(valStr));
|
||||
|
||||
hashTable.Add(key, val);
|
||||
|
||||
++numLines;
|
||||
|
||||
hashTable.Add(key, val);
|
||||
}
|
||||
}
|
||||
|
||||
std::cout<< "Total Add() time" << totalTime << std::endl;
|
||||
std::cout << "Added " << numLines << " lines." << std::endl;
|
||||
// Read data.
|
||||
{
|
||||
auto context = service.GetContext();
|
||||
auto& hashTable = context[hashTableIndex];
|
||||
|
||||
for (const auto& keyValuePair : keyValuePairs)
|
||||
{
|
||||
const auto& keyStr = keyValuePair.first;
|
||||
|
||||
IWritableHashTable::Key key;
|
||||
key.m_data = reinterpret_cast<const std::uint8_t*>(keyStr.c_str());
|
||||
key.m_size = keyStr.size();
|
||||
|
||||
IWritableHashTable::Value val;
|
||||
hashTable.Get(key, val);
|
||||
|
||||
std::cout << std::string(reinterpret_cast<const char*>(val.m_data), val.m_size) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче