summaryrefslogtreecommitdiff
path: root/example/hos_write_complete.cpp
blob: ebe13fbc3d023aef6a6f9732a089750bb535caf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*************************************************************************
    > 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>
}
#include"../src/hos_client.h"

//#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;
}

void callback(bool result, const char *error, void *userdata)
{
    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]);
    //int test_times = 10000;
    //char *buf = (char *)malloc(1024 * 1024 * 4);
    char buf[1024 * 1024 * 4];
    //char buf[1024 * 4];
    size_t buf_size;
    int mode = FILE_MODE;
    size_t fd[10000] = {0}; 
    userdata_t data = {&finished};

    file_to_buffer(object, buf, &buf_size);

    debuginfo("hos_client_init start ...\n");
    hos_client_handle handle = hos_client_create("http://192.168.44.10:9098/hos/", "default", "default", 4);
    if (handle == NULL) 
    {
        debuginfo("error:hos_client_handle\n");
        return -1;
    }  
    debuginfo("hos_client_init success ... \n");

    debuginfo("hos_create_bucket start ... \n");
    if(hos_create_bucket(handle, bucket))
    {
        debuginfo("hos_create_bucket failed ... \n");
        return -1;
    }
    debuginfo("hos_create_bucket success ... \n");

    debuginfo("hos_verify_bucket start ... \n");
    if(!hos_verify_bucket(handle, bucket))
    {
        debuginfo("hos_verify_bucket failed ... \n");
        return -1;
    }
    debuginfo("hos_verify_bucket success ... \n");
    
#if 1
    mode = FILE_MODE;
    for (i = 0; i < test_times; i++)
    {
        fd[i] = hos_open_fd(handle, bucket, object, callback, (void *)&data, 0, mode);
    }

    debuginfo("hos_upload_file start ...\n");
    clock_gettime(CLOCK_MONOTONIC, &start);
    for (i = 0; i < test_times; i++)
    {
        hos_write(fd[i], object, 0, 0, 0);
    }
    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

    mode = BUFF_MODE | APPEND_MODE;
    for (i = 0; i < test_times; i++)
    {
        fd[i] = hos_open_fd(handle, bucket, object, callback, (void *)&data, 0, mode);
    }
    debuginfo("hos_upload_buf start ...\n");
    clock_gettime(CLOCK_MONOTONIC, &start);
    for (i = 0; i < test_times; i++)
    {
        hos_write(fd[i], buf, buf_size, 0, i);
    }
    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)
    {
        time = calc_time(start, finished);
        time /= test_times;
        printf("hos upload finished spent %llu ns\n", time);
    }

    debuginfo("hos_client_close end ...\n");

    return 0;
}