Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

add cluster balance indicator #214

Merged
merged 28 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions include/dsn/dist/replication/replication_types.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions include/dsn/utility/math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

#pragma once

#include <vector>
#include <cstdint>

namespace dsn {
namespace utils {

double mean_stddev(const std::vector<uint32_t> &result_set, bool partial_sample);

} // namespace utils
} // namespace dsn
37 changes: 37 additions & 0 deletions src/core/core/math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

#include <numeric>
#include <algorithm>
#include <math.h>
#include <dsn/c/api_utilities.h>
#include <dsn/utility/math.h>

namespace dsn {
namespace utils {

double mean_stddev(const std::vector<uint32_t> &result_set, bool partial_sample)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以加个assert(result_set.size() > 1)

dassert(result_set.size() > 1, "invalid sample data input for stddev");

double sum = std::accumulate(result_set.begin(), result_set.end(), 0.0);
double mean = sum / result_set.size();

double accum = 0.0;
std::for_each(result_set.begin(), result_set.end(), [&](const double d) {
accum += (d - mean) * (d - mean);
});

double stddev;
if (partial_sample)
stddev = sqrt(accum / (result_set.size() - 1));
else
stddev = sqrt(accum / (result_set.size()));

stddev = ((double)((int)((stddev + 0.005) * 100))) / 100;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里只是为了做四舍五入吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

四舍五入保留小数点后两位

return stddev;
}

} // namespace utils
} // namespace dsn
Loading