|
| 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 | +} |
0 commit comments