|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 15, |
| 6 | + "id": "2ad860c6", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import bigframes.pandas as bpd\n", |
| 11 | + "\n", |
| 12 | + "bpd.options.bigquery.ordering_mode = 'partial'\n" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "code", |
| 17 | + "execution_count": 16, |
| 18 | + "id": "03a64e81", |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "df = bpd.read_gbq(\"bigquery-public-data.baseball.schedules\")[[\"homeTeamName\", \"awayTeamName\", \"duration_minutes\"]]\n" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 17, |
| 28 | + "id": "b71fd6b4", |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "import math\n", |
| 33 | + "\n", |
| 34 | + "def moderate_cpu_work(n: int) -> int:\n", |
| 35 | + " \"\"\"\n", |
| 36 | + " Performs a fixed amount of CPU-bound math to test UDF overhead.\n", |
| 37 | + " \"\"\"\n", |
| 38 | + " if n is None:\n", |
| 39 | + " return 0\n", |
| 40 | + " \n", |
| 41 | + " # Using a fixed iteration count ensures the 'work' is consistent \n", |
| 42 | + " # regardless of the input value, which helps in benchmarking.\n", |
| 43 | + " iterations = 100_000 \n", |
| 44 | + " aggregate = float(n)\n", |
| 45 | + " \n", |
| 46 | + " for i in range(iterations):\n", |
| 47 | + " # A mix of trig and power functions to keep the CPU busy\n", |
| 48 | + " # We use i to ensure the calculation doesn't get optimized away\n", |
| 49 | + " aggregate += math.sqrt(abs(math.sin(i) * math.cos(aggregate)))\n", |
| 50 | + " \n", |
| 51 | + " # Return a valid integer (modulo-ed to keep it clean)\n", |
| 52 | + " return int(aggregate) % 1_000_000" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": null, |
| 58 | + "id": "651095ed", |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "classic_func = bpd.remote_function(reuse=False, cloud_function_service_account=\"default\", cloud_function_memory_mib=16384)(moderate_cpu_work)\n", |
| 63 | + "concurrent_func = bpd.remote_function(reuse=False, cloud_function_service_account=\"default\", cloud_function_memory_mib=16384, workers=33, concurrency=32)(moderate_cpu_work)\n" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": null, |
| 69 | + "id": "608f3659", |
| 70 | + "metadata": {}, |
| 71 | + "outputs": [ |
| 72 | + { |
| 73 | + "name": "stderr", |
| 74 | + "output_type": "stream", |
| 75 | + "text": [ |
| 76 | + "/usr/local/google/home/tbergeron/src/bigframes/bigframes/core/logging/log_adapter.py:183: TimeTravelCacheWarning: Reading cached table from 2026-02-24 19:15:25.384225+00:00 to avoid\n", |
| 77 | + "incompatibilies with previous reads of this table. To read the latest\n", |
| 78 | + "version, set `use_cache=False` or close the current session with\n", |
| 79 | + "Session.close() or bigframes.pandas.close_session().\n", |
| 80 | + " return method(*args, **kwargs)\n" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "data": { |
| 85 | + "text/html": [ |
| 86 | + "\n", |
| 87 | + " Query processed 62.6 kB in 27 seconds of slot time. [<a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:71a54816-1dba-4943-9537-9f8322fd4a11&page=queryresults\">Job bigframes-dev:US.71a54816-1dba-4943-9537-9f8322fd4a11 details</a>]\n", |
| 88 | + " " |
| 89 | + ], |
| 90 | + "text/plain": [ |
| 91 | + "<IPython.core.display.HTML object>" |
| 92 | + ] |
| 93 | + }, |
| 94 | + "metadata": {}, |
| 95 | + "output_type": "display_data" |
| 96 | + } |
| 97 | + ], |
| 98 | + "source": [ |
| 99 | + "df1 = bpd.read_gbq(\"bigquery-public-data.bitcoin_blockchain.blockss\")[[\"block_id\"]]\n", |
| 100 | + "r1 = df1.assign(result_val=df1[\"block_id\"].apply(classic_func)).to_gbq()\n" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": null, |
| 106 | + "id": "54fb7a54", |
| 107 | + "metadata": {}, |
| 108 | + "outputs": [ |
| 109 | + { |
| 110 | + "name": "stderr", |
| 111 | + "output_type": "stream", |
| 112 | + "text": [ |
| 113 | + "/usr/local/google/home/tbergeron/src/bigframes/bigframes/core/logging/log_adapter.py:183: TimeTravelCacheWarning: Reading cached table from 2026-02-24 19:15:25.384225+00:00 to avoid\n", |
| 114 | + "incompatibilies with previous reads of this table. To read the latest\n", |
| 115 | + "version, set `use_cache=False` or close the current session with\n", |
| 116 | + "Session.close() or bigframes.pandas.close_session().\n", |
| 117 | + " return method(*args, **kwargs)\n" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "data": { |
| 122 | + "text/html": [ |
| 123 | + "\n", |
| 124 | + " Query processed 62.6 kB in 25 seconds of slot time. [<a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:US:cd16b23e-a47a-4c16-9cfe-0ba230a324dd&page=queryresults\">Job bigframes-dev:US.cd16b23e-a47a-4c16-9cfe-0ba230a324dd details</a>]\n", |
| 125 | + " " |
| 126 | + ], |
| 127 | + "text/plain": [ |
| 128 | + "<IPython.core.display.HTML object>" |
| 129 | + ] |
| 130 | + }, |
| 131 | + "metadata": {}, |
| 132 | + "output_type": "display_data" |
| 133 | + } |
| 134 | + ], |
| 135 | + "source": [ |
| 136 | + "df2 = bpd.read_gbq(\"bigquery-public-data.bitcoin_blockchain.blocks\")[[\"block_id\"]]\n", |
| 137 | + "r2 = df2.assign(result_val=df2[\"block_id\"].apply(concurrent_func)).to_gbq()\n" |
| 138 | + ] |
| 139 | + } |
| 140 | + ], |
| 141 | + "metadata": { |
| 142 | + "kernelspec": { |
| 143 | + "display_name": "venv (3.12.6)", |
| 144 | + "language": "python", |
| 145 | + "name": "python3" |
| 146 | + }, |
| 147 | + "language_info": { |
| 148 | + "codemirror_mode": { |
| 149 | + "name": "ipython", |
| 150 | + "version": 3 |
| 151 | + }, |
| 152 | + "file_extension": ".py", |
| 153 | + "mimetype": "text/x-python", |
| 154 | + "name": "python", |
| 155 | + "nbconvert_exporter": "python", |
| 156 | + "pygments_lexer": "ipython3", |
| 157 | + "version": "3.12.6" |
| 158 | + } |
| 159 | + }, |
| 160 | + "nbformat": 4, |
| 161 | + "nbformat_minor": 5 |
| 162 | +} |
0 commit comments