0

Remove use of wide characters in stats table identifiers.

Review URL: http://codereview.chromium.org/11544


git-svn-id: svn://svn.chromium.org/chrome/trunk/src@5847 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
evanm@google.com
2008-11-21 20:47:00 +00:00
parent 7632898811
commit d232c09f25
24 changed files with 332 additions and 327 deletions

@ -25,7 +25,7 @@ const int Histogram::kHexRangePrintingFlag = 0x8000;
Histogram::Histogram(const wchar_t* name, Sample minimum,
Sample maximum, size_t bucket_count)
: StatsRate(name),
: StatsRate(WideToASCII(name).c_str()),
histogram_name_(WideToASCII(name)),
declared_min_(minimum),
declared_max_(maximum),
@ -39,7 +39,7 @@ Histogram::Histogram(const wchar_t* name, Sample minimum,
Histogram::Histogram(const wchar_t* name, TimeDelta minimum,
TimeDelta maximum, size_t bucket_count)
: StatsRate(name),
: StatsRate(WideToASCII(name).c_str()),
histogram_name_(WideToASCII(name)),
declared_min_(static_cast<int> (minimum.InMilliseconds())),
declared_max_(static_cast<int> (maximum.InMilliseconds())),

@ -74,10 +74,10 @@
class StatsCounter {
public:
// Create a StatsCounter object.
explicit StatsCounter(const std::wstring& name)
explicit StatsCounter(const std::string& name)
: counter_id_(-1) {
// We prepend the name with 'c:' to indicate that it is a counter.
name_ = L"c:";
name_ = "c:";
name_.append(name);
};
@ -146,7 +146,7 @@ class StatsCounter {
if (counter_id_ == -1) {
counter_id_ = table->FindCounter(name_);
if (table->GetSlot() == 0) {
if (!table->RegisterThread(L"")) {
if (!table->RegisterThread("")) {
// There is no room for this thread. This thread
// cannot use counters.
counter_id_ = 0;
@ -163,7 +163,7 @@ class StatsCounter {
return NULL;
}
std::wstring name_;
std::string name_;
// The counter id in the table. We initialize to -1 (an invalid value)
// and then cache it once it has been looked up. The counter_id is
// valid across all threads and processes.
@ -177,9 +177,9 @@ class StatsCounter {
class StatsCounterTimer : protected StatsCounter {
public:
// Constructs and starts the timer.
explicit StatsCounterTimer(const std::wstring& name) {
explicit StatsCounterTimer(const std::string& name) {
// we prepend the name with 't:' to indicate that it is a timer.
name_ = L"t:";
name_ = "t:";
name_.append(name);
}
@ -230,10 +230,10 @@ class StatsCounterTimer : protected StatsCounter {
class StatsRate : public StatsCounterTimer {
public:
// Constructs and starts the timer.
explicit StatsRate(const wchar_t* name)
explicit StatsRate(const char* name)
: StatsCounterTimer(name),
counter_(name),
largest_add_(std::wstring(L" ").append(name).append(L"MAX").c_str()) {
largest_add_(std::string(" ").append(name).append("MAX").c_str()) {
}
virtual void Add(int value) {

@ -70,7 +70,7 @@ namespace {
const int kTableVersion = 0x13131313;
// The name for un-named counters and threads in the table.
const wchar_t kUnknownName[] = L"<unknown>";
const char kUnknownName[] = "<unknown>";
// Calculates delta to align an offset to the size of an int
inline int AlignOffset(int offset) {
@ -109,7 +109,7 @@ class StatsTablePrivate {
// Construct a new StatsTablePrivate based on expected size parameters, or
// return NULL on failure.
static StatsTablePrivate* New(const std::wstring& name, int size,
static StatsTablePrivate* New(const std::string& name, int size,
int max_threads, int max_counters);
base::SharedMemory* shared_memory() { return &shared_memory_; }
@ -122,7 +122,7 @@ class StatsTablePrivate {
int max_threads() const { return table_header_->max_threads; }
// Accessors for our tables
wchar_t* thread_name(int slot_id) const {
char* thread_name(int slot_id) const {
return &thread_names_table_[
(slot_id-1) * (StatsTable::kMaxThreadNameLength)];
}
@ -132,7 +132,7 @@ class StatsTablePrivate {
int* thread_pid(int slot_id) const {
return &(thread_pid_table_[slot_id-1]);
}
wchar_t* counter_name(int counter_id) const {
char* counter_name(int counter_id) const {
return &counter_names_table_[
(counter_id-1) * (StatsTable::kMaxCounterNameLength)];
}
@ -154,21 +154,21 @@ class StatsTablePrivate {
base::SharedMemory shared_memory_;
TableHeader* table_header_;
wchar_t* thread_names_table_;
char* thread_names_table_;
int* thread_tid_table_;
int* thread_pid_table_;
wchar_t* counter_names_table_;
char* counter_names_table_;
int* data_table_;
};
// static
StatsTablePrivate* StatsTablePrivate::New(const std::wstring& name,
StatsTablePrivate* StatsTablePrivate::New(const std::string& name,
int size,
int max_threads,
int max_counters) {
scoped_ptr<StatsTablePrivate> priv(new StatsTablePrivate());
if (!priv->shared_memory_.Create(name, false, true, size))
if (!priv->shared_memory_.Create(UTF8ToWide(name), false, true, size))
return NULL;
if (!priv->shared_memory_.Map(size))
return NULL;
@ -212,8 +212,8 @@ void StatsTablePrivate::ComputeMappedPointers(void* memory) {
// Verify we're looking at a valid StatsTable.
DCHECK_EQ(table_header_->version, kTableVersion);
thread_names_table_ = reinterpret_cast<wchar_t*>(data + offset);
offset += sizeof(wchar_t) *
thread_names_table_ = reinterpret_cast<char*>(data + offset);
offset += sizeof(char) *
max_threads() * StatsTable::kMaxThreadNameLength;
offset += AlignOffset(offset);
@ -225,8 +225,8 @@ void StatsTablePrivate::ComputeMappedPointers(void* memory) {
offset += sizeof(int) * max_threads();
offset += AlignOffset(offset);
counter_names_table_ = reinterpret_cast<wchar_t*>(data + offset);
offset += sizeof(wchar_t) *
counter_names_table_ = reinterpret_cast<char*>(data + offset);
offset += sizeof(char) *
max_counters() * StatsTable::kMaxCounterNameLength;
offset += AlignOffset(offset);
@ -241,14 +241,14 @@ void StatsTablePrivate::ComputeMappedPointers(void* memory) {
// We keep a singleton table which can be easily accessed.
StatsTable* StatsTable::global_table_ = NULL;
StatsTable::StatsTable(const std::wstring& name, int max_threads,
StatsTable::StatsTable(const std::string& name, int max_threads,
int max_counters)
: impl_(NULL),
tls_index_(SlotReturnFunction) {
int table_size =
AlignedSize(sizeof(StatsTablePrivate::TableHeader)) +
AlignedSize((max_counters * sizeof(wchar_t) * kMaxCounterNameLength)) +
AlignedSize((max_threads * sizeof(wchar_t) * kMaxThreadNameLength)) +
AlignedSize((max_counters * sizeof(char) * kMaxCounterNameLength)) +
AlignedSize((max_threads * sizeof(char) * kMaxThreadNameLength)) +
AlignedSize(max_threads * sizeof(int)) +
AlignedSize(max_threads * sizeof(int)) +
AlignedSize((sizeof(int) * (max_counters * max_threads)));
@ -282,7 +282,7 @@ StatsTable::~StatsTable() {
global_table_ = NULL;
}
int StatsTable::RegisterThread(const std::wstring& name) {
int StatsTable::RegisterThread(const std::string& name) {
int slot = 0;
// Registering a thread requires that we lock the shared memory
@ -298,10 +298,10 @@ int StatsTable::RegisterThread(const std::wstring& name) {
DCHECK(impl_);
// We have space, so consume a column in the table.
std::wstring thread_name = name;
std::string thread_name = name;
if (name.empty())
thread_name = kUnknownName;
base::wcslcpy(impl_->thread_name(slot), thread_name.c_str(),
base::strlcpy(impl_->thread_name(slot), thread_name.c_str(),
kMaxThreadNameLength);
*(impl_->thread_tid(slot)) = PlatformThread::CurrentId();
*(impl_->thread_pid(slot)) = base::GetCurrentProcId();
@ -336,8 +336,8 @@ void StatsTable::UnregisterThread(StatsTableTLSData* data) {
DCHECK(impl_);
// Mark the slot free by zeroing out the thread name.
wchar_t* name = impl_->thread_name(data->slot);
*name = L'\0';
char* name = impl_->thread_name(data->slot);
*name = '\0';
// Remove the calling thread's TLS so that it cannot use the slot.
tls_index_.Set(NULL);
@ -363,8 +363,8 @@ int StatsTable::CountThreadsRegistered() const {
// We intentionally do not lock the table during the operation.
int count = 0;
for (int index = 1; index <= impl_->max_threads(); index++) {
wchar_t* name = impl_->thread_name(index);
if (*name != L'\0')
char* name = impl_->thread_name(index);
if (*name != '\0')
count++;
}
return count;
@ -391,7 +391,7 @@ int StatsTable::FindEmptyThread() const {
int index = 1;
for (; index <= impl_->max_threads(); index++) {
wchar_t* name = impl_->thread_name(index);
char* name = impl_->thread_name(index);
if (!*name)
break;
}
@ -400,7 +400,7 @@ int StatsTable::FindEmptyThread() const {
return index;
}
int StatsTable::FindCounterOrEmptyRow(const std::wstring& name) const {
int StatsTable::FindCounterOrEmptyRow(const std::string& name) const {
// Note: the API returns slots numbered from 1..N, although
// internally, the array is 0..N-1. This is so that we can return
// zero as "not found".
@ -413,16 +413,16 @@ int StatsTable::FindCounterOrEmptyRow(const std::wstring& name) const {
int free_slot = 0;
for (int index = 1; index <= impl_->max_counters(); index++) {
wchar_t* row_name = impl_->counter_name(index);
char* row_name = impl_->counter_name(index);
if (!*row_name && !free_slot)
free_slot = index; // save that we found a free slot
else if (!wcsncmp(row_name, name.c_str(), kMaxCounterNameLength))
else if (!strncmp(row_name, name.c_str(), kMaxCounterNameLength))
return index;
}
return free_slot;
}
int StatsTable::FindCounter(const std::wstring& name) {
int StatsTable::FindCounter(const std::string& name) {
// Note: the API returns counters numbered from 1..N, although
// internally, the array is 0..N-1. This is so that we can return
// zero as "not found".
@ -444,7 +444,7 @@ int StatsTable::FindCounter(const std::wstring& name) {
return AddCounter(name);
}
int StatsTable::AddCounter(const std::wstring& name) {
int StatsTable::AddCounter(const std::string& name) {
DCHECK(impl_);
if (!impl_)
@ -461,10 +461,10 @@ int StatsTable::AddCounter(const std::wstring& name) {
if (!counter_id)
return 0;
std::wstring counter_name = name;
std::string counter_name = name;
if (name.empty())
counter_name = kUnknownName;
base::wcslcpy(impl_->counter_name(counter_id), counter_name.c_str(),
base::strlcpy(impl_->counter_name(counter_id), counter_name.c_str(),
kMaxCounterNameLength);
}
@ -486,7 +486,7 @@ int* StatsTable::GetLocation(int counter_id, int slot_id) const {
return &(row[slot_id-1]);
}
const wchar_t* StatsTable::GetRowName(int index) const {
const char* StatsTable::GetRowName(int index) const {
if (!impl_)
return NULL;
@ -510,7 +510,7 @@ int StatsTable::GetRowValue(int index) const {
return GetRowValue(index, 0);
}
int StatsTable::GetCounterValue(const std::wstring& name, int pid) {
int StatsTable::GetCounterValue(const std::string& name, int pid) {
if (!impl_)
return 0;
@ -520,7 +520,7 @@ int StatsTable::GetCounterValue(const std::wstring& name, int pid) {
return GetRowValue(row, pid);
}
int StatsTable::GetCounterValue(const std::wstring& name) {
int StatsTable::GetCounterValue(const std::string& name) {
return GetCounterValue(name, 0);
}
@ -536,7 +536,7 @@ int StatsTable::GetMaxThreads() const {
return impl_->max_threads();
}
int* StatsTable::FindLocation(const wchar_t* name) {
int* StatsTable::FindLocation(const char* name) {
// Get the static StatsTable
StatsTable *table = StatsTable::current();
if (!table)
@ -545,11 +545,11 @@ int* StatsTable::FindLocation(const wchar_t* name) {
// Get the slot for this thread. Try to register
// it if none exists.
int slot = table->GetSlot();
if (!slot && !(slot = table->RegisterThread(L"")))
if (!slot && !(slot = table->RegisterThread("")))
return NULL;
// Find the counter id for the counter.
std::wstring str_name(name);
std::string str_name(name);
int counter = table->FindCounter(str_name);
// Now we can find the location in the table.

@ -46,7 +46,7 @@ class StatsTable {
//
// max_counters is the maximum number of counters the table will support.
// If the StatsTable already exists, this number is ignored.
StatsTable(const std::wstring& name, int max_threads, int max_counters);
StatsTable(const std::string& name, int max_threads, int max_counters);
// Destroys the StatsTable. When the last StatsTable is destroyed
// (across all processes), the StatsTable is removed from disk.
@ -74,7 +74,7 @@ class StatsTable {
//
// On success, returns the slot id for this thread. On failure,
// returns 0.
int RegisterThread(const std::wstring& name);
int RegisterThread(const std::string& name);
// Returns the number of threads currently registered. This is really not
// useful except for diagnostics and debugging.
@ -86,7 +86,7 @@ class StatsTable {
// If the counter does not exist, attempts to create a row for the new
// counter. If there is no space in the table for the new counter,
// returns 0.
int FindCounter(const std::wstring& name);
int FindCounter(const std::string& name);
// TODO(mbelshe): implement RemoveCounter.
@ -96,7 +96,7 @@ class StatsTable {
// Gets the counter name at a particular row. If the row is empty,
// returns NULL.
const wchar_t* GetRowName(int index) const;
const char* GetRowName(int index) const;
// Gets the sum of the values for a particular row.
int GetRowValue(int index) const;
@ -106,11 +106,11 @@ class StatsTable {
// Gets the sum of the values for a particular counter. If the counter
// does not exist, creates the counter.
int GetCounterValue(const std::wstring& name);
int GetCounterValue(const std::string& name);
// Gets the sum of the values for a particular counter for a given pid.
// If the counter does not exist, creates the counter.
int GetCounterValue(const std::wstring& name, int pid);
int GetCounterValue(const std::string& name, int pid);
// The maxinum number of counters/rows in the table.
int GetMaxCounters() const;
@ -129,7 +129,7 @@ class StatsTable {
// Convenience function to lookup a counter location for a
// counter by name for the calling thread. Will register
// the thread if it is not already registered.
static int* FindLocation(const wchar_t *name);
static int* FindLocation(const char *name);
private:
// Returns the space occupied by a thread in the table. Generally used
@ -154,7 +154,7 @@ class StatsTable {
// Locates a counter in the table or finds an empty row. Returns a
// number > 0 on success, or 0 on failure. The caller must hold the
// shared_memory_lock when calling this function.
int FindCounterOrEmptyRow(const std::wstring& name) const;
int FindCounterOrEmptyRow(const std::string& name) const;
// Internal function to add a counter to the StatsTable. Assumes that
// the counter does not already exist in the table.
@ -164,13 +164,13 @@ class StatsTable {
//
// On success, returns the counter_id for the newly added counter.
// On failure, returns 0.
int AddCounter(const std::wstring& name);
int AddCounter(const std::string& name);
// Get the TLS data for the calling thread. Returns NULL if none is
// initialized.
StatsTableTLSData* GetTLSData() const;
typedef base::hash_map<std::wstring, int> CountersMap;
typedef base::hash_map<std::string, int> CountersMap;
bool opened_;
StatsTablePrivate* impl_;

@ -24,27 +24,27 @@ class StatsTableTest : public MultiProcessTest {
// Open a StatsTable and verify that we can write to each of the
// locations in the table.
TEST_F(StatsTableTest, VerifySlots) {
const std::wstring kTableName = L"VerifySlotsStatTable";
const std::string kTableName = "VerifySlotsStatTable";
const int kMaxThreads = 1;
const int kMaxCounter = 5;
StatsTable table(kTableName, kMaxThreads, kMaxCounter);
// Register a single thread.
std::wstring thread_name = L"mainThread";
std::string thread_name = "mainThread";
int slot_id = table.RegisterThread(thread_name);
EXPECT_TRUE(slot_id);
// Fill up the table with counters.
std::wstring counter_base_name = L"counter";
std::string counter_base_name = "counter";
for (int index=0; index < kMaxCounter; index++) {
std::wstring counter_name = counter_base_name;
StringAppendF(&counter_name, L"counter.ctr%d", index);
std::string counter_name = counter_base_name;
StringAppendF(&counter_name, "counter.ctr%d", index);
int counter_id = table.FindCounter(counter_name);
EXPECT_GT(counter_id, 0);
}
// Try to allocate an additional thread. Verify it fails.
slot_id = table.RegisterThread(L"too many threads");
slot_id = table.RegisterThread("too many threads");
EXPECT_EQ(slot_id, 0);
// Try to allocate an additional counter. Verify it fails.
@ -53,16 +53,16 @@ TEST_F(StatsTableTest, VerifySlots) {
}
// CounterZero will continually be set to 0.
const std::wstring kCounterZero = L"CounterZero";
const std::string kCounterZero = "CounterZero";
// Counter1313 will continually be set to 1313.
const std::wstring kCounter1313 = L"Counter1313";
const std::string kCounter1313 = "Counter1313";
// CounterIncrement will be incremented each time.
const std::wstring kCounterIncrement = L"CounterIncrement";
const std::string kCounterIncrement = "CounterIncrement";
// CounterDecrement will be decremented each time.
const std::wstring kCounterDecrement = L"CounterDecrement";
const std::string kCounterDecrement = "CounterDecrement";
// CounterMixed will be incremented by odd numbered threads and
// decremented by even threads.
const std::wstring kCounterMixed = L"CounterMixed";
const std::string kCounterMixed = "CounterMixed";
// The number of thread loops that we will do.
const int kThreadLoops = 1000;
@ -101,7 +101,7 @@ void StatsTableThread::Run() {
// Create a few threads and have them poke on their counters.
TEST_F(StatsTableTest, MultipleThreads) {
// Create a stats table.
const std::wstring kTableName = L"MultipleThreadStatTable";
const std::string kTableName = "MultipleThreadStatTable";
const int kMaxThreads = 20;
const int kMaxCounter = 5;
StatsTable table(kTableName, kMaxThreads, kMaxCounter);
@ -133,25 +133,25 @@ TEST_F(StatsTableTest, MultipleThreads) {
StatsCounter mixed_counter(kCounterMixed);
// Verify the various counters are correct.
std::wstring name;
name = L"c:" + kCounterZero;
std::string name;
name = "c:" + kCounterZero;
EXPECT_EQ(0, table.GetCounterValue(name));
name = L"c:" + kCounter1313;
name = "c:" + kCounter1313;
EXPECT_EQ(1313 * kMaxThreads,
table.GetCounterValue(name));
name = L"c:" + kCounterIncrement;
name = "c:" + kCounterIncrement;
EXPECT_EQ(kMaxThreads * kThreadLoops,
table.GetCounterValue(name));
name = L"c:" + kCounterDecrement;
name = "c:" + kCounterDecrement;
EXPECT_EQ(-kMaxThreads * kThreadLoops,
table.GetCounterValue(name));
name = L"c:" + kCounterMixed;
name = "c:" + kCounterMixed;
EXPECT_EQ((kMaxThreads % 2) * kThreadLoops,
table.GetCounterValue(name));
EXPECT_EQ(0, table.CountThreadsRegistered());
}
const std::wstring kTableName = L"MultipleProcessStatTable";
const std::string kTableName = "MultipleProcessStatTable";
MULTIPROCESS_TEST_MAIN(StatsTableMultipleProcessMain) {
// Each process will open the shared memory and set counters
@ -177,7 +177,7 @@ MULTIPROCESS_TEST_MAIN(StatsTableMultipleProcessMain) {
// Create a few processes and have them poke on their counters.
TEST_F(StatsTableTest, MultipleProcesses) {
// Create a stats table.
const std::wstring kTableName = L"MultipleProcessStatTable";
const std::string kTableName = "MultipleProcessStatTable";
const int kMaxProcs = 20;
const int kMaxCounter = 5;
StatsTable table(kTableName, kMaxProcs, kMaxCounter);
@ -207,16 +207,16 @@ TEST_F(StatsTableTest, MultipleProcesses) {
StatsCounter decrement_counter(kCounterDecrement);
// Verify the various counters are correct.
std::wstring name;
name = L"c:" + kCounterZero;
std::string name;
name = "c:" + kCounterZero;
EXPECT_EQ(0, table.GetCounterValue(name));
name = L"c:" + kCounter1313;
name = "c:" + kCounter1313;
EXPECT_EQ(1313 * kMaxProcs,
table.GetCounterValue(name));
name = L"c:" + kCounterIncrement;
name = "c:" + kCounterIncrement;
EXPECT_EQ(kMaxProcs * kThreadLoops,
table.GetCounterValue(name));
name = L"c:" + kCounterDecrement;
name = "c:" + kCounterDecrement;
EXPECT_EQ(-kMaxProcs * kThreadLoops,
table.GetCounterValue(name));
EXPECT_EQ(0, table.CountThreadsRegistered());
@ -224,7 +224,7 @@ TEST_F(StatsTableTest, MultipleProcesses) {
class MockStatsCounter : public StatsCounter {
public:
MockStatsCounter(const std::wstring& name)
MockStatsCounter(const std::string& name)
: StatsCounter(name) {}
int* Pointer() { return GetPtr(); }
};
@ -232,50 +232,50 @@ class MockStatsCounter : public StatsCounter {
// Test some basic StatsCounter operations
TEST_F(StatsTableTest, StatsCounter) {
// Create a stats table.
const std::wstring kTableName = L"StatTable";
const std::string kTableName = "StatTable";
const int kMaxThreads = 20;
const int kMaxCounter = 5;
StatsTable table(kTableName, kMaxThreads, kMaxCounter);
StatsTable::set_current(&table);
MockStatsCounter foo(L"foo");
MockStatsCounter foo("foo");
// Test initial state.
EXPECT_TRUE(foo.Enabled());
EXPECT_NE(foo.Pointer(), static_cast<int*>(0));
EXPECT_EQ(0, table.GetCounterValue(L"c:foo"));
EXPECT_EQ(0, table.GetCounterValue("c:foo"));
EXPECT_EQ(0, *(foo.Pointer()));
// Test Increment.
while(*(foo.Pointer()) < 123) foo.Increment();
EXPECT_EQ(123, table.GetCounterValue(L"c:foo"));
EXPECT_EQ(123, table.GetCounterValue("c:foo"));
foo.Add(0);
EXPECT_EQ(123, table.GetCounterValue(L"c:foo"));
EXPECT_EQ(123, table.GetCounterValue("c:foo"));
foo.Add(-1);
EXPECT_EQ(122, table.GetCounterValue(L"c:foo"));
EXPECT_EQ(122, table.GetCounterValue("c:foo"));
// Test Set.
foo.Set(0);
EXPECT_EQ(0, table.GetCounterValue(L"c:foo"));
EXPECT_EQ(0, table.GetCounterValue("c:foo"));
foo.Set(100);
EXPECT_EQ(100, table.GetCounterValue(L"c:foo"));
EXPECT_EQ(100, table.GetCounterValue("c:foo"));
foo.Set(-1);
EXPECT_EQ(-1, table.GetCounterValue(L"c:foo"));
EXPECT_EQ(-1, table.GetCounterValue("c:foo"));
foo.Set(0);
EXPECT_EQ(0, table.GetCounterValue(L"c:foo"));
EXPECT_EQ(0, table.GetCounterValue("c:foo"));
// Test Decrement.
foo.Decrement(1);
EXPECT_EQ(-1, table.GetCounterValue(L"c:foo"));
EXPECT_EQ(-1, table.GetCounterValue("c:foo"));
foo.Decrement(0);
EXPECT_EQ(-1, table.GetCounterValue(L"c:foo"));
EXPECT_EQ(-1, table.GetCounterValue("c:foo"));
foo.Decrement(-1);
EXPECT_EQ(0, table.GetCounterValue(L"c:foo"));
EXPECT_EQ(0, table.GetCounterValue("c:foo"));
}
class MockStatsCounterTimer : public StatsCounterTimer {
public:
MockStatsCounterTimer(const std::wstring& name)
MockStatsCounterTimer(const std::string& name)
: StatsCounterTimer(name) {}
TimeTicks start_time() { return start_time_; }
@ -285,13 +285,13 @@ class MockStatsCounterTimer : public StatsCounterTimer {
// Test some basic StatsCounterTimer operations
TEST_F(StatsTableTest, StatsCounterTimer) {
// Create a stats table.
const std::wstring kTableName = L"StatTable";
const std::string kTableName = "StatTable";
const int kMaxThreads = 20;
const int kMaxCounter = 5;
StatsTable table(kTableName, kMaxThreads, kMaxCounter);
StatsTable::set_current(&table);
MockStatsCounterTimer bar(L"bar");
MockStatsCounterTimer bar("bar");
// Test initial state.
EXPECT_FALSE(bar.Running());
@ -302,62 +302,62 @@ TEST_F(StatsTableTest, StatsCounterTimer) {
bar.Start();
PlatformThread::Sleep(500);
bar.Stop();
EXPECT_LE(500, table.GetCounterValue(L"t:bar"));
EXPECT_LE(500, table.GetCounterValue("t:bar"));
// Verify that timing again is additive.
bar.Start();
PlatformThread::Sleep(500);
bar.Stop();
EXPECT_LE(1000, table.GetCounterValue(L"t:bar"));
EXPECT_LE(1000, table.GetCounterValue("t:bar"));
}
// Test some basic StatsRate operations
TEST_F(StatsTableTest, StatsRate) {
// Create a stats table.
const std::wstring kTableName = L"StatTable";
const std::string kTableName = "StatTable";
const int kMaxThreads = 20;
const int kMaxCounter = 5;
StatsTable table(kTableName, kMaxThreads, kMaxCounter);
StatsTable::set_current(&table);
StatsRate baz(L"baz");
StatsRate baz("baz");
// Test initial state.
EXPECT_FALSE(baz.Running());
EXPECT_EQ(0, table.GetCounterValue(L"c:baz"));
EXPECT_EQ(0, table.GetCounterValue(L"t:baz"));
EXPECT_EQ(0, table.GetCounterValue("c:baz"));
EXPECT_EQ(0, table.GetCounterValue("t:baz"));
// Do some timing.
baz.Start();
PlatformThread::Sleep(500);
baz.Stop();
EXPECT_EQ(1, table.GetCounterValue(L"c:baz"));
EXPECT_LE(500, table.GetCounterValue(L"t:baz"));
EXPECT_EQ(1, table.GetCounterValue("c:baz"));
EXPECT_LE(500, table.GetCounterValue("t:baz"));
// Verify that timing again is additive.
baz.Start();
PlatformThread::Sleep(500);
baz.Stop();
EXPECT_EQ(2, table.GetCounterValue(L"c:baz"));
EXPECT_LE(1000, table.GetCounterValue(L"t:baz"));
EXPECT_EQ(2, table.GetCounterValue("c:baz"));
EXPECT_LE(1000, table.GetCounterValue("t:baz"));
}
// Test some basic StatsScope operations
TEST_F(StatsTableTest, StatsScope) {
// Create a stats table.
const std::wstring kTableName = L"StatTable";
const std::string kTableName = "StatTable";
const int kMaxThreads = 20;
const int kMaxCounter = 5;
StatsTable table(kTableName, kMaxThreads, kMaxCounter);
StatsTable::set_current(&table);
StatsCounterTimer foo(L"foo");
StatsRate bar(L"bar");
StatsCounterTimer foo("foo");
StatsRate bar("bar");
// Test initial state.
EXPECT_EQ(0, table.GetCounterValue(L"t:foo"));
EXPECT_EQ(0, table.GetCounterValue(L"t:bar"));
EXPECT_EQ(0, table.GetCounterValue(L"c:bar"));
EXPECT_EQ(0, table.GetCounterValue("t:foo"));
EXPECT_EQ(0, table.GetCounterValue("t:bar"));
EXPECT_EQ(0, table.GetCounterValue("c:bar"));
// Try a scope.
{
@ -365,9 +365,9 @@ TEST_F(StatsTableTest, StatsScope) {
StatsScope<StatsRate> timer2(bar);
PlatformThread::Sleep(500);
}
EXPECT_LE(500, table.GetCounterValue(L"t:foo"));
EXPECT_LE(500, table.GetCounterValue(L"t:bar"));
EXPECT_EQ(1, table.GetCounterValue(L"c:bar"));
EXPECT_LE(500, table.GetCounterValue("t:foo"));
EXPECT_LE(500, table.GetCounterValue("t:bar"));
EXPECT_EQ(1, table.GetCounterValue("c:bar"));
// Try a second scope.
{
@ -375,9 +375,9 @@ TEST_F(StatsTableTest, StatsScope) {
StatsScope<StatsRate> timer2(bar);
PlatformThread::Sleep(500);
}
EXPECT_LE(1000, table.GetCounterValue(L"t:foo"));
EXPECT_LE(1000, table.GetCounterValue(L"t:bar"));
EXPECT_EQ(2, table.GetCounterValue(L"c:bar"));
EXPECT_LE(1000, table.GetCounterValue("t:foo"));
EXPECT_LE(1000, table.GetCounterValue("t:bar"));
EXPECT_EQ(2, table.GetCounterValue("c:bar"));
}
} // namespace base