2020-09-23 19:06:09 +08:00
|
|
|
/*************************************************************************
|
|
|
|
|
> File Name: single_thread.cpp
|
|
|
|
|
> Author: pxz
|
|
|
|
|
> Created Time: Fri 11 Sep 2020 09:52:05 AM CST
|
|
|
|
|
************************************************************************/
|
|
|
|
|
extern "C"
|
|
|
|
|
{
|
|
|
|
|
#include<stdio.h>
|
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
#include<unistd.h>
|
|
|
|
|
#include<string.h>
|
|
|
|
|
#include<time.h>
|
|
|
|
|
}
|
2020-10-14 15:23:01 +08:00
|
|
|
#include"hos_client.h"
|
2020-09-23 19:06:09 +08:00
|
|
|
|
|
|
|
|
//#define test_times 10000
|
|
|
|
|
|
|
|
|
|
#define debuginfo (void)
|
|
|
|
|
|
|
|
|
|
typedef struct userdata_s
|
|
|
|
|
{
|
|
|
|
|
struct timespec *finished;
|
|
|
|
|
}userdata_t;
|
|
|
|
|
|
|
|
|
|
static size_t calc_time(struct timespec start, struct timespec end)
|
|
|
|
|
{
|
|
|
|
|
return (end.tv_sec * 1000 * 1000 * 1000 + end.tv_nsec -
|
|
|
|
|
(start.tv_sec * 1000 * 1000 * 1000 + start.tv_nsec));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int file_to_buffer(const char *file, char *buffer, size_t *len)
|
|
|
|
|
{
|
|
|
|
|
FILE *fp = fopen(file, "r");
|
|
|
|
|
int num = 0;
|
|
|
|
|
*len = 0;
|
|
|
|
|
if (fp == NULL)
|
|
|
|
|
{
|
|
|
|
|
debuginfo("fopen file failed:%s\n", file);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
do{
|
|
|
|
|
num = fread(&buffer[*len], 1, 4096, fp);
|
|
|
|
|
if (num < 0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
*len += num;
|
|
|
|
|
}while(num == 4096);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 18:09:03 +08:00
|
|
|
void callback(bool result, const char *error, const char *bucket, const char *object, void *userdata)
|
2020-09-23 19:06:09 +08:00
|
|
|
{
|
|
|
|
|
userdata_t *data = (userdata_t *)userdata;
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, data->finished);
|
|
|
|
|
return ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
if (argc != 4)
|
|
|
|
|
{
|
|
|
|
|
debuginfo("usege: singThread [bucket name] [object name]\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
struct timespec start, end, finished;
|
|
|
|
|
size_t time;
|
|
|
|
|
int i = 0;
|
|
|
|
|
char *bucket = argv[1];
|
|
|
|
|
char *object = argv[2];
|
|
|
|
|
int test_times = atoi(argv[3]);
|
2020-10-14 15:23:01 +08:00
|
|
|
char *buf = (char *)malloc(1024 * 1024 * 40);
|
2020-09-23 19:06:09 +08:00
|
|
|
size_t buf_size;
|
|
|
|
|
int mode = FILE_MODE;
|
2020-10-14 15:23:01 +08:00
|
|
|
size_t fd[10001] = {0};
|
2020-09-23 19:06:09 +08:00
|
|
|
userdata_t data = {&finished};
|
|
|
|
|
|
|
|
|
|
file_to_buffer(object, buf, &buf_size);
|
|
|
|
|
|
2020-11-02 17:55:02 +08:00
|
|
|
hos_init_api();
|
2020-09-23 19:06:09 +08:00
|
|
|
debuginfo("hos_client_init start ...\n");
|
2020-11-11 11:15:14 +08:00
|
|
|
hos_client_handle handle = hos_client_create("http://192.168.40.223:9098/hos/", "default", "default", 400);
|
2020-10-14 15:23:01 +08:00
|
|
|
//hos_client_handle handle = hos_client_create("http://192.168.32.10:9098/hos/", "default", "default", 4);
|
2020-09-23 19:06:09 +08:00
|
|
|
if (handle == NULL)
|
|
|
|
|
{
|
2020-10-14 15:23:01 +08:00
|
|
|
printf("error:hos_client_handle\n");
|
2020-09-23 19:06:09 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
debuginfo("hos_client_init success ... \n");
|
|
|
|
|
|
|
|
|
|
debuginfo("hos_create_bucket start ... \n");
|
|
|
|
|
if(hos_create_bucket(handle, bucket))
|
|
|
|
|
{
|
2020-10-14 15:23:01 +08:00
|
|
|
printf("hos_create_bucket failed ... \n");
|
2020-09-23 19:06:09 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
debuginfo("hos_create_bucket success ... \n");
|
|
|
|
|
|
|
|
|
|
debuginfo("hos_verify_bucket start ... \n");
|
|
|
|
|
if(!hos_verify_bucket(handle, bucket))
|
|
|
|
|
{
|
2020-10-14 15:23:01 +08:00
|
|
|
printf("hos_verify_bucket failed ... \n");
|
2020-09-23 19:06:09 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
debuginfo("hos_verify_bucket success ... \n");
|
|
|
|
|
|
2020-09-27 11:58:23 +08:00
|
|
|
#if 1
|
|
|
|
|
mode = FILE_MODE;
|
2020-09-23 19:06:09 +08:00
|
|
|
debuginfo("hos_upload_file start ...\n");
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
|
|
|
|
for (i = 0; i < test_times; i++)
|
|
|
|
|
{
|
2020-10-14 15:23:01 +08:00
|
|
|
fd[i] = hos_open_fd(handle, bucket, object, callback, (void *)&data, 0, mode);
|
2020-10-20 17:17:12 +08:00
|
|
|
hos_write(fd[i], object, 0, 0);
|
2020-09-23 19:06:09 +08:00
|
|
|
}
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
|
|
|
|
time = calc_time(start, end);
|
|
|
|
|
time /= test_times;
|
|
|
|
|
printf("hos_upload_file spent %llu ns\n", time);
|
|
|
|
|
debuginfo("hos_upload_file end ...\n");
|
|
|
|
|
#else
|
|
|
|
|
|
2020-09-27 11:58:23 +08:00
|
|
|
mode = BUFF_MODE | APPEND_MODE;
|
2020-10-14 15:23:01 +08:00
|
|
|
#if 0
|
2020-09-27 11:58:23 +08:00
|
|
|
for (i = 0; i < test_times; i++)
|
2020-09-23 19:06:09 +08:00
|
|
|
{
|
|
|
|
|
}
|
2020-10-14 15:23:01 +08:00
|
|
|
#endif
|
2020-09-23 19:06:09 +08:00
|
|
|
debuginfo("hos_upload_buf start ...\n");
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
|
|
|
|
for (i = 0; i < test_times; i++)
|
|
|
|
|
{
|
2020-10-14 15:23:01 +08:00
|
|
|
fd[i] = hos_open_fd(handle, bucket, object, callback, (void *)&data, 0, mode);
|
|
|
|
|
//printf("hos_write start...\n");
|
|
|
|
|
if(hos_write(fd[i], buf, buf_size, 0, i + 1) != HOS_CLIENT_OK)
|
|
|
|
|
{
|
|
|
|
|
printf("error: hos_write!\n");
|
|
|
|
|
}
|
|
|
|
|
//printf("hos_write success...\n");
|
2020-09-23 19:06:09 +08:00
|
|
|
}
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
|
|
|
|
time = calc_time(start, end);
|
|
|
|
|
time /= test_times;
|
|
|
|
|
printf("hos_upload_buf spent %llu ns\n", time);
|
|
|
|
|
debuginfo("hos_upload_buf end ...\n");
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
debuginfo("hos_client_close start ...\n");
|
|
|
|
|
if (hos_client_destory(handle) == 0)
|
|
|
|
|
{
|
2020-10-14 15:23:01 +08:00
|
|
|
//printf("start:%lu\n", start.tv_sec * 1000 * 1000 * 1000 + start.tv_nsec);
|
|
|
|
|
//printf("finished:%lu\n", finished.tv_sec * 1000 * 1000 * 1000 + finished.tv_nsec);
|
2020-09-23 19:06:09 +08:00
|
|
|
time = calc_time(start, finished);
|
|
|
|
|
time /= test_times;
|
|
|
|
|
printf("hos upload finished spent %llu ns\n", time);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debuginfo("hos_client_close end ...\n");
|
2020-11-02 17:55:02 +08:00
|
|
|
hos_shutdown_api();
|
2020-10-14 15:23:01 +08:00
|
|
|
free(buf);
|
2020-09-23 19:06:09 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|