Skip to content

Commit 0176b4c

Browse files
authored
Add COPY benchmark (#152)
Closes GH-151
1 parent 4e30c2a commit 0176b4c

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

benchmark/integer/copy.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include <stdbool.h>
21+
#include <stdio.h>
22+
#include <stdlib.h>
23+
#include <sys/time.h>
24+
25+
#include <libpq-fe.h>
26+
27+
int
28+
main(int argc, char** argv)
29+
{
30+
PGconn* connection = PQconnectdb("dbname=afs_benchmark");
31+
PGresult* result;
32+
struct timeval before;
33+
struct timeval after;
34+
35+
if (PQstatus(connection) != CONNECTION_OK)
36+
{
37+
fprintf(stderr, "failed to connect: %s\n", PQerrorMessage(connection));
38+
PQfinish(connection);
39+
return EXIT_FAILURE;
40+
}
41+
42+
gettimeofday(&before, NULL);
43+
result = PQexec(connection, "COPY data TO STDOUT (FORMAT binary)");
44+
if (PQresultStatus(result) != PGRES_COPY_OUT)
45+
{
46+
fprintf(stderr, "failed to copy: %s\n", PQerrorMessage(connection));
47+
PQclear(result);
48+
PQfinish(connection);
49+
return EXIT_FAILURE;
50+
}
51+
52+
while (true)
53+
{
54+
char* buffer;
55+
int size = PQgetCopyData(connection, &buffer, 0);
56+
if (size == -1)
57+
{
58+
break;
59+
}
60+
if (size == -2)
61+
{
62+
fprintf(stderr, "failed to read copy data: %s\n", PQerrorMessage(connection));
63+
PQclear(result);
64+
PQfinish(connection);
65+
return EXIT_FAILURE;
66+
}
67+
/* printf("%.*s\n", size, buffer); */
68+
free(buffer);
69+
}
70+
gettimeofday(&after, NULL);
71+
printf("%.3fsec\n",
72+
(after.tv_sec + (after.tv_usec / 1000000.0)) -
73+
(before.tv_sec + (before.tv_usec / 1000000.0)));
74+
PQclear(result);
75+
76+
return EXIT_SUCCESS;
77+
}

benchmark/integer/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
# under the License.
1717

1818
libpq = dependency('libpq')
19+
executable('copy', 'copy.c', dependencies: [libpq])
1920
executable('select', 'select.c', dependencies: [libpq])

benchmark/integer/select.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ main(int argc, char** argv)
3737

3838
if (PQstatus(connection) != CONNECTION_OK)
3939
{
40-
fprintf(stderr, "failed to connect: %s", PQerrorMessage(connection));
40+
fprintf(stderr, "failed to connect: %s\n", PQerrorMessage(connection));
4141
PQfinish(connection);
4242
return EXIT_FAILURE;
4343
}
@@ -46,7 +46,7 @@ main(int argc, char** argv)
4646
result = PQexec(connection, "SELECT * FROM data");
4747
if (PQresultStatus(result) != PGRES_TUPLES_OK)
4848
{
49-
fprintf(stderr, "failed to select: %s", PQerrorMessage(connection));
49+
fprintf(stderr, "failed to select: %s\n", PQerrorMessage(connection));
5050
PQclear(result);
5151
PQfinish(connection);
5252
return EXIT_FAILURE;

0 commit comments

Comments
 (0)