Skip to content

Instantly share code, notes, and snippets.

@larryhou
Last active May 13, 2021 10:01
Show Gist options
  • Save larryhou/6eb97904f4dde78258a6dc6848149993 to your computer and use it in GitHub Desktop.
Save larryhou/6eb97904f4dde78258a6dc6848149993 to your computer and use it in GitHub Desktop.
//
// main.cpp
// alignperf
//
// Created by LARRYHOU on 2021/5/13.
//
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <chrono>
#define DISK_PAGE_SIZE (4<<10)
std::vector<uint64_t> benchmark(std::string filename, std::ios::off_type offset, int repeat)
{
std::vector<uint64_t> records;
std::fstream fs;
fs.open(filename, std::ios::in);
fs.seekg(0, std::ios::end);
auto length = fs.tellg();
char block[DISK_PAGE_SIZE];
while (repeat--)
{
fs.clear();
fs.seekg(offset);
auto stime = std::chrono::high_resolution_clock::now();
auto remain = length;
while (remain > 0)
{
auto size = std::min<uint64_t>(DISK_PAGE_SIZE, remain - fs.tellg());
fs.read(block, size);
remain -= size;
}
auto etime = std::chrono::high_resolution_clock::now();
records.push_back(std::chrono::duration_cast<std::chrono::duration<uint64_t, std::nano>>(etime - stime).count());
}
fs.close();
return records;
}
int main(int argc, const char * argv[])
{
for (auto i = 1; i < argc; i++)
{
std::string filename(argv[i]);
std::cout << filename << std::endl;
auto offset = 1;
std::vector<int> cases;
while (offset <= DISK_PAGE_SIZE)
{
cases.push_back(offset);
offset <<= 1;
}
for (auto i = 0; i < 10; i++)
{
cases.push_back(rand() % DISK_PAGE_SIZE + 1);
}
for (auto i = 0; i < cases.size(); i++)
{
offset = cases[i];
auto stats = benchmark(filename, offset, 3);
uint64_t sum = 0;
for (auto iter = stats.begin(); iter != stats.end(); iter++) { sum += *iter; }
auto mean = sum / stats.size();
printf(" %4d %lld=(", offset, mean);
for (auto iter = stats.begin(); iter != stats.end(); iter++) {
std::cout << *iter << ',';
}
std::cout << '\b' << ')' << std::endl;
offset <<= 1;
}
}
return 0;
}
LOCAL_PATH :=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := alignperf
LOCAL_SRC_FILES := alignperf.cpp
include $(BUILD_EXECUTABLE)
APP_ABI := x86 arm64-v8a
APP_PLATFORM := android-21
APP_STL := c++_static
APP_CPPFLAGS := -std=c++11
APP_BUILD_SCRIPT := Android.mk
NDK_PROJECT_PATH=. ~/Library/Android/sdk/ndk/20.0.5594570/ndk-build NDK_APPLICATION_MK=Application.mk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment